continue playing music when app closes
I am developing an app in wp7 and in some part.
I start a mediaelement to play my music file from isolated storage, with available functions(play, pause, next, previous, seek) and all goes perfect!
Now i want to make the app continue playing on user-exit or page-unload or something like that when mediaelement already playing!
Notice in the abobe try the
list.playlist: is a list with audiotracks num_input: is the number of music file in list that will play, (get: from mediaelement) time_input: is the timespan from mediaelement before stop, (set: backgroundaudioplayer to start) play: is boolean variable, true if mediaelement plays Stop(): is function that stops mediaelement from playing
Thats i am trying is:
private void PhoneApplicationPage_Unloaded(object sender, RoutedEventArgs e)
{
if (play == true)
{
TimeSpan time_input = med.Position;
Stop();
BackgroundAudioPlayer.Instance.Close();
BackgroundAudioPlayer.Instance.Track = list.playlist[num_input];
BackgroundAudioPlayer.Instance.Position = time_input;
if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState) { BackgroundAudioPlayer.Instance.Pause(); }
else { BackgroundAudioPlayer.Instance.Play(); }
}
}
void Instance_PlayStateChanged(object sender, EventArgs e)
{
switch (BackgroundAudioPlayer.Instance.PlayerState)
{
case PlayState.Playing:
break;
case PlayState.Paused:
case PlayState.Stopped:
break;
}
}
The problem is that when the app page-unload (not on user exit, the first problem) the backgroundaudioplayer gets the name and the EnabledControls and all staff but it isn't playing even I press the play button(so buttons not working, second problem)!
Also i want to control the list of my app beside that player(previous, next, play, pause) even the app is closed!
All the songs are located in isolated and i have the song-name and file-name in database. The audio track looks like:
AudioTrack audiotrack1 = new AudioTrack(new Uri(emp.EmployeeFile + ".mp3", UriKind.Relative), emp.EmployeeName, null, null, null, null, EnabledPlayerControls.All);
Thanks!
Implementing background audio requires more work. See this article for a walkthrough: http://msdn.microsoft.com/EN-US/library/windowsphone/develop/hh202978(v=vs.105).aspx
Beware the agent lives in another process, I'm not sure whether SqlCe supports concurrent access to the same database from different processes. IMO the best way to communicate with the background agent is the isolated storage file (eg the playlist) guarded by the named System.Threading.Mutex
.
上一篇: MediaElement拒绝循环
下一篇: 应用程序关闭时继续播放音乐