我正在制作可在后台播放音频流的Windows 8.1应用。但它不会在后台播放音频流,例如,如果应用程序最小化。当应用程序处于焦点时,它可以正常播放音频流。我希望有人能提供帮助。感谢。
MainPage.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)
{
}
}
答案 0 :(得分:0)
您是否已声明&#34;音频 - 后台任务&#34;在您的应用清单中?
鲁
答案 1 :(得分:0)
不确定您是否曾找到并回答此问题,但查看您的代码时,您似乎没有调用InitializeTransportControls()方法。