Windows Media Player seamless looping of video - c#

I am implementing a Windows Media Player into my WinForms application. axWindowsMediaPlayer is in the application and it is working fine, but my issue is that there is a black-screen flicker in between the playing of the video. But the odd thing is that it does not flash the black screen in between the first and second times playing the video. So, this leads me to believe it must be some sort of buffering or something in which during the first play it keeps up fine but thereafter is has problems keeping up with the loading and also playing of the video at the same time.
So, basically what I wondering is if the best fix would be to somehow completely cache the video, or somehow pause it as it loads it again, and maybe I have to go about this all based on a timer or something to that effect. In any case, please let me know what, if anything is known for a fix to this problem of the blackscreen flicker in between looping of a video in a Winforms embedded Windows Media Player.

You can try to manually reset video current position using follow code. It will start video from beginning without any flicker.
AxWindowsMediaPlayer.Ctlcontrols.currentPosition = 0
Or use this within a timer tick event and when video is near end then manually play it from beginning like this.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If AxWindowsMediaPlayer1.Ctlcontrols.currentPosition > AxWindowsMediaPlayer1.Ctlcontrols.currentItem.duration - 0.01 Then
AxWindowsMediaPlayer1.Ctlcontrols.currentPosition = 0
End If
End Sub
I hope this helps.

private void timer1_Tick(object sender, EventArgs e)
{
if(axWindowsMediaPlayer1.Ctlcontrols.currentPosition > axWindowsMediaPlayer1.Ctlcontrols.currentItem.duration - 0.01)
{
axWindowsMediaPlayer1.Ctlcontrols.currentPosition = 0;
}
}
this code change currentPosition to 0 second when video running 0.01 milliseconds so that transition effect of black screen can be avoided

Related

C# Windows Media Player video playing behind form

I am trying to play a video after a certain event is triggered.
Currently the video starts playing but it stays behind the form (You can see it playing in the opacity of the toolbar). I have tried to bring it to the front, refresh it, select it, update it but none of it seems to work. Also, if I manually open the windows media player program and close it the video "jumps" to the front of the screen.
This is the code used to start playing the video
wmp.settings.autoStart = true;
wmp.uiMode = "none";
wmp.Visible = true;
wmp.URL = #"C:\folder\video.mp4";
wmp.Update();
I also check if the video is still playing using the Status_Change event to set it to full screen and try to bring it to front
private void wmp_StatusChange(object sender, EventArgs e)
{
if (wmp.playState == WMPLib.WMPPlayState.wmppsPlaying)
{
wmp.fullScreen = true;
wmp.BringToFront();
wmp.Update();
}
}
Despite these efforts the video still plays behind the form. Any suggestions would be appreciated!

Is there any default time interval for mediaended event to be fired?

I have a WPF application which dynamically loads video or image files(depending on user's choice, to a MediaElement control.
It is working fine when it is a video and gets the MediaEnded event fired on ending the video.
But when I load an image, the MediaEndedevent is fired within 5 seconds.
Is it a default value? or can I change it programmatically?
Is there any property to change this interval or disable such an option?
Is it possible to make it paused until a specific action?
I have set the following properties as follows
MediaControl1.LoadedBehavior = MediaState.Manual;
MediaControl1.UnloadedBehavior = MediaState.Manual;
MediaElement is a (very thin) wrapper around Windows Media Player (or rather - uses the same framework which is used by Windows Media Player). If you open an image in Windows Media Player - you will see it will "play" it like a slideshow (even for 1 image), for about 5 seconds. That's why you get MediaEnded event in 5 seconds - Windows Media Player plays slideshow with your image for that duration. I doubt there is a way to change this from WPF (because it's behavior of external program\framework, not related to MediaElement itself) and I'm not aware of the way to change this for Windows Media Player (and even if there is such a way - it will have global effect and you probably don't want to modify your clients computer in such a way).
To solve your problem - just don't use MediaElement for displaying images - use something like Image control. If you have really strong reasons to do that - you can pause MediaElement with Pause method after your "slideshow" has been loaded, then it will not fire MediaEnded event. All in all - I cannot imagine any use case where you really have to use MediaElement for images.
You can repeat the media when you load image. Handle MediaEnded event like this:
void me_MediaEnded(object sender, EventArgs e)
{
//play video again
mediaElement.Position = new TimeSpan(0, 0, 1);
mediaElement.Play();
}
Additionally see this. It may help:
https://stackoverflow.com/a/3406857/5675763

C# - Xamarin Forms audio player stops working after 5 or 6 uses

