Mangoã§BackgroundTasksã£ã¦ã®ããµãã¼ãããã¦ããã®ä¸ã«BackgroundAudioã£ã¦ã®ããã£ã¦ããã使ãã¨ãæ軽ã«ãªã¼ãã£ãªãã¬ã¤ã¤ã¼ã¢ããªãä½ãã¡ãããã¿ããªãããã§ã¢ããªã¤ãã£ã¦ãã©ã¤ãã¼ãã¯ã©ã¦ããæå¹æ´»ç¨ããããï¼ã¨ããã話ã§ãã
é·ãã®ã§ç¶ãã§ã©ããã
ãªã¼ãã£ãªåçã¨ã¼ã¸ã§ã³ãã追å ãã
ã½ãªã¥ã¼ã·ã§ã³ã¨ã¯ã¹ããã¼ã©ã®ã½ãªã¥ã¼ã·ã§ã³ãå³ã¯ãªãã¯ã追å (D)|æ°ããããã¸ã§ã¯ã(N)
Windows Phone ãªã¼ãã£ãªåçã¨ã¼ã¸ã§ã³ã ãé¸æãã¦ããã¸ã§ã¯ãã«è¿½å
ããã¾ã§ã§ã½ãªã¥ã¼ã·ã§ã³æ§æã¯ãããªã£ã¦ããã¯ã
æå¾ã«ã¡ã¤ã³ã®ã¢ããªã¸AudioPlaybackAgent1ã®åç
§ã追å ãã
PhoneApp4ã®åç
§è¨å®ãå³ã¯ãªãã¯ãåç
§ã®è¿½å (R)
ããã¸ã§ã¯ã ã¿ãããAudioPlaybackAgent1ãé¸æãã¦OK
ããã§BackgroundAudioãã¢ããªãã使ç¨ã§ããããã«ãªãã¾ããã
ãªã¼ãã£ãªåçã¨ã¼ã¸ã§ã³ãã使ã£ã¦ãªã¼ãã£ãªãåçãã
ã¾ãã¯ä¸çªç°¡åãªä¾ããã
PhoneApp4ã®MainPage.xamlã¸ãã¿ã³ã追å ãã¾ãã
<!-- ã¡ãã¼ã¦ãã¨ã¼ --> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Button Click="Button_Click" Content="Play" /> </Grid>
ã³ã¼ãããã¤ã³ãã«æ¬¡ã®ãæ¸ãã¾ãã
using Microsoft.Phone.BackgroundAudio;
private void Button_Click(object sender, RoutedEventArgs e) { BackgroundAudioPlayer.Instance.Track = new AudioTrack( new Uri("http://example.com/music.mp3"), "Sample Track", "Some Artist", "Unknown Album", null); BackgroundAudioPlayer.Instance.Play(); }
ãã¡ã¤ã«ã®ãã¹ã¨ãã¯åèªããæãã«åãã¦ãã ãããã¿ã¤ãã«ã¨ãã¢ã¼ãã£ã¹ãã¨ãã¯ãããªã¥ã¼ã ã³ã³ããã¼ã«ã®ã¨ããã¨ããããã¯ç»é¢ã¨ãã«åºã¾ãã
ããã¾ã§åºæ¥ããã¨ããããé³æ¥½é³´ãã¾ããå®è¡ãã¦ãã¿ã³ãæ¼ãã¨ããã¯ã°ã©ã¦ã³ãã§åçããã¾ããã¢ããªæ®ºããã£ã¦åé¡ãªãã§ãã
ãªã¼ãã£ãªåçã¨ã¼ã¸ã§ã³ããããå°ãã¡ããã¨ã³ã³ããã¼ã«ããã«ã¯
AudioPlaybackAgent1ã®AudioPlayer.csã«AudioPlayerAgentã¯ã©ã¹ãå®è£
ããã¯ã©ã¹ã®ãã³ãã¬ã¼ããè¨è¿°ãã¦ããã¾ãã
ä»ã²ã¨ã¤ããæ¥æ¬èªåããã¦ãªãã¦ããããªæããªãã§ãããããã¯æ¸ãã¨ãã¾ãããã¨ããã¨ãããããã¯ã¢ãããã¦ã
OnPlayStateChangedé¢æ°
åçç¶æ
ãå¤æ´ãããã¨ãã«å¼ã³åºãããé¢æ°ã§ãç¶æ
ã«å¿ãã¦ãããããªå¦çãåºæ¥ã¾ãã
ããããã³ãã¬ã¼ãã«ãã°ããã£ã¦ãPlayState.TrackEndedã®æã«GetPreviousTrackãå¼ã³åºãããã«ãªã£ã¦ãã¾ãããåççµãã£ãã¨ãã«ä¸ã¤åã®ãã©ãã¯å¼ã°ãã¦ãå°ãã®ã§ããããæ¸ãæãã¦
case PlayState.TrackEnded: player.Track = GetNextTrack(); break;
ã¨ããããã«å¤æ´ãã¦ããã¾ããããã»ãã®ã¤ãã³ãã¯å¿ è¦ãªãé©å®ã
OnUserActioné¢æ°
ã¦ã¼ã¶ããªã«ãæä½ããã¨ãã«å¼ã°ãã¾ããBackgroundAudioPlayer.Instance.Play() ã¨ãããã¨ãã«å¼ã°ããã®ãããã
ããã§ãåçãããã©ãã¯ãã»ããããã¦ããªãã¨ãã¯IsolatedStorageãããã¬ã¤ãªã¹ããèªã¿åºãã¦ãã¼ã¹ãããªã©ã®å¦çãããã¨ããã§ãã
GetNextTracké¢æ°
ç¾å¨åçä¸ã®ãã©ãã¯ã®æ¬¡ãåå¾ããé¢æ°ããªããããæãã«å®è£
ãã¦ãã ããã
ãã¨ãã°ãããªæãã
private AudioTrack GetNextTrack() { AudioTrack track = null; // 次ã«ã¾ã ãã©ãã¯ããããªããããè¿ã if (++trackNum < tracks.Count) track = tracks[trackNum]; return track; }
nullãè¿ãã¨åçã¯çµäºãã¾ãã
GetPreviousTracké¢æ°
GetNextTracké¢æ°ã¨åãããã«ããã£ã¡ã¯åçä¸ã®ãã©ãã¯ã®ä¸ã¤åã®ãã©ãã¯ãè¿ãã¾ãããã£ã¡ãããæãã§å®è£
ãã¦ãã ããã
ãããªæãã§ãã
private AudioTrack GetPreviousTrack() { AudioTrack track = null; // ä¸ã¤åã«ã¾ã ãã©ãã¯ããããªããããè¿ã if (--trackNum >= 0) track = tracks[trackNum]; return track; }
ä»ã«ãã¨ã©ã¼æã«å¼ã°ããé¢æ°ã¨ãããããããã¾ãããã¡ããã¨ããã¢ããªãä½ããªãã®ã§ããã°å®è£
ããªãã¦ãåé¡ç¡ãã§ãã*1ã¡ããã¨ããã¢ããªæ¸ãã¨ãã¯ã¨ã©ã¼å¦çã¨ãããæ¹ãããã§ããã©ãããã¡ã®ãµã¼ãããã¹ããªã¼ã ãã¦ãã©ã¤ãã¼ãã¯ã©ã¦ããBackgroundAudioã§æå¹æ´»ç¨ããã
ï¼ã£ã¦ããç¨éãªããããããã®å®è£
ã§ååã§ãã
ã¿ãªãããã¢ããªä½ã£ã¦ããµã¼ãå´ãå®è£
ãã¦ãå¿«é©ãªé³æ¥½ã©ã¤ããéãã¾ãããï¼
*1:æ´è¨