3

Silverlight アプリに、TestAudioという名前の可変MediaElement変数があります。

ボタンをクリックすると、オーディオが正しく再生されます。

しかし、ボタンをもう一度クリックすると、オーディオが再生されません。

MediaElement をもう一度再生するにはどうすればよいですか?

位置を 0 に戻すための以下の試行はどれも機能しませんでした:

private void Button_Click_PlayTest(object sender, RoutedEventArgs e)
{
    //TestAudio.Position = new TimeSpan(0, 0, 0);
    //TestAudio.Position = TestAudio.Position.Add(new TimeSpan(0, 0, 0));
    //TestAudio.Position = new TimeSpan(0, 0, 0, 0, 0);
    //TestAudio.Position = TimeSpan.Zero;

    TestAudio.Play();
}
4

3 に答える 3

9

私はそれを見つけました。最初にオーディオを停止してから、位置を設定する必要があります。

TestAudio.Stop();
TestAudio.Position = TimeSpan.Zero;
于 2010-03-22T03:41:56.433 に答える
3

MediaElement には、ループ再生のサポートが組み込まれていません。MediaEnded イベントを使用してメディアの位置をゼロに設定するか、Stop() メソッドを呼び出すことができます。どちらも、再生用のビデオ/オーディオの先頭に配置されます。

 public partial class MainPage : UserControl
{
    public MainPage()
    {
        // Required to initialize variables
        InitializeComponent();
        // Used for loopback.
        MyME.MediaEnded += new RoutedEventHandler(MyME_MediaEnded);
    }

    // MediaElement has no looping capabilities so need to set the position back
    // to the begining after the video finishes in order to play again.
    // Or you can use the stop method
    void MyME_MediaEnded(object sender, RoutedEventArgs e)
    {
        //MyME.Position = TimeSpan.Zero;
        MyME.Stop();
    }

    private void BtnPlay_Click(object sender, RoutedEventArgs e)
    {

        MyME.Play();
    }

    private void BtnPause_Click(object sender, RoutedEventArgs e)
    {
        MyME.Pause();
    }

    private void BtnStop_Click(object sender, RoutedEventArgs e)
    {
        MyME.Stop();
    }   

}
于 2011-01-25T22:20:42.930 に答える
2

I found the above did not work for me, and the only way I could get it working was to create a mediaelement dynamically. Heres the code I used - I copied the values from a MediaElement named mePlayClick I initially put in the XAML but you may not need to do this.

    private void Play_MediaSound()
    {
        // Create new media element dynamically
        MediaElement mediaElement = new MediaElement();

        // Reuse settings in XAML 
        mediaElement.Volume = mePlayClick.Volume;
        mediaElement.Source = mePlayClick.Source;
        mediaElement.AutoPlay = mePlayClick.AutoPlay;

        // WHen the media ends, remove the media element
        mediaElement.MediaEnded += (sender, args) =>
        {
            LayoutRoot.Children.Remove(mediaElement);
            mediaElement = null;
        };

        // Add the media element, must be in visual ui tree
        LayoutRoot.Children.Add(mediaElement);

        // When opened, play
        mediaElement.MediaOpened += (sender, args) =>
        {
            mediaElement.Play();
        };
    }
于 2010-04-02T07:11:46.937 に答える