通过实例验证播放本地音乐MediaPlayer的reset函数并不会阻塞主线程

如题,通过下面的一个例子,频繁点击播放下一首歌,验证MediaPlayer的reset函数是否会发生阻塞。

验证数据如下:在从20:26:04.673到20:26:36.168的32秒时间内,点击170次Next播放下一首歌曲,都成功,没有发生阻塞主线程导致ANR的问题。

由于本例播放的都是本地歌曲 ,所以没有发生阻塞的情况。如果是播放在线歌曲,需要与网络交换,频繁执行播放动作,可能会发生ANR;播放网络歌曲需要使用异步的prepareAsync函数,而不能使用同步的prepare,以免发生阻塞。

(1)第一次播放的log如下:

06-24 20:26:04.673 26426 26426 D MainActivity: -------------------------------
06-24 20:26:04.678 26426 26426 D MainActivity: play next music
06-24 20:26:04.678 26426 26426 D MainActivity: before reset
06-24 20:26:04.683 26426 26426 D MainActivity: after reset
06-24 20:26:04.683 26426 26426 D MainActivity: before setDataSource:/mnt/sdcard/Music/白安/麦田捕手-白安.mp3
06-24 20:26:04.763 26426 26426 D MainActivity: after setDataSource to prepare
06-24 20:26:04.768 26426 26426 D MainActivity: onPrepared
06-24 20:26:04.898 26426 26426 D MainActivity: -------------------------------


(2)最后6次播放的log如下:

06-24 20:26:35.013 26426 26426 D MainActivity: -------------------------------
06-24 20:26:35.013 26426 26426 D MainActivity: play next music
06-24 20:26:35.013 26426 26426 D MainActivity: before reset
06-24 20:26:35.018 26426 26426 D MainActivity: after reset
06-24 20:26:35.018 26426 26426 D MainActivity: before setDataSource:/mnt/sdcard/Music/Download/余生一起过-刘德华.aac
06-24 20:26:35.068 26426 26426 D MainActivity: after setDataSource to prepare
06-24 20:26:35.073 26426 26426 D MainActivity: onPrepared
06-24 20:26:35.213 26426 26426 D MainActivity: -------------------------------
06-24 20:26:35.213 26426 26426 D MainActivity: play next music
06-24 20:26:35.213 26426 26426 D MainActivity: before reset
06-24 20:26:35.218 26426 26426 D MainActivity: after reset
06-24 20:26:35.218 26426 26426 D MainActivity: before setDataSource:/mnt/sdcard/Music/白安/麦田捕手-白安.mp3
06-24 20:26:35.298 26426 26426 D MainActivity: after setDataSource to prepare
06-24 20:26:35.308 26426 26426 D MainActivity: onPrepared
06-24 20:26:35.403 26426 26426 D MainActivity: -------------------------------
06-24 20:26:35.403 26426 26426 D MainActivity: play next music
06-24 20:26:35.403 26426 26426 D MainActivity: before reset
06-24 20:26:35.403 26426 26426 D MainActivity: after reset
06-24 20:26:35.403 26426 26426 D MainActivity: before setDataSource:/mnt/sdcard/Music/Download/幸福就是-刘若英.mp3
06-24 20:26:35.463 26426 26426 D MainActivity: after setDataSource to prepare
06-24 20:26:35.463 26426 26426 D MainActivity: onPrepared
06-24 20:26:35.593 26426 26426 D MainActivity: -------------------------------
06-24 20:26:35.593 26426 26426 D MainActivity: play next music
06-24 20:26:35.593 26426 26426 D MainActivity: before reset
06-24 20:26:35.598 26426 26426 D MainActivity: after reset
06-24 20:26:35.598 26426 26426 D MainActivity: before setDataSource:/mnt/sdcard/Music/Download/余生一起过-刘德华.aac
06-24 20:26:35.678 26426 26426 D MainActivity: after setDataSource to prepare
06-24 20:26:35.683 26426 26426 D MainActivity: onPrepared
06-24 20:26:35.983 26426 26426 D MainActivity: -------------------------------
06-24 20:26:35.983 26426 26426 D MainActivity: play next music
06-24 20:26:35.983 26426 26426 D MainActivity: before reset
06-24 20:26:35.983 26426 26426 D MainActivity: after reset
06-24 20:26:35.983 26426 26426 D MainActivity: before setDataSource:/mnt/sdcard/Music/白安/麦田捕手-白安.mp3
06-24 20:26:36.068 26426 26426 D MainActivity: after setDataSource to prepare
06-24 20:26:36.073 26426 26426 D MainActivity: onPrepared
06-24 20:26:36.168 26426 26426 D MainActivity: -------------------------------
06-24 20:26:36.168 26426 26426 D MainActivity: play next music
06-24 20:26:36.168 26426 26426 D MainActivity: before reset
06-24 20:26:36.173 26426 26426 D MainActivity: after reset
06-24 20:26:36.173 26426 26426 D MainActivity: before setDataSource:/mnt/sdcard/Music/Download/幸福就是-刘若英.mp3
06-24 20:26:36.238 26426 26426 D MainActivity: after setDataSource to prepare
06-24 20:26:36.243 26426 26426 D MainActivity: onPrepared


