現在、私は自分のコードでこれを行っています、
playbackElement1.AutoPlay = true;
playbackElement1.SetSource(stream, this.m_recordStorageFile.FileType);
playbackElement1.Position = new TimeSpan(0, 0, 0, 0, 5000);
playbackElement1.Play();
5秒より長いビデオをチェックしているため、機能しません。
現在、私は自分のコードでこれを行っています、
playbackElement1.AutoPlay = true;
playbackElement1.SetSource(stream, this.m_recordStorageFile.FileType);
playbackElement1.Position = new TimeSpan(0, 0, 0, 0, 5000);
playbackElement1.Play();
5秒より長いビデオをチェックしているため、機能しません。
2つの問題があります。まず、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);
ロードしたら、NaturalDurationプロパティを使用してメディアの長さを確認し、必要に応じてHasTimeSpanプロパティとTimeSpanプロパティを使用してメディアをTimeSpanに変換します。
一言で言えば、他のビデオプレーヤーと同じように、MediaElementの下にスライダーを追加する必要があります...-スライダーはプレーヤーの現在の位置を更新します-タイマーは300ミリ秒ごとに現在のプレーヤーの位置でスライダーを更新します
コード:
<Slider x:Name="videoSlider" Thumb.DragStarted="videoSlider_DragStarted" Thumb.DragCompleted="videoSlider_DragCompleted" Margin="10,0,10,30" Height="18" VerticalAlignment="Bottom"/>
<MediaElement x:Name="videoPlayer" Margin="4,59,152,53" Volume=".5" MediaOpened="videoPlayer_MediaOpened">
[これはスライダー付きのメディア要素の写真です][1]
これで、メディア要素とスライダーができました...次へ:C#コード:)
public partial class MainWindow : Window
{
bool isSeekingMedia = false;
DispatcherTimer seeker; // the timer to update the Slider
public MainWindow()
{
InitializeComponent();
player = new MediaPLayer();
IsPlaying(false);
seeker = new DispatcherTimer();
seeker.Interval = TimeSpan.FromMilliseconds(200);
seeker.Tick += Seeker_Tick;
}
///Seeker_Tick will update the Slider position while the video is playing
private void Seeker_Tick(object sender, EventArgs e)
{
try
{
MediatimeCounter.Content = String.Format("{0:hh}:{0:mm}:{0:ss}/{1:hh}:{1:mm}:{1:ss}", videoPlayer.Position, videoPlayer.NaturalDuration.TimeSpan);
if (!isSeekingMedia)
{
videoSlider.Value = videoPlayer.Position.TotalSeconds;
}
}
catch (Exception ex) { }
}
//This code is going to set Seeking to true to avoid playing the video if the user is changing the slider position... it kinda causes a performance issue.. so it's better to update the video position once the slider dragging event is completed.
private void videoSlider_DragStarted(object sender, RoutedEventArgs e)
{
isSeekingMedia = true;
}
//and this code is to update the video position based on the slider value.
private void videoSlider_DragCompleted(object sender, RoutedEventArgs e)
{
if (videoPlayer.Source != null)
{
isSeekingMedia = false;
this.videoPlayer = player.SeekVideo(videoPlayer, videoSlider.Value);
}
}
