WPF MediaElement 的奇怪行为
我目前正在使用 MediaElement 播放各种不同的文件,并且我似乎可以正常使用其中的大部分文件。
我注意到的一件事是音频文件(在本例中具体为 mp3)在第一次尝试时拒绝播放。有时您可以听到一毫秒(非常没有吸引力)的声音。更像是一个昙花一现,然后就什么也没有了。任何随后加载音乐的尝试都很好,很奇怪。视频将在第一次尝试时播放,流媒体也是如此。这似乎只适用于本地音频文件。
启动音频和视频文件的代码几乎相同。
private void lvVideos_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var depObj = e.OriginalSource as DependencyObject;
if (depObj != null)
{
var parent = depObj.FindVisualAncestor<ListViewItem>();
if (parent != null && lvVideos.SelectedItem != null)
{
State = PlayState.Closed;
Video video = lvVideos.SelectedItem as Video;
if (video == null) return;
lblTrackName.Text = video.Title;
MediaPlayer.Source = null;
MediaPlayer.Source = new Uri(video.Location);
CurrentMedia = MediaType.Video;
State = PlayState.Playing;
}
}
}
private void lvMusic_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var depObj = e.OriginalSource as DependencyObject;
if (depObj != null)
{
var parent = depObj.FindVisualAncestor<ListViewItem>();
if (parent != null && lvMusic.SelectedItem != null)
{
State = PlayState.Closed;
Music song = lvMusic.SelectedItem as Music;
if (song == null) return;
lblTrackName.Text = song.Title;
MediaPlayer.Source = null;
MediaPlayer.Source = new Uri(song.Location);
CurrentMedia = MediaType.Music;
State = PlayState.Playing;
}
}
}
正如您所看到的,我尝试在加载音频之前将源属性清空,但没有成功。我设法想出了一个肮脏的解决方法。其中涉及将源设置为一个注定会失败的文件(应用程序的 .exe)并在应用程序初始化时播放它。这允许加载的第一个音乐文件正确播放。
以前有其他人遇到过这个吗?有什么解决办法吗?
编辑:天哪,我觉得很愚蠢。显然罪魁祸首是 mediaElement.ScrubbingEnabled = true;哪个(根据文档)是一个看似有用的选项,也许它应该只针对远程流启用?
I am current using MediaElement to play a variety of different files and I seem to have most of it working.
One thing I noticed is that Audio files (in this case mp3's specifically) refuse to play on the first attempt. Sometimes you can hear a millisecond (very unattractive) worth of sound. More like a blip and then nothing. Any subsequent attempt to load music works just fine, odd. Videos will play on the first attempt, and so will streamed media. This seems to only apply to local audio files.
The code that starts both audio and video files are pretty much identical.
private void lvVideos_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var depObj = e.OriginalSource as DependencyObject;
if (depObj != null)
{
var parent = depObj.FindVisualAncestor<ListViewItem>();
if (parent != null && lvVideos.SelectedItem != null)
{
State = PlayState.Closed;
Video video = lvVideos.SelectedItem as Video;
if (video == null) return;
lblTrackName.Text = video.Title;
MediaPlayer.Source = null;
MediaPlayer.Source = new Uri(video.Location);
CurrentMedia = MediaType.Video;
State = PlayState.Playing;
}
}
}
private void lvMusic_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var depObj = e.OriginalSource as DependencyObject;
if (depObj != null)
{
var parent = depObj.FindVisualAncestor<ListViewItem>();
if (parent != null && lvMusic.SelectedItem != null)
{
State = PlayState.Closed;
Music song = lvMusic.SelectedItem as Music;
if (song == null) return;
lblTrackName.Text = song.Title;
MediaPlayer.Source = null;
MediaPlayer.Source = new Uri(song.Location);
CurrentMedia = MediaType.Music;
State = PlayState.Playing;
}
}
}
As you can see I attempted to null the source property prior to loading the audio to no avail. I have managed to come up with a dirty hack of a workaround. Which involved setting the source to a file that is gaurenteed to fail (the app's .exe) and playing it as the app initialized. This allows for the first music file loaded to play properly.
Has anybody else come across this before? and are there any fixes?
EDIT: omg I feel stupid. apparently the culprit was mediaElement.ScrubbingEnabled = true; which (by the documentation) is a seemingly useful option, perhaps it should only be enabled for remote streams?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然罪魁祸首是 mediaElement.ScrubbingEnabled = true;哪个(根据文档)是一个看似有用的选项,也许它应该只针对远程流启用?
Apparently the culprit was mediaElement.ScrubbingEnabled = true; which (by the documentation) is a seemingly useful option, perhaps it should only be enabled for remote streams?