(3)执行reset前后的log如下,reset在极短的时间内就会执行完毕,一般在10毫秒以内。

3:06-24 20:26:04.678 26426 26426 D MainActivity: before reset
4:06-24 20:26:04.683 26426 26426 D MainActivity: after reset
10:06-24 20:26:04.898 26426 26426 D MainActivity: before reset
11:06-24 20:26:04.898 26426 26426 D MainActivity: after reset
17:06-24 20:26:05.063 26426 26426 D MainActivity: before reset
18:06-24 20:26:05.068 26426 26426 D MainActivity: after reset
24:06-24 20:26:05.243 26426 26426 D MainActivity: before reset
25:06-24 20:26:05.243 26426 26426 D MainActivity: after reset
31:06-24 20:26:05.578 26426 26426 D MainActivity: before reset
32:06-24 20:26:05.583 26426 26426 D MainActivity: after reset
38:06-24 20:26:05.758 26426 26426 D MainActivity: before reset
39:06-24 20:26:05.763 26426 26426 D MainActivity: after reset
45:06-24 20:26:05.918 26426 26426 D MainActivity: before reset
46:06-24 20:26:05.918 26426 26426 D MainActivity: after reset
52:06-24 20:26:06.098 26426 26426 D MainActivity: before reset
53:06-24 20:26:06.098 26426 26426 D MainActivity: after reset
......

1144:06-24 20:26:34.838 26426 26426 D MainActivity: before reset
1145:06-24 20:26:34.838 26426 26426 D MainActivity: after reset
1151:06-24 20:26:35.013 26426 26426 D MainActivity: before reset
1152:06-24 20:26:35.018 26426 26426 D MainActivity: after reset
1158:06-24 20:26:35.213 26426 26426 D MainActivity: before reset
1159:06-24 20:26:35.218 26426 26426 D MainActivity: after reset
1165:06-24 20:26:35.403 26426 26426 D MainActivity: before reset
1166:06-24 20:26:35.403 26426 26426 D MainActivity: after reset
1172:06-24 20:26:35.593 26426 26426 D MainActivity: before reset
1173:06-24 20:26:35.598 26426 26426 D MainActivity: after reset
1179:06-24 20:26:35.983 26426 26426 D MainActivity: before reset
1180:06-24 20:26:35.983 26426 26426 D MainActivity: after reset
1186:06-24 20:26:36.168 26426 26426 D MainActivity: before reset
1187:06-24 20:26:36.173 26426 26426 D MainActivity: after reset


(4)验证代码如下:

public class MainActivity extends Activity implements View.OnClickListener
{

    private static final String TAG = "MainActivity";

    private MediaPlayer mMediaPlayer = null;
    private Button mBtnPlayStop;
    private Button mBtnNext;
    
    private boolean mIsPlayFlag = false;
    
    private String mPathArray[] = new String[] {
      "/mnt/sdcard/Music/白安/麦田捕手-白安.mp3",
      "/mnt/sdcard/Music/Download/幸福就是-刘若英.mp3",
      "/mnt/sdcard/Music/Download/余生一起过-刘德华.aac"
    };
    private int mIndex = 0;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mBtnPlayStop = (Button)findViewById(R.id.btnPlayStop);
        mBtnPlayStop.setOnClickListener(this);
        
        mBtnNext = (Button)findViewById(R.id.btnNext);
        mBtnNext.setOnClickListener(this);
        
