关闭正确的MediaPlayer [英] Closing MediaPlayer properly

查看:192
本文介绍了关闭正确的MediaPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了我以为我用,直到我注意到了详细LogCat中输出成品,看到一个MediaPlayer的这个活动不断重复。

I've got a MediaPlayer activity that I thought I was finished with until I noticed the verbose LogCat output and saw this constantly repeating.

01-17 17:03:50.466: D/StatusBarPolicy(209): iconIndex=1
01-17 17:03:50.476: V/StatusBarPolicy(209): cdmaLevel:1;max:4
01-17 17:03:50.476: D/StatusBarPolicy(209): iconLevel:1
01-17 17:03:50.476: D/StatusBarService(209): updateIcon slot=phone_signal index=20 viewIndex=14 old=StatusBarIcon(pkg=com.android.systemui id=0x7f020007 level=0 visible=true num=0 ) icon=StatusBarIcon(pkg=com.android.systemui id=0x7f020008 level=0 visible=true num=0 )
01-17 17:03:50.597: V/MediaPlayer(16768): getCurrentPosition
01-17 17:03:50.597: V/MediaPlayerService(82): getCurrentPosition
01-17 17:03:50.597: V/MediaPlayerService(82): [261] getCurrentPosition = 277943
01-17 17:03:50.597: V/MediaPlayerService(82): [261] isPlaying: 0
01-17 17:03:50.597: V/MediaPlayer(16768): isPlaying: 0
01-17 17:03:50.597: V/MediaPlayerService(82): [261] isPlaying: 0
01-17 17:03:50.597: V/MediaPlayer(16768): isPlaying: 0
01-17 17:03:50.847: V/MediaPlayer(16768): getCurrentPosition
01-17 17:03:50.847: V/MediaPlayerService(82): getCurrentPosition
01-17 17:03:50.847: V/MediaPlayerService(82): [261] getCurrentPosition = 277943'

这让我觉得我没有正确关闭我的活动。在code我使用的是

This makes me think I'm not closing my activity properly. The code I'm using for that is

cancelButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            new AlertDialog.Builder( CastrRecorder.this )
            .setTitle( "Close" )
            .setMessage( "Any unsaved changes will be lost. Continue?" )
            .setPositiveButton( "Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Log.d( "AlertDialog", "Positive" );
                    mPlayer.stop();
                    mPlayer.release();
                    Intent baseIntent = new Intent(Recorder.this, Activity.class);
                    Recorder.this.startActivity(baseIntent);
                }
            })
            .setNegativeButton( "No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Log.d( "AlertDialog", "Negative" );
                }
            } )
            .show();
        }
    });

在previous code不断重复的用户完全关闭该应用程序后还是一样。我不禁认为这是坏的。我可以做prevent这是什么?

The previous code keeps repeating even after the user closes the app completely. I can't help but think that would be bad. What can I do to prevent this?

推荐答案

不知道这是至关重要的,但我打电话mPlayer.reset()在停止之间的()和release()(我没有看到这个问题)。你绝对相信它的您的应用程序正在生成这些日志条目?

Not sure if this is critical, but I call mPlayer.reset() in between stop() and release() (and I don't see this problem). Are you absolutely sure it's your app that's generating these log entries?

此外,你的媒体播放器呼叫不会在UI线程上运行,因为它们会导致ANR的是很重要的。此外,在相同的背景线程中运行他们都让媒体播放器调用get系列化,因为MediaPlayer的不是线程安全的。

Also, it's important that your media player calls are not run on the UI thread as they can cause ANRs. Also, run them all on the same background thread so that media player calls get serialized, since the MediaPlayer is not thread safe.

这篇关于关闭正确的MediaPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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