Android-媒体播放器在current = mp.getCurrentPosition();

时间:2016-12-18 12:50:46

标签: android android-mediaplayer fatal-error

我正在尝试让媒体播放器播放音乐。但是在播放3-4首歌后它停止了。并给出以下错误。 我正好在current = mp.getCurrentPosition();收到错误 我的代码是:

public class player extends AppCompatActivity implements View.OnClickListener {

    ArrayList<File> mysong;
    static MediaPlayer mp;
    SeekBar sb;
    int position;
    Uri u;
    TextView tv;
    Thread updateseek;
    Button btplay, btFF, btBB, btnext, btpreviout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        sb = (SeekBar) findViewById(R.id.seekBar);
        btplay = (Button) findViewById(R.id.button);
        btFF = (Button) findViewById(R.id.button3);
        btBB = (Button) findViewById(R.id.button2);
        btnext = (Button) findViewById(R.id.button5);
        btpreviout = (Button) findViewById(R.id.button4);
        tv = (TextView)findViewById(R.id.textView2);
        tv.setVisibility(View.INVISIBLE);

        btpreviout.setOnClickListener(this);
        btnext.setOnClickListener(this);
        btBB.setOnClickListener(this);
        btFF.setOnClickListener(this);
        btplay.setOnClickListener(this);

        if (mp != null) {
            mp.stop();
            mp.release();
        }

        updateseek = new Thread(){
            @Override
            public void run() {
                int total = mp.getDuration();
                int current = 0;
               sb.setMax(total);
                while(current < total){
                    try {
                        sleep(500);
                        current = mp.getCurrentPosition();
                        sb.setProgress(current);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                super.run();
            }
        };



        Intent i = getIntent();
        Bundle b = i.getExtras();
        mysong = (ArrayList) b.getParcelableArrayList("mysonglist");
        position = b.getInt("pos", 0);

        u = Uri.parse(mysong.get(position).toString());
        mp = MediaPlayer.create(getApplicationContext(), u);
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.start();
        sb.setMax(mp.getDuration());
        updateseek.start();

        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });


        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    if(sb.getProgress()>(mp.getDuration()/2)){
                        tv.setVisibility(View.VISIBLE);
                    }else tv.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                btplay.setText("p");
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                    mp.seekTo(seekBar.getProgress());
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                if (mp.isPlaying()) {
                    mp.pause();
                } else mp.start();
                break;

            case R.id.button2:
                mp.seekTo(mp.getCurrentPosition() - 5000);
                break;

            case R.id.button3:
                mp.seekTo(mp.getCurrentPosition() + 5000);
                break;

            case R.id.button4:
                mp.stop();
                mp.release();
                position = (position - 1 < 0) ? mysong.size() - 1 : (position - 1) % mysong.size();
                u = Uri.parse(mysong.get(position).toString());
                mp = MediaPlayer.create(getApplicationContext(), u);
                sb.setProgress(0);
                mp.start();
                sb.setMax(mp.getDuration());
                break;

            case R.id.button5:
                mp.stop();
                mp.release();
                position = (position + 1) % mysong.size();
                u = Uri.parse(mysong.get(position).toString());
                mp = MediaPlayer.create(getApplicationContext(), u);
                sb.setProgress(0);
                mp.start();
                sb.setMax(mp.getDuration());
                break;
        }
    }
}

我的日志猫在这里:

  