        setVolumeControlStream(AudioManager.STREAM_MUSIC);

        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer arg0) {
                Log.d(TAG, "onPrepared");
                mMediaPlayer.start();
            }
            
        });
        mMediaPlayer.setOnCompletionListener(new OnCompletionListener()
        {

            @Override
            public void onCompletion(MediaPlayer mp)
            {
                Log.d(TAG, "onCompletion");
                next();
            }
        });
        mMediaPlayer.setOnErrorListener(new OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.d(TAG, "onError what:" + what + ", extra:" + extra);
                
                return false;
            }
            
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())  
        {  
        case R.id.btnPlayStop:
            if (mIsPlayFlag) {
                try {
                    Log.d(TAG,"-------------------------------");
                    Log.d(TAG, "play:" + mPathArray[mIndex]);
                    Log.d(TAG, "before reset");
                    mMediaPlayer.reset();
                    Log.d(TAG, "after reset");
                    
                    Log.d(TAG, "before setDataSource");
                    mMediaPlayer.setDataSource(mPathArray[mIndex]);
                    Log.d(TAG, "after setDataSource to prepare");
                    mMediaPlayer.prepare();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                mBtnPlayStop.setText("Stop");
            } else {
                Log.d(TAG,"-------------------------------");
                Log.d(TAG, "stop" + mPathArray[mIndex]);
                mMediaPlayer.stop();
                mBtnPlayStop.setText("Play");
            }
            
            mIsPlayFlag = !mIsPlayFlag;
            break;

        case R.id.btnNext:
            Log.d(TAG,"-------------------------------");
            Log.d(TAG, "play next music");
            next();
            break;
        }  
    }

 
    private void next() {
        try {
            Log.d(TAG, "before reset");
            mMediaPlayer.reset();
            Log.d(TAG, "after reset");
            
            if (mIndex < mPathArray.length - 1) {
                mIndex++;
            } else {
                mIndex = 0;
            }
            Log.d(TAG, "before setDataSource:" + mPathArray[mIndex]);
            mMediaPlayer.setDataSource(mPathArray[mIndex]);
            Log.d(TAG, "after setDataSource to prepare");
            mMediaPlayer.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


发布了270 篇原创文章 · 获赞 276 · 访问量 379万+
展开阅读全文

Android Mediaplayer报错

12-10

顺序播放五六首歌曲的时候机会出现错误 (-38,0); Attempt to call getDuation withot a valid mediaplayer 下面代码 class OnCompletionListener implements android.media.MediaPlayer.OnCompletionListener { @Override public void onCompletion(MediaPlayer mp) { if (currIndex >= (list.size() - 1)) { statethreadFlag = true; mp.stop(); Toast.makeText(getApplicationContext(), "歌曲全部播放完毕", Toast.LENGTH_LONG).show(); } else { statethreadFlag = true; mp.stop(); currIndex++; start(); } } } public void start() { Log.i("mp3", "服务执行了start方法,开始播放歌曲"); if (bundle.getBoolean("flag")) { hMap = list.get(currIndex); player.reset(); try { player.setDataSource(hMap.get("path")); player.prepare(); } catch (Exception e) { e.printStackTrace(); } player.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { // TODO Auto-generated method stub mp.start(); serviceFlag = 1; statethreadFlag = false; new Thread(new StateThread()) { }.start(); } }); class StateThread implements Runnable { @Override public void run() { Log.i("mp3", "服务线程启动"); while (!statethreadFlag) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } nowTime = player.getCurrentPosition(); allTime = player.getDuration(); Log.i("mp3", nowTime + " " + allTime + ""); Intent stateIntent = new Intent(); Bundle bundle = new Bundle(); stateIntent.setAction("com.player.broadcast"); bundle.putInt("now", nowTime); bundle.putInt("all", allTime); bundle.putInt("serviceFlag", serviceFlag); bundle.putInt("currIndex", currIndex); stateIntent.putExtra("state", bundle); sendBroadcast(stateIntent); if (nowTime >= allTime) { break; } } } } 网上找了一天也没解决这个问题 出错了取得的nowTime和allTime 都是-1261004135 希望大神指教,感激不尽,只剩1c币了 问答

android播放器加载时候按返回导致ANR异常,求大神

03-10

我自己写的一个android播放器,滑动屏幕进行快进,然后视频处于加载的时候,点击返回按钮,就会出现ANR异常。求解,想了好久了,求大神相救。不胜感激 ``` 03-10 13:18:44.878: E/mdb_debug(350): mdb_usb_sharenet.c:send_sharenet_msg[L-232]: mdb usb share net channel is not ready!: 0, Success 03-10 13:18:54.468: E/ActivityManager(960): ANR in com.edufound.dayvideo (com.edufound.dayvideo/.GameCpp) 03-10 13:18:54.468: E/ActivityManager(960): PID: 8639 03-10 13:18:54.468: E/ActivityManager(960): Reason: Input dispatching timed out (Waiting because no window has focus but there is a focused application that may eventually add a window when it finishes starting up.) 03-10 13:18:54.468: E/ActivityManager(960): Load: 18.71 / 18.09 / 17.94 03-10 13:18:54.468: E/ActivityManager(960): CPU usage from 6858ms to 83ms ago: 03-10 13:18:54.468: E/ActivityManager(960): 4.2% 960/system_server: 3.5% user + 0.7% kernel / faults: 384 minor 03-10 13:18:54.468: E/ActivityManager(960): 2.4% 202/surfaceflinger: 1.1% user + 1.3% kernel / faults: 16 minor 03-10 13:18:54.468: E/ActivityManager(960): 2.4% 1299/com.baidu.input_mi: 2.1% user + 0.2% kernel / faults: 2363 minor 03-10 13:18:54.468: E/ActivityManager(960): 1.9% 10951/mediaserver: 0% user + 1.9% kernel 03-10 13:18:54.468: E/ActivityManager(960): 1.6% 1172/com.android.systemui: 1.1% user + 0.4% kernel / faults: 13 minor 03-10 13:18:54.468: E/ActivityManager(960): 1.4% 33/kworker/u:1H: 0% user + 1.4% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.7% 733/RX_Thread: 0% user + 0.7% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.5% 462/charger_monitor: 0% user + 0.5% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.5% 1745/mpdecision: 0% user + 0.5% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.3% 8133/kworker/u:2: 0% user + 0.3% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.4% 130/w1_bus_master1: 0% user + 0.4% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.4% 8142/kworker/u:14: 0% user + 0.4% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.4% 8851/kworker/0:0H: 0% user + 0.4% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.2% 3/ksoftirqd/0: 0% user + 0.2% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.1% 7/kworker/u:0H: 0% user + 0.1% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.2% 418/sensors.qcom: 0.2% user + 0% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.2% 705/mdss_fb0: 0% user + 0.2% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.2% 731/TX_Thread: 0% user + 0.2% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.2% 8134/kworker/u:5: 0% user + 0.2% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.2% 8161/kworker/0:2H: 0% user + 0.2% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.2% 13784/adbd: 0% user + 0.2% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0% 199/netd: 0% user + 0% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0% 211/thermal-engine: 0% user + 0% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0% 312/sdcard: 0% user + 0% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0% 339/irq/289-wcd9xxx: 0% user + 0% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0% 1356/com.android.phone: 0% user + 0% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0% 1378/com.qualcomm.services.location: 0% user + 0% kernel / faults: 1 minor 03-10 13:18:54.468: E/ActivityManager(960): 0% 1562/android.process.acore: 0% user + 0% kernel / faults: 4 minor 03-10 13:18:54.468: E/ActivityManager(960): 0.1% 5372/logcat: 0.1% user + 0% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0% 12099/com.tencent.mm: 0% user + 0% kernel / faults: 12 minor 03-10 13:18:54.468: E/ActivityManager(960): 0.1% 12819/com.miui.weather2:weatherservice: 0% user + 0.1% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0% 15554/com.miui.guardprovider: 0% user + 0% kernel 03-10 13:18:54.468: E/ActivityManager(960): 0.1% 22737/com.tencent.mm:push: 0% user + 0.1% kernel 03-10 13:18:54.468: E/ActivityManager(960): 19% TOTAL: 8.4% user + 10% kernel + 0.6% iowait + 0.3% softirq 03-10 13:18:54.468: E/ActivityManager(960): CPU usage from 2062ms to 2579ms later with 99% awake: 03-10 13:18:54.468: E/ActivityManager(960): 5.6% 960/system_server: 0% user + 5.6% kernel 03-10 13:18:54.468: E/ActivityManager(960): 3.7% 989/ActivityManager: 0% user + 3.7% kernel 03-10 13:18:54.468: E/ActivityManager(960): 1.8% 960/system_server: 0% user + 1.8% kernel 03-10 13:18:54.468: E/ActivityManager(960): 1.8% 1464/Binder_B: 1.8% user + 0% kernel 03-10 13:18:54.468: E/ActivityManager(960): 2.5% 462/charger_monitor: 0% user + 2.5% kernel 03-10 13:18:54.468: E/ActivityManager(960): 1.1% 3/ksoftirqd/0: 0% user + 1.1% kernel 03-10 13:18:54.468: E/ActivityManager(960): 1.3% 1172/com.android.systemui: 1.3% user + 0% kernel 03-10 13:18:54.468: E/ActivityManager(960): 1.4% 1745/mpdecision: 0% user + 1.4% kernel 03-10 13:18:54.468: E/ActivityManager(960): 1.4% 10951/mediaserver: 0% user + 1.4% kernel 03-10 13:18:54.468: E/ActivityManager(960): 8.5% TOTAL: 1.4% user + 7.1% kernel 03-10 13:19:00.268: E/AudioSink(10951): received unknown event type: 1 inside CallbackWrapper ! ``` 问答

结束activity中出现MediaPlayer异常

02-22

在结束mediaplayer activity时出现异常,代码: 结束按钮: ImageView imageAllBack = (ImageView)this.findViewById(R.id.imageAllBack); imageAllBack.setOnClickListener(new OnClickListener() { public void onClick(View v) { //mediaPlayer.stop(); if(mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); } finish(); } }); mediaPlayer事件: @Override protected void onStop() { super.onStop(); mediaPlayer.stop(); mediaPlayer.release(); } @Override public boolean onTouchEvent(MotionEvent event) { mediaController.show(); return false; } //--MediaPlayerControl methods---------------------------------------------------- public void start() { mediaPlayer.start(); } public void pause() { mediaPlayer.pause(); } public int getDuration() { return mediaPlayer.getDuration(); } public int getCurrentPosition() { return mediaPlayer.getCurrentPosition(); } public void seekTo(int i) { mediaPlayer.seekTo(i); } public boolean isPlaying() { return mediaPlayer.isPlaying(); } public int getBufferPercentage() { return 0; } public boolean canPause() { return true; } public boolean canSeekBackward() { return true; } public boolean canSeekForward() { return true; } //-------------------------------------------------------------------------------- public void onPrepared(MediaPlayer mediaPlayer) { mediaController.setMediaPlayer(this); mediaController.setAnchorView(findViewById(R.id.main_audio_view)); handler.post(new Runnable() { public void run() { mediaController.setEnabled(true); mediaController.show(); } }); } public class MyMediaController extends MediaController { public MyMediaController(Context context) { super(context); // TODO Auto-generated constructor stub } @Override public void hide() { // Do Nothing to keep the show the controller all times } } 异常: 02-21 21:03:33.829: E/AndroidRuntime(8889): FATAL EXCEPTION: main 02-21 21:03:33.829: E/AndroidRuntime(8889): java.lang.IllegalStateException 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.media.MediaPlayer.isPlaying(Native Method) 02-21 21:03:33.829: E/AndroidRuntime(8889): at rce.android.naturalocal.NaturaPlayer.isPlaying(NaturaPlayer.java:126) 02-21 21:03:33.829: E/AndroidRuntime(8889): at rce.android.naturalocal.NaturaPlayer.getCurrentPosition(NaturaPlayer.java:115) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.widget.MediaController.setProgress(MediaController.java:436) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.widget.MediaController.access$500(MediaController.java:74) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.widget.MediaController$3.handleMessage(MediaController.java:407) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.os.Handler.dispatchMessage(Handler.java:99) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.os.Looper.loop(Looper.java:137) 02-21 21:03:33.829: E/AndroidRuntime(8889): at android.app.ActivityThread.main(ActivityThread.java:4441) 02-21 21:03:33.829: E/AndroidRuntime(8889): at java.lang.reflect.Method.invokeNative(Native Method) 02-21 21:03:33.829: E/AndroidRuntime(8889): at java.lang.reflect.Method.invoke(Method.java:511) 02-21 21:03:33.829: E/AndroidRuntime(8889): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 02-21 21:03:33.829: E/AndroidRuntime(8889): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 02-21 21:03:33.829: E/AndroidRuntime(8889): at dalvik.system.NativeStart.main(Native Method) 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览