I can't achieve simple thing. I'm using MediaElement and I want to play my video not from the beginning, but from, let's say, position 5 sec.
Workflow:
LoadedBehavior <- Manual;
Source <- some_source;
Play();
Inside MediaOpened:
Position <- 5 sec
Problem: For the few moments I see the beginning of the video and then it switches to 5 sec.
I know that if I want to control Position manually I need to use LoadedBehavior Manual, but how can I make the video be Paused immediately after loading?
The way I have done it and works is this (_location & _position I want are passed into the containing method of course)
MediaE2.Source = _location;
MediaE2.Position = _position;
//MediaE2.Source = new Uri(#"C:\Users\Public\Videos\Sample Videos\Wildlife.wmv", UriKind.Absolute);
MediaE2.Play();
Set the position first then play.
Jim
Related
I need a main 'home' video (full screen HD) running in a loop, and then at some point (say the user presses a key) another short movie plays (one of twelve selected at random), and then back to the home movie loop.
I'm using the mediaElement in WPF from my C# code - Is there a way to load the next movie into memory so that it's ready to play instantly? It currently takes about a second or so...
(I'm actually using two media elements with the triggered movie on top - I was doing a cross fade but have taken that out now until I can buffer the next clip. Not sure if this is the best way of doing this, any ideas? Many Thanks.
I figured out a way in case anyone is interested.
You can just specify a bunch of mediaElements on top of each other (can have different z index but not essential). Then did
video.Opacity = 0.0;
video.Play();
video.Pause();
for all of them, then when you want to play one you just set the opacity to 1 and play it and it'll play instantly.
Don't know if that's the best way, but works well. Now going to implement a cross fade to finish up. Happy days.
i want to use a track bar in a window form which opens a media file as the video or audio plays i want to move the slider of the track bar accordingly . I had set all the properties of the track bar tool like maximun,minimun, tickfreguency small and large change accordingly.
using mediaposition i collect the currentPosition of the media and total duration using get_duration() method and sets the trackbar value to the current position but the problem is as the media is running the slider of the bar is not moving . i know i am missing something in the code .
Make sure you actually update that information in LOOP. So it would be updated, for example, in timer. Every 1 second.
You can also check what values you get from those two calls(get_duration and get_position) using debugger.
I'm currently developing a WPF application which requires strictly timing, says, being late 2 seconds matters.
I have a MediaElement mediaPlayer which seeks to a new position and play every time a Dispatcher timer is fired. But I notice that the mediaPlayer.Position is not very synced with the timer. In the example below, I set the dispatcherTimer fired after 55 seconds, but the value received from MessageBox in timer_Tick is 108.276746, which is late 2 seconds (55 + 55 = 110).
private void button1_Click(object sender, RoutedEventArgs e)
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(55);
timer.Tick += new EventHandler(timer_Tick);
mediaPlayer.Source = new Uri("test.wma", UriKind.Relative);
_currentPosition = 55;
mediaPlayer.Position = TimeSpan.FromSeconds(_currentPosition);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
MessageBox.Show("Position" + mediaPlayer.Position.TotalSeconds);// print around 108 seconds
}
This is the problem because I need the mediaPlayer position is perfectly synced with the dispatcher timer.
For more information, the root problem here is: the dispatcher timer to strictly follow the mediaElement progress, because I need to sync other controls with the position that mediaPlayer. Being late 2 seconds is unacceptable. Does anyone know how to achieve this effect?
UPDATE PURPOSE: I'm trying to "switch illustration image" according to the playing position from an audio. For example, when the narrator read to "... We have a beautiful house" in the audio, the program will show pictures of a beautiful building. But now since the position is late, it will show the picture long before the audio mentions it.
As pointed out, it takes time for the media elements to load, therefore it is not wise to use a countdown timer. You should do the the other way round: the DispatchTimer fires say like 1 or 2 times every second, and when it's fired you check the position of the media element. If it's at a certain position then show the picture.
This approach also limits the maximum error do the time interval of your DispatchTimer events, assuming that the system fires them accurately.
I've used the media element a lot.
My suspicion is that the timer is fine ( give or take a few ms )
but the mediaplayer position is definitely guaranteed not to be where you expect it to be.
The 2 seconds could well be accounted for in loading the video file and loading the video/audio codecs. further more, if there is any lag at all ( cpu or ram spike or other ) the mediaplayer will also lag while the timer will not.
perhaps setup a scenario where the video is guaranteed to be loaded ( for example pausing it somewhere in the middle of the video ) then start the timer and play the video from there to check.
If you want a video player that doesn't have the MediaElement's slow initialization time for videos, try Jeremiah Morrill's MediaKit project. He has made some great improvements including load times. It is also open source so if you need more or information on where you're at in the video, you can add that to the source.
I am doing an alert system that will show messages 'a la' Messenger, and I want them to move or resize to make them appear.
How can I do this without having to do this:
do
{
this.prop += 1;
} while (this. prop = destination);
You need to make a Timer component and call the form's SetBounds method in the timer's Tick event.
You need a frame rate independent interpolator.
Take a look at: Frame Rate Independent interpolation. Basically, the idea is that you compute what your current position should be based upon the expected animation time and how long you've been animating... This means that the animation will take the same amount of time to go from point a to point b on any hardware.
Of course, you'll need to position the form with the values coming out of this, but thats the easy part!
set form.size property to change size, form.position to change position
I am currently working on an MP3 player (in a WPF application) with a WPF MediaPlayer and basically, I want to implement a Song Seeker which moves along with the current playing song.
I already implemented a song slider (from Sacha Barber's application) and it works when the user drags the seeker manually (as in, the song continues from that position) but I cannot figure out how to make the seeker move according to the current position in the song.
Trouble is I don't think there is a way to check when the Position property of the MediaPlayer has changed, so I'm stumped as to how I should implement this feature.
Any ideas on how to go about such an issue?
[Update]
As regards incrementing the seeker with a timer, I actually thought of using the reason I didn't try it yet is because I think there is a better way to implement this using the MediaTimeline...but I'm yet to figure out how.
ARISE answer! and serve your master
OK, I've figured out how to work this. I'm sure I'm not doing it the completely correct way but it does work.
Here is the code-behind of a WPF application, with a Pause/Play button.
public partial class Main : Window
{
MediaPlayer MPlayer;
MediaTimeline MTimeline;
public Main()
{
InitializeComponent();
var uri = new Uri("C:\\Test.mp3");
MPlayer = new MediaPlayer();
MTimeline = new MediaTimeline(uri);
MTimeline.CurrentTimeInvalidated += new EventHandler(MTimeline_CurrentTimeInvalidated);
MPlayer.Clock = MTimeline.CreateClock(true) as MediaClock;
MPlayer.Clock.Controller.Stop();
}
void MTimeline_CurrentTimeInvalidated(object sender, EventArgs e)
{
Console.WriteLine(MPlayer.Clock.CurrentTime.Value.TotalSeconds);
}
private void btnPlayPause_Click(object sender, RoutedEventArgs e)
{
//Is Active
if (MPlayer.Clock.CurrentState == ClockState.Active)
{
//Is Paused
if (MPlayer.Clock.CurrentGlobalSpeed == 0.0)
MPlayer.Clock.Controller.Resume();
else //Is Playing
MPlayer.Clock.Controller.Pause();
}
else if (MPlayer.Clock.CurrentState == ClockState.Stopped) //Is Stopped
MPlayer.Clock.Controller.Begin();
}
}
The trick is that once you set the clock of a MediaPlayer, it becomes clock controlled, thus the use of MPlayer.Clock.Controller to do all of the controlling :)
Never played with media player but assuming you know the length of song could you not setup a timer that ticks every second while the song is playing. Therefore for every tick just increment the seeker in relation to how long the song is in total.
Song is 100 seconds long. Therefore every second/tick is worth 1 percent of total progress.
You'd have to stop the timer when pausing song etc...
MediaElement has a position property which you could use for this: http://msdn.microsoft.com/en-us/library/system.windows.controls.mediaelement.position.aspx
Have you checked out the WPF MediaKit yet?