2012-03-12 3 views
0

サービスを拡張するメディアプレーヤークラスの停止に問題があります。私はstopをうまく呼び出すことができますが、stopService(Intent)を使用しようとすると強制終了します。私はコンテキストに関連するログにnullpointer例外を取得します。私はこれについてウェブ上のドキュメントを見つけることができません。ここで私が試したコードの一部です:OnAudioFocusChangeListenerのサービスを停止する場合= AudioManager.AUDIOFOCUS_LOSS

static OnAudioFocusChangeListener audioFocusChangeListener = new OnAudioFocusChangeListener() 
{ 
    public void onAudioFocusChange(int focusChange) 
    { 
     if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) 
     { 
      mp.setVolume(0.01f, 0.01f); 
     } 
     else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) 
     { 
      mp.setVolume(1.0f, 1.0f); 
     } 
     else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) 
     { 

       //stop playback and release everything---------------still need to confirm this is working 
       Play playService = new Play(); 
       playService.stopPlaying(); 
       //playService.stopAllServices(); 
       //Intent stopPlayingService = new Intent("com.joebutt.mouseworldradio.Play"); 
       //playService.getApplicationContext(); 
       //Context context = (Context) OnAudioFocusChangeListener.this; 
       Intent stopPlayingService = new Intent(playService.getBaseContext(), Play.class); 
       //stopPlayingService.setAction(Intent.); 
       //stopService(stopPlayingService); 
       playService.stopService(stopPlayingService); 


     } 
    } 
}; 

答えて

0

私は正しいコンテキストを取得できませんでした。だから、私がしたことは、BroadcastReceiverを作成し、AudioFocusListenerの内部からsendBroadcast()を使用したことです。次に、私のBroadcastReceiverのonReceive()メソッドで、stopServiceを使ってPlay.classをインテントで停止しました。今は素晴らしい作品です。多分これは将来誰かを助けるでしょう。

関連する問題