1

In my project, I use WPFMediaKit to play audio files in the format: myAudioFile.m3u8 which contains a list of links to myAudio1.ts, myAudio2.ts, etc.

I start playing files like this code:

    ...
   _player.Source = trackUri;

   _player.Dispatcher.Invoke((Action) (() =>
   {
       _player.Play();
       _player.MediaPosition = 100000000; // in ticks, doesn't work at this place 
   }));
   ...

In some cases, I need to start playing files not from the beginning, but for example from 10 seconds of playing.

How can I do that?

MakS
  • 131
  • 7
  • Did you try to set the MediaPosition first before calling `Play`? – rene Aug 21 '17 at 19:50
  • Of course I tried! – MakS Aug 21 '17 at 20:02
  • And? Any different results? Which player are you using btw? – rene Aug 21 '17 at 20:03
  • And no reaction. It looks like the player has not initialized these properties yet ... WPFMediaKit - this is the player. – MakS Aug 21 '17 at 20:10
  • That is not what I ask. I get that you use WPFMediakit but I want to know which instance of a [MediaoPlayerBase](https://github.com/Sascha-L/WPF-MediaKit/blob/3ecfa7830c71b78e894e1bfd622fed6e29e53050/Source/DirectShow/MediaPlayers/BaseClasses.cs#L149) derived class is used for `_player` as I see at least [three candidates](https://github.com/Sascha-L/WPF-MediaKit/tree/3ecfa7830c71b78e894e1bfd622fed6e29e53050/Source/DirectShow/MediaPlayers). I assume you have code similar to [this](https://github.com/Sascha-L/WPF-MediaKit/wiki) which uses `DvdPlayer`. I want to know what is used by you. – rene Aug 21 '17 at 20:17
  • I'm using [MediaUriElement](https://github.com/Sascha-L/WPF-MediaKit/blob/master/Source/DirectShow/Controls/MediaUriElement.cs) which is a wrapper over [MediaUriPlayer](https://github.com/Sascha-L/WPF-MediaKit/blob/3ecfa7830c71b78e894e1bfd622fed6e29e53050/Source/DirectShow/MediaPlayers/MediaUriPlayer.cs) – MakS Aug 21 '17 at 20:27
  • Can you try if you can set `_player.PreferedPositionFormat` to `MediaPositionFormat.MediaTime` and after that set the `MediaPosition` – rene Aug 21 '17 at 20:37
  • This is a step in the right direction. When playing the first track everything works, but when I transfer the second track to the same player, the track starts playing from the beginning, regardless of what is in MediaPosition. When playing the second track, MediaPosition and MediaDuration are filled with the previous values, which is probably how it affects. – MakS Aug 21 '17 at 21:10
  • Are you also setting the `PreferedPositionFormat` again? Point is that setter has logic to initialize the seeking interface. If that seeking interface remains null setting the MediaPosition has no effect. – rene Aug 21 '17 at 21:14
  • Just in case I set `PreferedPositionFormat` every time. Now I noticed that the second file is playing from the beginning, the third file is not playing from the beginning, but the fourth file can play both from the beginning and not from the beginning, although after each launch I set the `PreferedPositionFormat` and `MediaPosition` properties. – MakS Aug 21 '17 at 21:43

1 Answers1

0

Thanks to rene!
Set PreferedPositionFormat to MediaPositionFormat.MediaTime, before the position is changed. In most cases this is enough.

...
_player.Source = trackUri;

_player.Dispatcher.Invoke((Action) (() =>
{
   _player.Play();
   _player.PreferedPositionFormat = MediaPositionFormat.MediaTime;
   _player.MediaPosition = 100000000; // in ticks, doesn't work at this place 
}));
...
MakS
  • 131
  • 7
  • Unfortunately this does not work for me. No matter what value I set MediaPosition to, it always restarts from the beginning of the file. I am listening to an audio stream from a web url, does this make a difference? – Flopdong Aug 15 '18 at 17:41