简体   繁体   中英

UWP MediaPlayerElement plays audio twice

Im am making an app in UWP C# and I'am having the problem that the MediaPlayerElement plays audio twice at the same time. Second audio with slight delay. If i close or pause the video one of the audios stops and the other continues to play.

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

to stop:

player.MediaPlayer.Dispose();

xaml:

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

I am using this 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>

and this code:

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM