gpt4 book ai didi

c# - Windows 8.1 音频流不在后台播放

转载 作者:行者123 更新时间:2023-11-30 18:29:48 24 4
gpt4 key购买 nike

我正在制作一个可以在后台播放音频流的 Windows 8.1 应用程序。但它不会在后台播放音频流,例如,如果应用程序被最小化。当应用程序处于焦点时,它可以很好地播放音频流。我希望有人可以提供帮助。谢谢。

主页.xaml:

<Grid Name="mainGrid" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<TextBlock TextWrapping="Wrap" Text="Radio Limfjord" VerticalAlignment="Top" Height="116" FontSize="100" Margin="360,10,360,0"/>
<MediaElement Visibility="Visible" Name="mediaplayer" AudioCategory="BackgroundCapableMedia" AutoPlay="True" RealTimePlayback="True" AudioDeviceType="Multimedia" HorizontalAlignment="Right" Width="1218" Margin="0,366,63,177" AreTransportControlsEnabled="True" CurrentStateChanged="mediaplayer_CurrentStateChanged" />
<Button Name="cmdPlay" Content="Afspil" Margin="566,627,0,41" Click="cmdPlay_Click" Width="100" Height="100"/>
<Button Name="cmdStop" Content="Stop" Margin="714,626,0,41" Click="cmdStop_Click" Width="100" Height="100"/>
</Grid>

MainPage.xaml.cs:

public sealed partial class MainPage : Page
{
SystemMediaTransportControls systemControls;
public MainPage()
{
this.InitializeComponent();

}

void InitializeTransportControls()
{
// Hook up app to system transport controls.
systemControls = SystemMediaTransportControls.GetForCurrentView();
systemControls.ButtonPressed += SystemControls_ButtonPressed;

// Register to handle the following system transpot control buttons.
systemControls.IsPlayEnabled = true;
systemControls.IsPauseEnabled = true;
}

void MusicPlayer_CurrentStateChanged(object sender, RoutedEventArgs e)
{
switch (mediaplayer.CurrentState)
{
case MediaElementState.Playing:
systemControls.PlaybackStatus = MediaPlaybackStatus.Playing;
break;
case MediaElementState.Paused:
systemControls.PlaybackStatus = MediaPlaybackStatus.Paused;
break;
case MediaElementState.Stopped:
systemControls.PlaybackStatus = MediaPlaybackStatus.Stopped;
break;
case MediaElementState.Closed:
systemControls.PlaybackStatus = MediaPlaybackStatus.Closed;
break;
default:
break;
}
}

void SystemControls_ButtonPressed(SystemMediaTransportControls sender,
SystemMediaTransportControlsButtonPressedEventArgs args)
{
switch (args.Button)
{
case SystemMediaTransportControlsButton.Play:
PlayMedia();
break;
case SystemMediaTransportControlsButton.Pause:
PauseMedia();
break;
default:
break;
}
}

async void PlayMedia()
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
mediaplayer.Play();
});
}

async void PauseMedia()
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
mediaplayer.Pause();
});
}

private void cmdPlay_Click(object sender, RoutedEventArgs e)
{
mediaplayer.Source = new Uri("http://media.wlmm.dk/limfjord", UriKind.Absolute);
mediaplayer.Play();
}

private void cmdStop_Click(object sender, RoutedEventArgs e)
{
mediaplayer.Stop();

}

private void mediaplayer_CurrentStateChanged(object sender, RoutedEventArgs e)
{

}
}

最佳答案

您是否在应用 list 中声明了“音频 - 后台任务”?

飞行棋

更多:http://blogs.msdn.com/b/johnkenn/archive/2013/12/31/supporting-background-audio-in-your-windows-8-1-app.aspx

关于c# - Windows 8.1 音频流不在后台播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22451445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com