2016-03-03 93 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