我正在尝试使用Storyboard的Completed事件来指示音频文件何时播放完毕。 但是,看起来.Net在播放mp3和wav文件时的行为有所不同,而且该事件仅针对WAV触发。
这是一个XAML和代码,用于演示此问题:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
x:Name="mainWind">
<Window.Resources>
<Storyboard x:Key="mp3StoryBoard" Completed="Storyboard_Completed">
<MediaTimeline Source="001.mp3" Storyboard.TargetName="mediaEl" >
</MediaTimeline>
</Storyboard>
<Storyboard x:Key="wavStoryBoard" Completed="Storyboard_Completed">
<MediaTimeline Source="001.wav" Storyboard.TargetName="mediaEl" >
</MediaTimeline>
</Storyboard>
</Window.Resources>
<Grid>
<MediaElement x:Name="mediaEl" Height="120" HorizontalAlignment="Left" Margin="120,94,0,0" VerticalAlignment="Top" Width="160" />
<Button Content="Play MP3" Height="23" HorizontalAlignment="Left" Margin="148,260,0,0" Name="btnMP3" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Button Content="Play WAV" Height="23" HorizontalAlignment="Left" Margin="267,260,0,0" Name="btnWAV" VerticalAlignment="Top" Width="75" Click="btnWAV_Click" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Storyboard sb = (Storyboard)this.mainWind.Resources["mp3StoryBoard"];
sb.Begin();
}
private void Storyboard_Completed(object sender, EventArgs e)
{
MessageBox.Show(" Storyboard finished");
}
private void btnWAV_Click(object sender, RoutedEventArgs e)
{
Storyboard sb = (Storyboard)this.mainWind.Resources["wavStoryBoard"];
sb.Begin();
}
}
}
如果有人知道其原因以及如何克服它,那将会很高兴。
答案 0 :(得分:2)
您没有附加的事件处理程序。
你需要一个
sb.Completed +=Storyboard_Completed;
之前
答案 1 :(得分:0)
WPF MediaKit包含一个MediaElement替换,可以更好地处理mp3文件。