I'm making a Windows Form application that is supposed to play an animated gif while playing music.
I used SoundPlayer, and everything worked flawlessly. Then I found out I can't play multiple audio files with SoundPlayer, which was a problem (Background music AND effects needed).
So I looked a little bit into DirectSound, but my application froze everytime I did anything DirectSound related (Device, Buffer...).
So now I'm trying out WMP, I was happy at first, but when I enabled a GIF (Transparent, in case it matters), the music stopped in less than a second while you could see the GIF to stop for a frame or so, like it's loading something heavy. And this happens every time that audio and GIF are playing at the same time, no matter if one of them started in advance. When I used SoundPlayer, I could play 20 animated GIFs at once with no effect on audio.
To sum it up:
SoundPlayer = Only one audio file, unacceptable
DirectSound = Freezes for some reason (Maybe worth trying again? I'd like to put focus of this question on WMP though, I lost the code anyway)
WMP = Won't play along nicely with animated GIFs, which are required.
I tried playing both .wav and .mp3 file, no difference.
I added the wmp.dll reference before.
using System;
using System.Windows.Forms;
using System.Media;
private void button2_Click(object sender, EventArgs e)
{
var player = new WMPLib.WindowsMediaPlayer();
player.URL = "song.wav";
pictureBox1.Visible = true;
}
Is there something I should know about WMP? Can you recommend some other method? It does seem that the audio stops when the pocess reaches certain memory usage. It's steady on about 45 MB, but once I play the audio file it starts increasing, and plays until it reaches about 48 MB when it stops and returns to 45 MB.
Thanks for taking your time and helping me in any way.
EDIT: I was using a GIF picture with resolution of 500x500 which I used SizeMode=StretchImage on to actually make it appear smaller (200x200). In this case, the music played for about 0.2 seconds. I resized the resource picture to 100x100, using StretchImage to make it appear as 200x200 again, and the music played for about 8 seconds, about 4 seconds if I loaded two GIFs at once. Changing the .mp3 bitrate to make it just 170KB didn't help at all. I mean my Google Chrome takes 3GB of my memory and still plays multiple videos just fine, while my Form is taking not even 50MB and struggles. Surely there must be a way to play GIF and a song. Is there some kind of MemoryAssignLimit property? Or does WMP simply fail when it comes to bigger resources? Is it a limitation set by WinForms?
Followed this solution, and it worked like charm, GIFs everywhere and 30 overlapping songs, and nothing stops unless I want it to. Great.
Related
i am using vlc player in winforms. its working fine, but the problem is that its playing faster. i have videos recorded at 10fps. i think vlcControl is playing at 30fps, that's why video are playing faster. Referred this, but it didn't help. i couldn't find any function like set_play_back_speed function under vlcControl1.VideoI thought vlcControl would automatically figure out fps & play accordingly. if i open the same video from vlc media player installed on my windows pc, it plays at proper rate.
any clue how to fix this?
P.S: if i open any video recorded at 29.97fps, vlcControl in winforms is playing it at proper speed.
Well, there's no function to set FPS to video thru Vlc.DotNet and I am not sure are you using WinForms or WPF application, but best I can help is probably, if you would play with rate settings as in transpone video.
It's found at vlcControl1.rate (which is float value default is 1.00), if you have 30fps and only want 10fps well, you could try to set:
vlcControl1.rate = (int)(33 / 100);
Which then would decrease video and audio transpone to 10fps from 30fps (assuming 1.00 = 30fps).
Hello fellow developers.
I made a c# player to play videos and audio. To do that I used the System.Windows.Media MediaPlayer and it works great.
Here is the problem I encountered:
In some music files (not all of them) seeking to X time or playing it from the beginning to X time will have diffrent results.
In other words, If I play the song from the beginning, after 20 seconds the singer will say "Hello" but if I use the seek function to 20 sec I will hear the singer say something else (and 2 seconds later he will say "Hello").
I had to check if its the library mistake so I opened windows media player and played the song, When I click on the progress bar to X progress or when I play from the beginning to X progress the song is at different time.
Same like the library.
I checked this with VLC and it worked perfectly.
So here is my questions to you.
Does it mean that Media Player doesn't know how to seek this music file correctly?
Do you know how to fix this?
Should I use a different player, which one do you recommend?
I found the problem,
Encoding the audio as VBR made it happen like that.
Changing to CBR solved this.
I imported a axWindowsMediaPlayer control to my form to play a sequence of pictures and/or small videos.
Now I want the user to adjust the slide show to his own needs, so he should be able to select the "showtime" of each picture and just play the video's length.
I tried to apply a timer to the form, and change the axWindowsMediaPlayer URL every "x" amount of seconds. This works perfectly for pictures, but videos now have an extra blank screen or footage cut off (depends on the time of the video compared to the "x" amount of seconds).
I want to to use an event, which will trigger when the media has stopped playing(so it wont cut off video's), but when I searched trough the axWindowsMediaPlayer event's list, I only found onMediaChanged. This event is kinda strange, it triggers a few times on the beginning of a video and at the end...
Is there anyway I can solve this? Maby using something different than the axWindowsMediaPlayer?
I found the solution after google'ing for hours. If anyone wants to know how to acces the axWindowsMediaPlayer's duration:
axWindowsMediaPlayer1.currentMedia.duration
It's a property (get and set) so this is all I needed.
I'm skeptical if I'm doing this the right way.
I have to play an animation at about 60fps. I'm using a List<Image> to load all the frames at the time of initialization. And then using a System.Timers.Timer to call an event at every 10ms which will change the image in the pictureBox.
List<Image> imageList = new List<Image>();
private static Timer_Event(o, e)
{
pictureBox.Image = imageList[i++];
}
So, am I doing this right? Is there a better approach?
What you're describing seems unlikely to work in a robust fashion. Here's a brief summary:
In my experience, timers aren't generally accurate. Since timer messages go through the Windows Message pump, they can only move as fast as your Windows UI processes messages. Any application that interferes with messages will cause your timers to stutter, although careful work can prevent most of this. Some classic discussion of timers here: Winforms Timer for Dummies
In general, List is a very inefficient way to store images. For a one-second animation at 60 frames per second, you will have to hold 60 images in memory and decompress each one individually.
Here are some possible solutions, and the tradeoffs they entail:
Full Motion Video
If you are looking to present a full motion video on your form, you should really consider using a MediaElement (for WPF applications: http://www.c-sharpcorner.com/uploadfile/dpatra/media-element-in-wpf/ ) or a MediaPlayer object (for WinForms applications: http://msdn.microsoft.com/en-us/library/bb383953(v=vs.90).aspx ).
This will allow you to play a lengthy video that contains extremely high quality images, varied compression, and start or stop the playback arbitrarily. However, the startup and memory usage requirements of a full video player are greater than just displaying a single image on screen. You will find that your application takes a moment to initialize the video subsystem, which may be annoying.
Animated Images
It's possible to show an animated image in a picturebox in Windows Forms or WPF. You would simply generate the animated image - generally using a GIF animation file. This will work smoothly for a majority of simple animations, and it's possible to get free-to-use animated GIFs from websites like this one: http://www.chimply.com/Generator
Here's a walkthrough of how to place an animated GIF on your form: http://trompelecode.com/2010/12/animated-progress-indicator-in-csharp-windows-forms/
Sprite Animation
Let's say you need to accurately represent each image exactly (which isn't necessary if you're simply trying to look appealing), and that you don't want the overhead of a video system (which is okay if you're only playing a second or two worth of animations). What you want to do then is create a single composite "sprite" image. This reduces the memory overhead requirements of your application and reduces the amount of time decompressing files.
For example, here's a website that generates PNG sprites for you: http://wearekiss.com/spritepad
Once you generate an image with sprites, you can place it within a picturebox and animate the image by changing the relative position of the image within the picturebox. Here's a walkthrough of how to accomplish this: C# picturebox load image with an offset
Summary
Any way you choose to display an animated image, you will have some tradeoffs. I like to pick the simplest possible solution for myself - and in my case I like to use an animated GIF image. Good luck animating!
The .avi is alternative otherwise, however, if you have bunch of images than probably utilizing threads/timers apparently seems to be the way forward.
The easiest way would be to use an animated gif in that pictureBox and don't mess with timers.
You may also be able to put your animation together using WPF or Silverlight ... if you need the animation to be interactive or programmatically controllable that would probably be the way to go.
If on the other hand the animated content is static then AVI (or MPEG etc.) is probably your best bet.
You don't say exactly what you're trying to accomplish so I can't be sure.
I am trying to implement a simple game in .NET with C#. There is a ball bouncing against objects, and when it bounces I play a sound asynchronously using System.Media:SoundPlayer. The problem is that most of the time (not always) the ball freezes at the time of impact, for as long as the sound plays.
This happens on Windows XP, but not on Windows 7. I use .NET 3.5.
And the weirdest thing is that the problem disappears if I open the application Windows Media Player and play a few seconds of an mp3.
Here's the code where I initialize the SoundPlayer object:
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream s = a.GetManifestResourceStream("Game.Resources.GameBounce.wav");
_playerBounce = new System.Media.SoundPlayer(s);
_playerBounce.Load();
...and when the ball hits an obstacle, this line is called:
_playerBounce.Play();
I have also tried "Attempt #4" in this post:
How to use System.Media.SoundPlayer to asynchronously play a sound file?
Another desperate attempt, based on some advice I found somewhere on the net, was to save the sound stream to a file, that SoundPlayer works better when initialized from a file.
Neither of those attempts worked.
Any ideas?