我正在用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/