自动下一首歌使搜索栏和按钮崩溃

时间:2018-04-01 14:10:12

标签: java android

我是Android编程的新手。我按照媒体播放器教程,但我现在正试图让它更先进,唯一的问题就是没有按预期工作。

当歌曲结束时,它应该开始播放下一首歌曲,但不会更新我的搜索栏(只更新Max),这会让我的按钮崩溃我的应用程序。我尝试过很多东西但是自己无法修复它。如果我按下一个按钮(音乐曲目结束前)按钮和搜索栏工作。

我想要的是我的搜索栏重置新歌并重新开始,我希望我的按钮在onCompletion下一首歌之后工作。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);

    btPlay = (ImageButton) findViewById(R.id.btPlay);
    btNxt = (ImageButton) findViewById(R.id.btNxt);
    btPv = (ImageButton) findViewById(R.id.btPv);

    btPlay.setOnClickListener(this);
    btNxt.setOnClickListener(this);
    btPv.setOnClickListener(this);

    sb = (SeekBar) findViewById(R.id.seekBar);
    updateSeekBar = new Thread() {
        @Override
        public void run() {
            int TotalDuration = mp.getDuration();
            int currentPosition = 0;
            while (currentPosition < TotalDuration) try {
                sleep(500);
                currentPosition = mp.getCurrentPosition();
                sb.setProgress(currentPosition);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

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

        mp.release();
    }
    Intent i = getIntent();
    Bundle b = i.getExtras();
    mySongs = (ArrayList) b.getParcelableArrayList("songlist");
    position = b.getInt("pos", 0);

    u = Uri.parse(mySongs.get(position).toString());
    mp = MediaPlayer.create(getApplicationContext(), u);
    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            try {
                mp.stop();
                mp.reset();
                position = (position + 1) % mySongs.size();
                u = Uri.parse(mySongs.get(position).toString());
                mp = MediaPlayer.create(getApplicationContext(), u);
                sb.setMax(mp.getDuration());
                mp.start();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    mp.start();
    sb.setMax(mp.getDuration());
    updateSeekBar.start();

    sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

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

@Override
public void onClick(View v) {
    int id = v.getId();
    switch (id) {
        case R.id.btPlay:
            if (mp.isPlaying()) {
                mp.pause();
                btPlay.setImageResource(R.drawable.play);
            } else {
                mp.start();
                btPlay.setImageResource(R.drawable.pause);
            }
            break;
        case R.id.btNxt:
            try {
                if (mp.isPlaying()) {
                    mp.stop();
                    mp.release();
                    position = (position + 1) % mySongs.size();
                    u = Uri.parse(mySongs.get(position).toString());
                    mp = MediaPlayer.create(getApplicationContext(), u);
                    mp.start();
                    sb.setMax(mp.getDuration());
                }
            } catch (Exception e) {
                e.printStackTrace();

            }

            break;
        case R.id.btPv:
            try {
                if (mp.isPlaying()) {
                    mp.stop();
                    mp.release();
                    position = (position - 1 < 0) ? mySongs.size() - 1 : position - 1;
                    u = Uri.parse(mySongs.get(position).toString());
                    mp = MediaPlayer.create(getApplicationContext(), u);
                    mp.start();
                    sb.setMax(mp.getDuration());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            break;
        default:

            break;
    }
}

}

错误(在Android监视器中):

java.lang.IllegalStateException

0 个答案:

没有答案