Answered by:
unable to play background music

Question
-
i am trying to make a media player app, i had implemented the code needed to play background music but when i minimize the app the audio stop's automatically but still the song is running in the background(i can't hear the audio but the track is still playing), is there anything wrong with my code?, the below is my code
Mainpage.xaml
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.Column="0" Orientation="Vertical" VerticalAlignment="Center">
<Button Content="play" Click="playit"></Button>
<Button Content="pause" Click="pauseit"></Button>
<Button Content="stop" Click="stopit"></Button>
<Button Content="pick" Click="openfile"></Button>
<Button Content="forward" Click="skip"></Button>
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="0" Orientation="Vertical" VerticalAlignment="Center">
<MediaElement x:Name="mymedia" Stretch="Uniform" AreTransportControlsEnabled="True" AudioCategory="BackgroundCapableMedia" CurrentStateChanged="MusicPlayer_CurrentStateChanged"></MediaElement>
</StackPanel>
</Grid>Mainpage.xaml.cs
namespace mediaaa
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
//attempt
SystemMediaTransportControls systemControls;
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 SystemControls_ButtonPressed(Windows.Media.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, () =>
{
mymedia.Play();
});
}
async void PauseMedia()
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
mymedia.Pause();
});
}
void MusicPlayer_CurrentStateChanged(object sender, RoutedEventArgs e)
{
switch (mymedia.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;
}
}
//end
private void playit(object sender, RoutedEventArgs e)
{
mymedia.DefaultPlaybackRate = 1;
mymedia.Play();
}
private void pauseit(object sender, RoutedEventArgs e)
{
mymedia.Pause();
}
private void stopit(object sender, RoutedEventArgs e)
{
mymedia.Stop();
}
private async void openfile(object sender, RoutedEventArgs e)
{
var picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".wmv");
picker.FileTypeFilter.Add(".mp3");
var file = await picker.PickSingleFileAsync();
var stream = await file.OpenAsync(FileAccessMode.Read);
mymedia.SetSource(stream, file.ContentType);
mymedia.Play();
}
private void skip(object sender, RoutedEventArgs e)
{
mymedia.DefaultPlaybackRate = 3.0;
mymedia.Play();
}
}
}
Monday, January 5, 2015 6:38 AM
Answers
-
i found the mistake in my code dave, i forgot to initialize the transport controls of my app, now i initialized it & everything is working fine :)
- Marked as answer by Anne Jing Tuesday, January 6, 2015 2:14 AM
Monday, January 5, 2015 1:42 PM
All replies
-
did you add the background audio capability in the manifest? did you checked the samples?
Microsoft Certified Solutions Developer - Windows Store Apps Using C#
Monday, January 5, 2015 8:45 AM -
yes i addeed the background audio capability, can u please provide a link to download sample playing audio in background ?Monday, January 5, 2015 10:13 AM
-
here is one for windows 8: https://code.msdn.microsoft.com/windowsapps/Background-Audio-in-WinRT-344bcf4d
Microsoft Certified Solutions Developer - Windows Store Apps Using C#
Monday, January 5, 2015 10:59 AM -
i found the mistake in my code dave, i forgot to initialize the transport controls of my app, now i initialized it & everything is working fine :)
- Marked as answer by Anne Jing Tuesday, January 6, 2015 2:14 AM
Monday, January 5, 2015 1:42 PM