我有 MP3 播放服务,它有自己的类并使用 Mediaplayer 并与 HTTP 连接。它必须播放在上一个活动中选择的 URL 之一,我将其传递给 PlayerActivity。
我在以下方式创建服务PlayerActivity onCreate
:
startService(new Intent(this, PlayerService.class));
Intent connectionIntent = new Intent(this, PlayerService.class);
bindService(connectionIntent, mp3PlayerServiceConnection, Context.BIND_AUTO_CREATE);
这是第一个选定的 URL 启动。我在新线程中启动 Mediaplayer 调用不阻塞 UI(调用ActivityPlayer
本身):
private ServiceConnection mp3PlayerServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName arg0, IBinder binder) {
mp3Service = ((LocalBinder) binder).getService();
Thread t = new Thread() {
public void run() {
mp3Service.playSong(getApplicationContext(),url);
}
};
t.start();
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
};
问题是如何将一个新的 URL 传递给这个服务的线程,当用户销毁这个 Activity 时,进入菜单并选择新的 URL。新的流必须在同一个线程中播放,但我遇到了使用后退按钮返回主页并再次启动应用程序的情况,我同时播放了 2 个 URL。也许是新Thread()
声明的原因。
那么,当Activity用一个URL创建时,如何将它的URL权传递给Service的线程,所以如果是旧的URL,什么都不会发生,如果是新的,播放器会切换到新的URL,但不能同时播放2个流?