12-18 18:04:37.483 4846-4846 / com.bucketlist.dell.musicplayer   W / Resources:转换为字符串:TypedValue {t = 0x12 / d = 0x0 a = 3   r = 0x7f0c0057} 12-18 18:04:37.483   4846-4846 / com.bucketlist.dell.musicplayer W / Resources:转换为   string:TypedValue {t = 0x12 / d = 0x0 a = 3 r = 0x7f0c0058} 12-18 18:04:37.493   4846-4846 / com.bucketlist.dell.musicplayer W / Resources:转换为   string:TypedValue {t = 0x12 / d = 0x0 a = 3 r = 0x7f0c0072} 12-18 18:04:37.493   4846-4846 / com.bucketlist.dell.musicplayer W / Resources:转换为   string:TypedValue {t = 0x12 / d = 0x0 a = 3 r = 0x7f0c0073} 12-18 18:04:37.493   4846-4846 / com.bucketlist.dell.musicplayer D / AbsSeekBar:AbsSeekBar   构造函数:mAllowedSeeBarAnimation = false 12-18 18:04:37.503   4846-4846 / com.bucketlist.dell.musicplayer W / Resources:转换为   string:TypedValue {t = 0x12 / d = 0x0 a = 3 r = 0x7f0c0074} 12-18 18:04:37.503   4846-4846 / com.bucketlist.dell.musicplayer W / Resources:转换为   string:TypedValue {t = 0x12 / d = 0x0 a = 3 r = 0x7f0c0075} 12-18 18:04:37.503   4846-4846 / com.bucketlist.dell.musicplayer W / Resources:转换为   string:TypedValue {t = 0x12 / d = 0x0 a = 3 r = 0x7f0c0076} 12-18 18:04:37.513   4846-4846 / com.bucketlist.dell.musicplayer W / Resources:转换为   string:TypedValue {t = 0x12 / d = 0x0 a = 3 r = 0x7f0c0077} 12-18 18:04:37.513   4846-4846 / com.bucketlist.dell.musicplayer W / Resources:转换为   string:TypedValue {t = 0x12 / d = 0x0 a = 3 r = 0x7f0c0078} 12-18 18:04:37.513   4846-4846 / com.bucketlist.dell.musicplayer W / Resources:转换为   string:TypedValue {t = 0x12 / d = 0x0 a = 3 r = 0x7f0c0079} 12-18 18:04:37.513   4846-4846 / com.bucketlist.dell.musicplayer I / MediaPlayer:sendBroadcast   CONTEXT_AWARE_MUSIC_INFO - 类型(停止) - id(573)12-18 18:04:37.633   4846-4846 / com.bucketlist.dell.musicplayer E / MediaPlayer-JNI:   QCMediaPlayer媒体播放器不存在12-18 18:04:37.693   4846-4856 / com.bucketlist.dell.musicplayer W / MediaPlayer:info / warning   (973,0)12-18 18:04:37.703 4846-5528 / com.bucketlist.dell.musicplayer   E / AndroidRuntime:致命异常:Thread-26716                                                                                  过程:com.bucketlist.dell.musicplayer,PID:4846                                                                                  java.lang.IllegalStateException                                                                                      在android.media.MediaPlayer.getCurrentPosition(Native方法)                                                                                      在com.bucketlist.dell.musicplayer.player $ 1.run(player.java:65)12-18   18:04:37.703 4846-4846 / com.bucketlist.dell.musicplayer E / MediaPlayer:   应该有字幕控制器已设置12-18 18:04:37.813   4846-4846 / com.bucketlist.dell.musicplayer D / PhoneWindow: FMB   isFloatingMenuEnabled mFloatingMenuBtn:null 12-18 18:04:37.813   4846-4846 / com.bucketlist.dell.musicplayer D / PhoneWindow: FMB   isFloatingMenuEnabled返回false 12-18 18:04:37.843   4846-4846 / com.bucketlist.dell.musicplayer D / SRIB_DCS:log_dcs   输入了ThreadedRenderer :: initialize! 12-18 18:04:37.863   4846-4846 / com.bucketlist.dell.musicplayer I / MediaPlayer:不要发送   意图。 msg.arg1 = 0,msg.arg2 = 0 12-18 18:04:37.863   4846-4846 / com.bucketlist.dell.musicplayer E / MediaPlayer:应该有   字幕控制器已设置12-18 18:04:37.883   4846-4846 / com.bucketlist.dell.musicplayer I / MediaPlayer:发送上下文   意识到事件12-18 18:04:37.893   4846-4846 / com.bucketlist.dell.musicplayer I / MediaPlayer:sendBroadcast   CONTEXT_AWARE_MUSIC_INFO - 类型(开始) - id(574)12-18 18:04:39.623   4846-5528 / com.bucketlist.dell.musicplayer I / Process:发送信号。   PID:4846 SIG:9

     

12-18 18:04:37.633 4846-4846 / com.bucketlist.dell.musicplayer E / MediaPlayer-JNI:QCMediaPlayer媒体播放器不存在12-18   18:04:37.693 4846-4856 / com.bucketlist.dell.musicplayer W / MediaPlayer:   info / warning(973,0)12-18 18:04:37.703   4846-5528 / com.bucketlist.dell.musicplayer E / AndroidRuntime:致命   例外:Thread-26716                                                                                  过程:com.bucketlist.dell.musicplayer,PID:4846                                                                                  java.lang.IllegalStateException                                                                                      在android.media.MediaPlayer.getCurrentPosition(Native方法)                                                                                      在com.bucketlist.dell.musicplayer.player $ 1.run(player.java:65)12-18   18:04:37.703 4846-4846 / com.bucketlist.dell.musicplayer E / MediaPlayer:   应该已经设置了字幕控制器

1 个答案:

答案 0 :(得分:0)

在开发音乐播放器时,我遇到了同样的异常,问题的根本原因是从错误的状态调用mp.getCurrentPosition()。

你从oncreate开始直接更新并且它连续运行,如果一首歌完成并且你正在尝试播放当时媒体播放器处于停止状态的下一首歌,那时你尝试调用getCurrentPosition它会触发异常。

因此在调用mp.getCurrentPosition()之前添加一个if条件(mp.isPlaying)并确保在调用getCurrentPosition()时播放该歌曲。