开发者

MP3 does not trigger the Storyboard.Completed event

开发者 https://www.devze.com 2023-02-17 23:31 出处:网络
I am trying to use the Storyboard\'s Completed event to indicate when an audio file has finished playing.

I am trying to use the Storyboard's Completed event to indicate when an audio file has finished playing. However, it seems .Net behaves differently when playing mp3 and wav files, and the event is triggered only for WAV.

Here is a XAML and code that demonstrate the problem:

<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)开发者_StackOverflow社区
    {
        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();
    }
}

}

Will be glad if anyone has an idea for the reason for that and how to overcome it.


You don't have an attached event handler.

You need a

sb.Completed +=Storyboard_Completed;

before


The WPF MediaKit includes a MediaElement replacement, which may work better with mp3 files.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号