回到上一个活动时,我的媒体进度会丢失(seekBar和媒体播放器进度)

时间:2018-03-13 07:07:57

标签: java android android-mediaplayer android-seekbar

实际上我有2个活动,在主要活动中我使用回收视图显示所有歌曲并在其上设置onClickListner,当有人点击它时,它启动一个新活动(MediaPlayer Activity)并开始播放歌曲以及媒体控制(如paly,pause和seekBar)。

但是当我回到主要活动并点击一个按钮(基本上再次进入MediaPlayer活动)时,所有mediaPlayer和seekBar进度都消失了(播放,暂停,下一个和上一个按钮)没有响应点击次数。

这是我开始MediaPlayer活动的地方:

 @Override
    public  void onBindViewHolder(final MyViewHolder holder, final int position) {

        holder.Song.setText(mData.get(position).title);
        holder.Artist.setText(mData.get(position).artist);


        Glide.with(mcontext).load(mData.get(position).imagePath).
                placeholder(R.drawable.unknown_art).into(holder.PhotoId);


        holder.Options.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mcontext, "You clicked on options", Toast.LENGTH_SHORT).show();
            }
        });

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


                Intent intent = new Intent(mcontext, MediaPlayerActivity.class);
                intent.putExtra("songPosition",position);
                mcontext.startActivity(intent);


            }
        });

这些是MediaPlayerActivity中的代码:

public class  MediaPlayerActivity extends AppCompatActivity {

    static MediaPlayer mediaPlayer=null;
     static int pos=0;
     SeekBar seekBar;

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

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        Intent intent = getIntent();
        pos = intent.getIntExtra("songPosition",pos);

        seekBar = (SeekBar)findViewById(R.id.seekBar);

        Button next = (Button)findViewById(R.id.button_1);
        Button previous = (Button)findViewById(R.id.button_2);
        final Button play_pause = (Button)findViewById(R.id.button_3);
        final TextView info = (TextView)findViewById(R.id.songInfo);

         info.setText(VerticalRecyclerViewAdapter.mData.get(pos).title);

         if(mediaPlayer!=null&&mediaPlayer.isPlaying()){

             mediaPlayer.stop();
             mediaPlayer.reset();
             mediaPlayer.release();
         }
         playMedia(pos);


         next.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 try {
                     mediaPlayer.stop();
                     mediaPlayer.reset();
                     mediaPlayer.release();
                     playMedia(++pos);
                     info.setText(VerticalRecyclerViewAdapter.mData.get(pos).title);
                 }catch (IndexOutOfBoundsException e){
                     Toast.makeText(MediaPlayerActivity.this,"Already the last song!!!",Toast.LENGTH_SHORT).show();
                 }
             }
         });




         previous.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 try {
                     mediaPlayer.stop();
                     mediaPlayer.reset();
                     mediaPlayer.release();
                     playMedia(--pos);
                     info.setText(VerticalRecyclerViewAdapter.mData.get(pos).title);
                 }catch (IndexOutOfBoundsException e){
                     Toast.makeText(MediaPlayerActivity.this,"Already the first song!!!",Toast.LENGTH_SHORT).show();
                 }
             }
         });



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

                 if(mediaPlayer!=null&&mediaPlayer.isPlaying()){

                     play_pause.setText(R.string.mp_play);
                     mediaPlayer.pause();
                 }
                 else{
                     play_pause.setText(R.string.mp_Pause);
                     mediaPlayer.start();
                  }
             }
         });


    }

    public void playMedia(int pos){

        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(VerticalRecyclerViewAdapter.mData.get(pos).songPath);
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    seekBar.setProgress(0);
                    seekBar.setMax(mp.getDuration());
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        setSeekbarProgress();
        onSeekbarChange();
        playNext();

    }

    public void playNext(){

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                try {
                    mediaPlayer.release();
                    playMedia(++MediaPlayerActivity.pos);

                }catch (IndexOutOfBoundsException e){

                    Toast.makeText(MediaPlayerActivity.this, "Already the last song", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    public void setSeekbarProgress(){

        new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {

                try {
                    if (mediaPlayer != null) {
                        seekBar.setProgress(mediaPlayer.getCurrentPosition());
                    }
                }
                catch (RuntimeException e){
                    Log.i("CRASH",""+e.getMessage());
                }
            }
        },0,1000);
    }


    public void onSeekbarChange(){

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if(b) {
                    mediaPlayer.seekTo(i);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }

        });
    }



}

这就是我想回到MediaPlayer活动(点击按钮)的地方:

 mFloatingToolbar.setClickListener(new FloatingToolbar.ItemClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onItemClick(MenuItem item) {

                switch (item.getItemId()) {

                    case R.id.music:

                        Intent intent = new Intent(getActivity(),MediaPlayerActivity.class);
                        startActivity(intent);

                        break;

                    case R.id.pause :


                        break;
                    case R.id.next :
                        Toast.makeText(getContext(), "Next", Toast.LENGTH_SHORT).show();
                        break;


                    default:
                        Toast.makeText(getContext(), "Back", Toast.LENGTH_SHORT).show();
                      verticalRecyclerViewAdapter.notifyDataSetChanged();
                        break;

                }
            }
            @Override
            public void onItemLongClick(MenuItem item) {

            }
        });

0 个答案:

没有答案