2016-03-03 117 views
1

我試圖在Android中使用服務播放音樂。我正在努力的是停止服務和音樂。這裏是我的服務:停止音樂和服務

MediaPlayer mediaPlayer; 

public SoundService() { 
    super("SoundService"); 
} 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO: Return the communication channel to the service. 
    throw new UnsupportedOperationException("Not yet implemented"); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 

} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.zenenegy); 
} 


@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    mediaPlayer.start(); 
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
     @Override 
     public void onCompletion(MediaPlayer mp) { 
      mp.release(); 
     } 
    }); 
    return START_STICKY; 
} 

public void stopp(){ 
    Log.d("SERVICE","STOP"); 
    stopSelf(); 
} 

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

當我稱之爲「STOPP」的方法,我得到了日誌,以便服務應該已經停止,但音樂繼續。我不知道如何停止音樂,因爲當我試圖使用mediPlayer.stop();方法我總是收到錯誤消息,所以如果我可以隨時停止服務,那就太好了。下面是活動的代碼:

public Button zene; 
public boolean hang = true; 
SoundService soundService; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_fomenu); 

    if(hang){ 
     startService(); 
    } 
} 

@Override 
protected void onResume() { 
    zene = (Button)findViewById(R.id.zene); 

    zene.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
       if(hang){ 
        soundService.stopp(); 
        //soundService.onDestroy(); 
       } 
      hang = !hang; 
     } 
    }); 
    super.onResume(); 
} 

public void startService(){ 
    startService(new Intent(getBaseContext(), SoundService.class)); 
} 

public void stopService(){ 
    stopService(new Intent(getBaseContext(),SoundService.class)); 
} 

}

我試着使用stopService();方法,但它不起作用。如果有人有一個想法如何停止服務中的音樂,請回復。

+0

做你嘗試調用mediaplayer.stop()和的OnDestroy –

+0

是釋放並沒有奏效。 –

+0

嘗試傳遞此mediaplayer oncreate而不是getapplicationcontext() –

回答

1

SoundService.java

public void stop(){ 
    Log.d("SERVICE","STOP"); 
    mp.stop(); 
    //release the media player if you want to 
    stopSelf(); 
} 

停止是使用Bound Services或在您的onStartCommand認識的意圖動作,然後調用一些方法stop()

這將是這個樣子

InYourActivity.java

public void stopService(){ 
    Intent stopIntent = new Intent(getBaseContext(),SoundService.class); 
    intent.setAction(SoundService.ACTION_STOP); 
    stopService(stopIntent); 
} 

SoundService.java

public static final String ACTION_STOP = "stop_music_service"; 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    if(intent.getAction()!=null && intent.getAction().equals(ACTION_STOP)){ 
     stop(); 
    } 
    mediaPlayer.start(); 
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
     @Override 
     public void onCompletion(MediaPlayer mp) { 
      mp.release(); 
     } 
    }); 
    return START_STICKY; 
} 
+0

不幸的是,沒有爲我工作。 –

+0

你能編輯你的問題來放置你的綁定代碼嗎? 'soundService.stopp();'soundService是如何初始化/分配的? – gaara87

+0

對不起,但我不知道什麼是綁定代碼。我在代碼的頂部初始化了soundService對象。 –

0

你需要調用stop方法爲MediaPlayer的實例已實例化的對象。在你的情況下,你的MediaPlayer對象被稱爲mediaPlayer。

下面是一個例子:

public void stopMusic(){ 
if (mediaPlayer != null && mediaPlayer.isPlaying()){ 
    mediaPlayer.stop(); 
    mediaPlayer.destroy(); 
} 
} 
+0

不幸的是它不工作。 –

+0

我注意到至少有兩個單獨的MediaPlayer對象實例化,一個名爲mp,另一個名爲mediaPlayer。您可能應該只創建一個MediaPlayer對象。這可能是問題的一部分。 – joshgoldeneagle

+0

@joshgoldeneagle mp對象引用了偵聽器返回的同一個mediaplayer對象。所以它安全地在那裏。 – gaara87