I am using this implementation of an Audio Player in Xamarin
https://www.codeproject.com/Articles/1088094/Playing-audio-mp-File-in-Xamarin-Forms
I have a button in my code that, when pressed, plays a certain, short (1-2 seconds) tone.
The player in the link works well, but for some reason if I repeatedly press my button like 5 or 6 times the audio player doesnt work anymore. Even if I leave the page and come back the audio player still doesn't work. What could be causing this? I'm fearing that it might be a device security thing on Android, because why else would it play the first few times?
Here is my code for clicking the button
void PlayChordClicked(object sender, EventArgs e)
{
DependencyService.Get<IAudio>().PlayAudioFile(myMp3);
}
Everything else is exactly as copied from the tutorial.
Thanks!
With help from Gusman in the comments, I figured out that I was most likely crashing the app by not disposing of the MediaPlayer after using it.
I added these few lines in the Android "PlayAudioFile" method and it did the trick
player.Completion += delegate
{
player.Release();
player.Dispose();
};

UWP Windows.Media.Playback - Stop/Unload Clip

I'm writing a UWP application which contains the MediaPlayer object. I am wanting to program a stop button which stops the media and sets the media to black however I can't find an approach to do it short of loading a short 'black' clip as an internal asset or hiding the element via the compositor.
The MediaPlayer does not have a function for stop, only play/pause and further, setting the media source to null simply pauses playback and freezes on the last frame shown rather than dropping to black.
Any suggestions would be much appreciated.
I recently had a related problem that boiled down to unloading media from a MediaPlayerElement.
If you navigate away from a page when playback is active, the MediaPlayerElement would not suspend or unload a playback in progress so the user would keep hearing the audio. If you re-open player's page, a new instance of a player control will be created and user may hear multiple audio tracks at once (although this requires different media sources). So it was essential to unload the media at some place like player's Unloaded event handler, page Unloaded handler, or OnNavigatingFrom/OnNavigatedFrom overrides.
Anyway, the code below stops and unloads a video/clip/media from the MediaPlayerElement.
MyMediaPlayerElement.MediaPlayer.Pause();
MyMediaPlayerElement.Source = null;
Actually, a null assignment line works just fine as a stop-and-unload, but since as of now (3/19/2018), MS documentation doesn't seem to cover how nullifying the source supposed to work, I prefer to keep an otherwise unnecessary call to Pause() here as well.
The MediaPlayer does not have a function for stop, only play/pause
As you known, MediaPlayerElement doesn't have stop method at default. Since MediaPlayerElement is newly for version 1607, for earlier version of Windows 10 we use MediaElement instead, so you can simply use MediaElement instead. MediaElement has Stop method, and after you invoke the stop method, the MediaElement will show black and set the media to begin which is just what you want. Code as follows:
<MediaElement x:Name="mediaelement" AreTransportControlsEnabled="True" Height="400" Width="400" AutoPlay="True" Source="http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_30fps_normal.mp4"></MediaElement>
<Button x:Name="btnstop" Click='btnstop_Click' Content="stop"></Button>
Code behind:
private void btnstop_Click(object sender, RoutedEventArgs e)
{
mediaelement.Stop();
}

Gif image looping only once

I was trying to figure out how to make sure that a GIF image what I put to my WindowsForm project through picturebox going to loop only once and still without finding an answer even after going through as many sites with this problem as I could find.
I know that with using of picturebox programmer lossing control of looping (in case that you set looping only once when creating the gif animation in editor) and I somehow have to get a code which will basically say when gif animation gets to some certain frame...in my case to last one which is number 7 and gets there in 430 milliseconds (190,40,40,40,40,40,40) the gif animation going to be Disposed.
So the whole idea was to make a simple game with balloons which will have an animation with flying and animation with exploding after pressing of the mouse on the ballon_grey picture box.
It would look like that:
private void timer1_Tick(object sender, EventArgs e)
{
//makes balloon going up
balloon_grey.Location = new Point(balloon_grey.Location.X, balloon_grey.Location.Y - 3);
}
private void balloon_grey_Click(object sender, EventArgs e)
{
//forces balloon_grey picturebox to change animation to balloon_grey_explosion.gif
balloon_grey.Image = Image.FromFile("balloon_grey_explosion.gif");
//gets the amount of frames from the gif animation...don't know if this is needed to reach the goal
int frames = Properties.Resources.balloon_grey_explosion.GetFrameCount(FrameDimension.Time);
}
Thank you for your time and any advice about the problem.

Categories