我的Android应用程序的一项活动包含显示GrooveShark播放器网页的 WebView 。当用户点击网页的播放按钮时,活动会自动启动播放曲目的默认MediaPlayer 。一切正常,问题出现在用户关闭此活动和MediaPlayer时,虽然不再播放曲目,仍然活着 ...即使应用关闭,MediaPlayer仍然活着。杀死它的唯一方法就是强迫应用程序停止......我似乎没什么大不了的,但在某些设备中,当用户在通话后挂机时,这个"僵尸"即使应用程序之前已被破坏,MediaPlayer也会突然开始播放...
这是我的活动代码的摘要:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song);
myWV = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWV.getSettings();
webSettings.setJavaScriptEnabled(true);
myWV.setWebViewClient(new MyWebViewClient());
myWV.setWebChromeClient(new WebChromeClient());
myWV.loadUrl(url);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
myWV.onPause();
myWV.pauseTimers();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
myWV.onResume();
myWV.resumeTimers();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
myWV.destroy();
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("grooveshark.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
这是第一次播放曲目
时日志显示的内容 12-03 20:52:13.184: D/webkit(23440): euler: isUrlBlocked = false
12-03 20:52:13.794: V/MediaPlayer(23440): constructor
12-03 20:52:13.804: V/MediaPlayer(23440): setListener
12-03 20:52:13.814: D/HTML5Audio(23440): setDataSource()
12-03 20:52:13.894: V/MediaPlayer(23440): setVideoSurfaceTexture
12-03 20:52:13.894: V/MediaPlayer(23440): prepareAsync
12-03 20:52:16.144: V/MediaPlayer(23440): message received msg=3, ext1=12, ext2=0
12-03 20:52:16.154: V/MediaPlayer(23440): buffering 12
12-03 20:52:16.154: V/MediaPlayer(23440): callback application
12-03 20:52:16.154: V/MediaPlayer(23440): back from callback
12-03 20:52:16.154: V/MediaPlayer(23440): message received msg=5, ext1=0, ext2=0
12-03 20:52:16.154: V/MediaPlayer(23440): New video size 0 x 0
12-03 20:52:16.154: V/MediaPlayer(23440): callback application
12-03 20:52:16.154: V/MediaPlayer(23440): back from callback
12-03 20:52:16.154: V/MediaPlayer(23440): message received msg=1, ext1=0, ext2=0
12-03 20:52:16.154: V/MediaPlayer(23440): prepared
12-03 20:52:16.154: V/MediaPlayer(23440): callback application
12-03 20:52:16.154: V/MediaPlayer(23440): back from callback
12-03 20:52:16.184: E/MediaPlayer(23440): mOnVideoSizeChangedListener is null. Failed to send MEDIA_SET_VIDEO_SIZE message.
12-03 20:52:16.194: I/MediaPlayer(23440): Don't send intent. msg.arg1 = 0, msg.arg2 = 0
12-03 20:52:16.194: I/MediaPlayer(23440): mOnPreparedListener. Send MEDIA_PREPARED message.
12-03 20:52:16.194: D/HTML5Audio(23440): onPrepared()
12-03 20:52:16.214: V/MediaPlayer(23440): getDuration
12-03 20:52:16.224: V/MediaPlayer(23440): seekTo 0
12-03 20:52:16.224: V/MediaPlayer(23440): getDuration
12-03 20:52:16.224: V/MediaPlayer(23440): message received msg=4, ext1=0, ext2=0
12-03 20:52:16.224: D/HTML5Audio(23440): play() called. mState = 2
12-03 20:52:16.224: V/MediaPlayer(23440): Received seek complete
12-03 20:52:16.224: V/MediaPlayer(23440): All seeks complete - return to regularly scheduled program
12-03 20:52:16.224: V/MediaPlayer(23440): callback application
12-03 20:52:16.224: V/MediaPlayer(23440): back from callback
12-03 20:52:16.244: V/MediaPlayer(23440): start
12-03 20:52:16.264: D/HTML5Audio(23440): play() - start()
12-03 20:52:16.284: I/MediaPlayer(23440): mOnSeekCompleteListener. Send MEDIA_SEEK_COMPLETE message.
12-03 20:52:16.464: V/MediaPlayer(23440): isPlaying: 1
12-03 20:52:16.714: V/MediaPlayer(23440): isPlaying: 1
....
一旦活动完成,最终 日志永远显示的内容
12-03 20:52:36.164: V/MediaPlayer(23440): message received msg=3, ext1=100, ext2=0
12-03 20:52:36.164: V/MediaPlayer(23440): buffering 100
12-03 20:52:36.164: V/MediaPlayer(23440): callback application
12-03 20:52:36.164: V/MediaPlayer(23440): back from callback
12-03 20:52:36.244: V/MediaPlayer(23440): isPlaying: 0
12-03 20:52:36.494: V/MediaPlayer(23440): isPlaying: 0
12-03 20:52:36.744: V/MediaPlayer(23440): isPlaying: 0
12-03 20:52:36.994: V/MediaPlayer(23440): isPlaying: 0
12-03 20:52:37.164: V/MediaPlayer(23440): message received msg=3, ext1=100, ext2=0
12-03 20:52:37.164: V/MediaPlayer(23440): buffering 100
12-03 20:52:37.164: V/MediaPlayer(23440): callback application
12-03 20:52:37.164: V/MediaPlayer(23440): back from callback
12-03 20:52:37.244: V/MediaPlayer(23440): isPlaying: 0
12-03 20:52:37.504: V/MediaPlayer(23440): isPlaying: 0
12-03 20:52:37.754: V/MediaPlayer(23440): isPlaying: 0
12-03 20:52:38.004: V/MediaPlayer(23440): isPlaying: 0
....
我需要找到一种方法来检索" zombie" MediaPlayer并停止 ...
非常感谢
答案 0 :(得分:9)
在与类似的issue斗争之后,我终于找到了解决方案。看来,如果在关闭Webview时AudioService没有焦点,则不会暂停。这导致了几个问题(另一个玩家无法开始玩等)。
唯一对我有用的解决方案是:
@Override
protected void onPause() {
super.onPause();
((AudioManager)getSystemService(
Context.AUDIO_SERVICE)).requestAudioFocus(
new OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {}
}, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
}
这将导致当前玩家在停下来时被暂停/杀死(不确定是哪个)并解决我的所有问题。 希望它也能帮到你......