Wpf MP3不会触发情节提要。已完成事件
我试图使用故事板的已完成事件来指示音频文件何时已完成播放。 但是,在播放mp3和wav文件时,.Net的行为似乎有所不同,并且该事件仅针对wav触发 以下是演示问题的XAML和代码:Wpf MP3不会触发情节提要。已完成事件,wpf,events,xaml,mp3,wav,Wpf,Events,Xaml,Mp3,Wav,我试图使用故事板的已完成事件来指示音频文件何时已完成播放。 但是,在播放mp3和wav文件时,.Net的行为似乎有所不同,并且该事件仅针对wav触发 以下是演示问题的XAML和代码: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.co
<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();
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Media.Animation;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApplication1
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
故事板sb=(故事板)this.mainWind.Resources[“mp3StoryBoard”];
某人开始做某事;
}
私有void情节提要_已完成(对象发送方,事件参数e)
{
MessageBox.Show(“故事板完成”);
}
私有无效btnWAV_单击(对象发送者,路由目标e)
{
故事板sb=(故事板)this.mainWind.Resources[“wavStoryBoard”];
某人开始做某事;
}
}
}
如果有人知道原因和克服方法,我会很高兴的。包括MediaElement替代品,它可以更好地处理mp3文件。您没有附带的事件处理程序 你需要一个
sb.Completed +=Storyboard_Completed;
以前