4

我的 Android 应用程序的一项活动包含一个显示 GrooveShark 播放器网页的WebView 。当用户点击网页的播放按钮时,Activity 会自动启动一个默认的 MediaPlayer来播放曲目。一切正常,当用户关闭此活动和 MediaPlayer 时出现问题,虽然不再播放曲目,但它仍然存在......即使应用程序关闭,MediaPlayer 仍然存在。杀死它的唯一方法是强制应用程序停止......我似乎没什么大不了的,但在某些设备中,当用户在电话后挂断时,这个“僵尸”MediaPlayer 突然开始播放即使该应用程序先前已被销毁,也可以跟踪...

这是我的 Activity 代码的摘要:

@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
....

我需要找到一种方法来检索那个“僵尸”媒体播放器并停止它......

非常感谢

4

1 回答 1

10

在遇到类似问题后,我终于找到了解决方案。看来,如果关闭 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);

}

这将导致当前玩家在下线时被暂停/杀死(不确定是哪个)并解决我的所有问题/问题。希望它也可以帮助你...

于 2013-12-12T20:52:04.087 回答