UWP MediaPlayerElement播放音频两次

时间:2018-06-18 18:40:32

标签: c# audio uwp windows-media-player

我正在使用UWP C#制作应用程序,我遇到的问题是MediaPlayerElement同时播放两次音频。第二音频略有延迟。如果我关闭或暂停视频,其中一个音频会停止,另一个继续播放。

player.AutoPlay = false;
player.Source = MediaSource.CreateFromUri(new Uri(link));
player.MediaPlayer.Play();

停止:

player.MediaPlayer.Dispose();

XAML:

<MediaPlayerElement x:Name="player" Margin="0" AreTransportControlsEnabled="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></MediaPlayerElement>

1 个答案:

答案 0 :(得分:0)

我正在使用此Xaml:

<UserControl
x:Class="BurningSeries.ui.VideoPlayer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Margin="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
d:DesignHeight="300"
d:DesignWidth="400">

<Grid Background="Black">
    <MediaPlayerElement x:Name="player" AreTransportControlsEnabled="True"></MediaPlayerElement>
</Grid>

和此代码:

public sealed partial class VideoPlayer : UserControl
{
    public VideoPlayer()
    {
        this.InitializeComponent();
    }

    public void setSourc(string link)
    {
        player.AutoPlay = false;
        player.Source = MediaSource.CreateFromUri(new Uri(link));
        player.MediaPlayer.Play();
    }

    public async void pause()
    {
        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            player.MediaPlayer.Dispose();
        });
    }

    public double Duration()
    {
        double result = 0;
        try
        {
            result = player.MediaPlayer.NaturalDuration.TotalSeconds;
        }
        catch { }
        return result;
    }

    public int Time()
    {
        int result = 0;
        try
        {
            result = player.MediaPlayer.Position.Seconds;
        }
        catch { }
        return result;
    }
}