如何在Windows8中使用MediaElement更改视频的位置?

use*_*555 2 c# mediaelement microsoft-metro windows-8 windows-runtime

目前我在我的代码中这样做,

 playbackElement1.AutoPlay = true;
 playbackElement1.SetSource(stream, this.m_recordStorageFile.FileType);
 playbackElement1.Position = new TimeSpan(0, 0, 0, 0, 5000);
 playbackElement1.Play();
Run Code Online (Sandbox Code Playgroud)

它不起作用,检查视频超过5秒.

akt*_*ton 9

有两个问题.首先,MediaElement只能在加载媒体后设置位置,通过处理MediaOpened事件来确定.其次,并非所有媒体都是可追求的.通过致电CanSeek进行检查.使用类似的东西:

playbackElement1.AutoPlay = true;
// Will fire after the media has loaded
playbackElement1.MediaOpened += (source, args) =>
{
    MediaElement player = (MediaElement) source;
    if (player.CanSeek)
    {
        player.Position = new TimeSpan(0, 0, 0, 0, 5000);   
    }
}                     
playbackElement1.SetSource(stream, this.m_recordStorageFile.FileType);
Run Code Online (Sandbox Code Playgroud)

加载后,使用NaturalDuration属性查看媒体的长度,如果需要,使用HasTimeSpanTimeSpan属性将其转换为TimeSpan .