WPF的MediaPlayer:如何按顺序播放,同步? [英] WPF MediaPlayer: How to play in sequence, sync?
本文介绍了WPF的MediaPlayer:如何按顺序播放,同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个功能:
公共静态无效播放(字符串文件名,布尔异步= FALSE)
{
System.Windows.Media.MediaPlayer MP =新System.Windows.Media.MediaPlayer();
mp.Open(FileName.ToUri());
mp.Play();
}
当我打电话
播放(@file1.mp3);
播放(@file2.mp3);
播放(@file3.mp3);
播放(@file4.mp3);
所有的人在同一时间玩了。
我怎样才能让MediaPlayer的等待文件的末尾,玩下? ?函数应该喜欢什么
编辑:
公共静态无效播放(URI文件名,布尔异步= FALSE)
{
的AutoResetEvent一个=新的AutoResetEvent(假);
MediaPlayer的熔点为新的MediaPlayer();
mp.MediaEnded + =(01,P1)=>
{
a.Set();
};
mp.MediaOpened + =(O,P)=>
{
INT总= Convert.ToInt32(mp.NaturalDuration.TimeSpan.TotalMilliseconds);
mp.Play();
如果(!异步)
{
//System.Threading.Thread.Sleep(total);
a.WaitOne();
}
};
mp.Open(文件名);
}
解决方案
我将分享我的解决方案与你:
我创建了一个窗口:WindowPlay.xaml
< ;窗口x:类=Sistema.Util.WindowPlay
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =HTTP:/ /schemas.microsoft.com/winfx/2006/xaml
标题=WindowPlay
AllowsTransparency =真
=背景透明
BorderBrush =透明
了borderThickness =0
ResizeMode =NoResize
WindowStyle =无
顶部=0左=0
宽度=16身高=16
ShowActivated =假
ShowInTaskbar =假
个
<网格和GT;
< MediaElement的名称=MediaElement1LoadedBehavior =播放UnloadedBehavior =关闭
MediaEnded =MediaElement1_MediaEnded/>
< /网格和GT;
< /窗GT;
WindowPlay.xaml.cs:
使用系统;使用System.Windows
;
命名空间Sistema.Util
{
///<总结> $ B $为WindowPlay.xaml
///< b ///交互逻辑; /总结>
公共部分类WindowPlay:窗口
{
公共WindowPlay()
{
试
{
的InitializeComponent();
}
抓
{
this.Close();
}
}
///<总结>
///播放指定的文件名
///< /总结>
///< PARAM NAME =文件名>至播放的文件名< /参数>
///< PARAM NAME =异步>如果在后台玩真并返回immediatelly控件调用代码。如果为False,玩,等到完成对控制返回给调用代码< /参数>
公共静态无效播放(URI文件名,布尔异步= FALSE)
{
WindowPlay W =新WindowPlay();
VAR MP = w.MediaElement1;
如果(MP == NULL)
{
//颇得ESTAR仙道fechada一个janela
的回报;
}
mp.Source =(文件名);
如果(异步)
w.Show();
,否则
w.ShowDialog();
}
私人无效MediaElement1_MediaEnded(对象发件人,RoutedEventArgs E)
{
this.Close();
}
}
}
I have this function:
public static void Play(string FileName, bool Async = false)
{
System.Windows.Media.MediaPlayer mp = new System.Windows.Media.MediaPlayer();
mp.Open(FileName.ToUri());
mp.Play();
}
When i call
Play(@"file1.mp3");
Play(@"file2.mp3");
Play(@"file3.mp3");
Play(@"file4.mp3");
all them play at same time.
How can i make MediaPlayer wait the end of the file, to play the next? What the function should like?
EDIT:
public static void Play(Uri FileName, bool Async = false)
{
AutoResetEvent a = new AutoResetEvent(false);
MediaPlayer mp = new MediaPlayer();
mp.MediaEnded += (o1, p1) =>
{
a.Set();
};
mp.MediaOpened += (o, p) =>
{
int total = Convert.ToInt32(mp.NaturalDuration.TimeSpan.TotalMilliseconds);
mp.Play();
if (!Async)
{
//System.Threading.Thread.Sleep(total);
a.WaitOne();
}
};
mp.Open(FileName);
}
解决方案
I will share my Solution with you:
I created a Window: WindowPlay.xaml
<Window x:Class="Sistema.Util.WindowPlay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowPlay"
AllowsTransparency="True"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="0"
ResizeMode="NoResize"
WindowStyle="None"
Top="0" Left="0"
Width="16" Height="16"
ShowActivated="False"
ShowInTaskbar="False"
>
<Grid>
<MediaElement Name="MediaElement1" LoadedBehavior="Play" UnloadedBehavior="Close"
MediaEnded="MediaElement1_MediaEnded" />
</Grid>
</Window>
WindowPlay.xaml.cs:
using System;
using System.Windows;
namespace Sistema.Util
{
/// <summary>
/// Interaction logic for WindowPlay.xaml
/// </summary>
public partial class WindowPlay : Window
{
public WindowPlay()
{
try
{
InitializeComponent();
}
catch
{
this.Close();
}
}
/// <summary>
/// Plays the specified file name
/// </summary>
/// <param name="FileName">The filename to play</param>
/// <param name="Async">If True play in background and return immediatelly the control to the calling code. If False, Play and wait until finish to return control to calling code.</param>
public static void Play(Uri FileName, bool Async = false)
{
WindowPlay w = new WindowPlay();
var mp = w.MediaElement1;
if (mp == null)
{
// pode estar sendo fechada a janela
return;
}
mp.Source = (FileName);
if (Async)
w.Show();
else
w.ShowDialog();
}
private void MediaElement1_MediaEnded(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
这篇关于WPF的MediaPlayer:如何按顺序播放,同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文