c# - UWP MediaPlayerElement播放音频两次

标签 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>

最佳答案

我正在使用此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;
    }
}

关于c# - UWP MediaPlayerElement播放音频两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50915622/

相关文章:

c# - Automapper、Mapper 未初始化。使用正确的配置调用初始化

c# - 如何在不复制的情况下从 C# 中的现有数组获取子数组?

java - 在java中改变音频速度

c++ - 使用Qt录制系统声音

c# - 如何将 mask 应用于 CompositionBrush

c# - 网站引用了一个使用 NHibernate 与数据库对话的控制台应用程序

c# - 无法修改存储在另一个属性中的结构的属性

audio - 从 mp3 创建 HLS 可流式音频文件

c# - CalendarDatePicker 绑定(bind)空值

c# - UWP MediaCapture-录制WAV音频,采样率16000,16位,单声道