SeekBar не в состоянии искать прогресс в приложении Музыкального проигрывателя - PullRequest
0 голосов
/ 07 марта 2019

Я новичок в разработке Android.Я пытаюсь создать приложение для музыкального плеера.Эта программа выдает IllegalStateException, если я удаляю блок захвата в строке

currentPosition = myMediaPlayer.getCurrentPosition();

Код:

updateSeekBar = new Thread(){

        @Override
        public void run(){

            int totalDuration = myMediaPlayer.getDuration();
            int currentPosition = 0;

            while(currentPosition<totalDuration)
            {
                try {
                    sleep(200);
                    currentPosition = myMediaPlayer.getCurrentPosition();
                    songSeekBar.setProgress(currentPosition);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                catch (IllegalStateException e)
                {

                    return;
                }
            }

        }
    };

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

    String name = getIntent().getStringExtra("SongName");
    String[] namearray = new String[name.length()];
    namearray = m.WordsUsingSplit(name);
    label.setText(namearray[0]);
    label.setSelected(true);

    mySongNameList = getIntent().getStringArrayListExtra("SongsList");

    Bundle bundle=getIntent().getExtras();

    mySongs = (ArrayList) bundle.getParcelableArrayList("PathList");
    song = mySongs.get(position).getName();
    position = bundle.getInt("pos",0);

    play(position);

    /*
    images = getIntent().getStringArrayListExtra("coverArt");
    information = (TextView) findViewById(R.id.info);
    information.setText(images.get(position).toString());
    File f = new File(images.get(position));
    Bitmap bm = BitmapFactory.decodeFile(f.getAbsolutePath());
    imageView = (ImageView) findViewById(R.id.cover_image);
    imageView.setImageBitmap(bm);
    */

    songSeekBar.setMax(myMediaPlayer.getDuration());

    updateSeekBar.start();

    songSeekBar.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) {
            myMediaPlayer.seekTo(seekBar.getProgress());
        }
    });

    bpause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            songSeekBar.setMax(myMediaPlayer.getDuration());

            if(myMediaPlayer.isPlaying()){
                bpause.setBackgroundResource(R.drawable.play_icon);
                myMediaPlayer.pause();
            }
            else
            {
                bpause.setBackgroundResource(R.drawable.pause_icon);
                myMediaPlayer.start();
            }
        }
    });

    bnext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            myMediaPlayer.stop();
            myMediaPlayer.release();

            position = (position+1)%mySongs.size();
            play(position);
        }
    });

    bprev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            myMediaPlayer.stop();
            myMediaPlayer.release();

            position = (((position-1)<0)?(mySongs.size()-1):(position-1));
            play(position);
        }
    });

    bshuffle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            myMediaPlayer.stop();
            myMediaPlayer.release();

            position = (int) (mySongs.size()* Math.random())+1;
            play(position);
        }
    });

}

void play(int p)
{
    Uri u = Uri.parse(mySongs.get(p).toString());
    myMediaPlayer = MediaPlayer.create(getApplicationContext(),u);

    String curName = mySongNameList.get(p);
    String[] aray = new String[curName.length()];
    aray = m.WordsUsingSplit(curName);
    label.setText(aray[0]);

    myMediaPlayer.start();
    myMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            bnext.performClick();
        }
    });
  }
}
...