2012-07-15 4 views
0

을 호출하면 Android MusicServices의 MediaPlay가 충돌합니다. 우리 앱에는 MusicService가 있습니다. 현재 음악 재생 및 중지에있어 완벽하게 작동하지만 이는 서비스 시작 및 중지에서 비롯됩니다. 다른 방법으로 호출하고 있지만 null checker로 둘러싸여 있지 않으면 mp.pause()이 충돌합니다. 그러나 null을 확인하면 전혀 작동하지 않습니다. 우리는 이전에이 모든 작업을 했었지만, 우리는 그것을 수행하는 방식을 재구성하기 시작했습니다. 왜냐하면 Android 4.0 (ICS)에서 음악은 우리가 멈추었을 때도 무작위로 계속 연주 했었지만 어쨌든 요점은 아닙니다.<MediaPlayer> .pause()

public class MusicService extends Service { 
    public static MediaPlayer mp; 

    @Override 
    public IBinder onBind(final Intent arg0) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mp = MediaPlayer.create(this, R.raw.title_music); 
     mp.setLooping(true); 
     mp.setVolume(200, 200); 
    } 

    @Override 
    public int onStartCommand(final Intent intent, final int flags, final int startId) { 
     mp.start(); 
     return 1; 
    } 

    @Override 
    public void onStart(final Intent intent, final int startId) { 

    } 

    public IBinder onUnBind(final Intent arg0) { 
     return null; 
    } 

    public static void onStop() { 
     mp.stop(); 
     mp.release(); 
    } 

    public static void onPause() { 
     if (mp!=null) { 
      mp.pause(); 
     } 
    } 

    public static void onResume() { 
     if (mp!=null) { 
      mp.start(); 
     } 
    } 


    @Override 
    public void onDestroy() { 
      mp.stop(); 
      mp.release(); 
      super.onDestroy(); 
    } 

    @Override 
    public void onLowMemory() { 
     mp.stop(); 
     mp.release(); 
    } 

} 

우리는 다른 활동에서 서비스를 시작하려면이 옵션을 사용 :

intent = new Intent(this, MusicService.class); 
    startService(intent); 

답변

0

로그없이 정말 이것이 문제가 100 % 확실하게 말할 수를하지만 그를 나타납니다 mp가 유효하지 않은 상태에서 pause 메서드가 호출되고 있습니다. onPause 메서드를 변경하여 그렇게 읽도록 제안합니다.

public static void onPause() { 
    if (mp!=null) { 
     if (mp.isPlaying()) 
      mp.pause(); 
    } 
} 

실제로 재생 중인지 확인하고 오류 상태를 제외한 모든 상태에서 작동합니다. 적어도 문서에 따르면.

관련 문제