停止Android的MediaPlayer的自动网页内的WebView启动 [英] Stop Android MediaPlayer automatically launched by webpage within WebView

查看:981
本文介绍了停止Android的MediaPlayer的自动网页内的WebView启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序的活动之一包含了一个的WebView 显示的歌词播放器的网页。当用户点击网页上的播放按钮活动的自动启动默认的MediaPlayer 播放的曲目。一切工作正常,当用户关闭这一活动和MediaPlayer的问题就出现了,虽然没有播放的曲目了,它的仍活着 ...甚至当应用程序被关闭,在MediaPlayer还活着。只有这样,才能杀死它是迫使应用程序停止......我似乎不是什么大不了的事,但是否在当用户一个电话后挂断了一些设备,这种僵尸的MediaPlayer突然开始播放即使该应用程序已经在轨previously摧毁...

one of the activities of my Android app contains a WebView that displays the GrooveShark player webpage. When the user taps on the play button of the webpage the activity automatically launches a default MediaPlayer that plays the track. Everything works fine, the problem arises when the user closes this activity and the MediaPlayer, although is not playing the track anymore, it remains alive... even when the app is closed, the MediaPlayer is still alive. The only way to kill it is by forcing the app to stop... I doesn't seem a big deal but in some devices when the user hangs up after a phone call, this "zombie" MediaPlayer all of a sudden starts to play the track even though the app was already previously destroyed...

这是在 $ C $我活动了C 摘要:

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

我需要找到一种方法来检索僵尸的MediaPlayer,并阻止它

感谢

推荐答案

有类似<一个挣扎后, href="http://stackoverflow.com/questions/20480111/android-dismissing-webview-with-html5-video-galaxy-s4?noredirect=1#comment30607301_20480111">issue,我终于找到了解决办法。看来,如果AudioService没有焦点,当你关闭web视图,它不会暂停。这将导致severals问题(另一个球员无法开始播放等。)

After struggling with a similar issue, I finally found a solution. It appears that if the AudioService does not have focus when you close the Webview, it is not paused. This causes severals issues (another player cannot start playing etc.)

这工作对我来说,唯一的解决办法是:

The only solution that worked for me is:

@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);

}

这将导致当前玩家被暂停/杀死(不知道这)会下降,解决了我所有的问题/问题的时候。 希望它可以帮助你太...

This will cause the current player to be paused/killed (not sure which) when going down and solve all of my issues / problems. Hope it could help you too...

这篇关于停止Android的MediaPlayer的自动网页内的WebView启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