android 将搜索栏设置为初始并将播放按钮切换为在音乐结束时暂停

android set seekbar to initial and toggle play button to pause when music finish

我正在从头开始设计一个媒体播放器,所以我陷入困境的是,当歌曲播放完毕后,我想将搜索栏设置为初始值 我知道这可以用

来完成

seekBar.setProgress(0) but this don't work with me and i want the play button to switch back to pause button and if the user play song again than the song will be played normally here is my code of media player and hope you tell me what is the logic and how to place it

public class MusicPlayerActivity extends AppCompatActivity implements Runnable,
        SeekBar.OnSeekBarChangeListener {
ImageView playpause;
SeekBar seekBar;
MediaPlayer mp = null;
int len = 0;
boolean isPlaying = false;


public MusicPlayerActivity() throws IOException {
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_music_player);
    String url = getIntent().getExtras().getString("musicurl");
    playpause = (ImageView)findViewById(R.id.imageView);
    seekBar = (SeekBar)findViewById(R.id.seekBar);
    playpause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(!isPlaying){
                playpause.setImageResource(R.drawable.pause);
                mp.pause();
                len = mp.getCurrentPosition();
                seekBar.setEnabled(false);


            }else{
                playpause.setImageResource(R.drawable.play);
                mp.seekTo(len);
                mp.start();
                seekBar.setEnabled(true);

            }
            isPlaying = !isPlaying;



        }
    });

    mp = new MediaPlayer();
    try {
        mp.setDataSource(url);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        mp.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //if(mp.isPlaying()) mp.stop(); mp.release();
    mp.start();
    seekBar.setMax(mp.getDuration());
    new Thread(this).start();
   // Toast.makeText(this, mp.getDuration(), Toast.LENGTH_SHORT).show();


}

//if(mp.isPlaying()){}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
                              boolean fromUser) {
    try {
        if (mp.isPlaying() || mp != null) {
            if (fromUser)
                mp.seekTo(progress);
        } else if (mp == null) {
            Toast.makeText(getApplicationContext(), "Media is not running",
                    Toast.LENGTH_SHORT).show();
            seekBar.setProgress(0);
        }
    } catch (Exception e) {
        Log.e("seek bar", "" + e);
        seekBar.setEnabled(false);

    }
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}

public void run() {
    int currentPosition = mp.getCurrentPosition();
    int total = mp.getDuration();

    while (mp != null && currentPosition < total) {
        try {
            Thread.sleep(1000);
            currentPosition = mp.getCurrentPosition();
        } catch (InterruptedException e) {
            return;
        } catch (Exception e) {
            return;
        }
        seekBar.setProgress(currentPosition);
    }
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            mp.stop();
            startActivity(new Intent(this,MainActivity.class));
            break;
    }
    return true;
}
@Override
public void onBackPressed() {
    Intent mainActivity = new Intent(Intent.ACTION_MAIN);
    mainActivity.addCategory(Intent.CATEGORY_HOME);
    mainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(mainActivity);
}

}

在您的 onCreate 中添加 seekbar.setOnSeekBarChangeListener(this); 否则 onProgressChanged() 不会被调用。

要重置进度条,请查看以下方法:

在您的媒体播放器实例上设置 OnCompletionListener。当媒体文件播放完毕后,您可以在OnCompletion(MediaPlayer mp)回调函数中执行所需的操作。

mp.start() 之前添加以下代码片段应该适合您。

mp.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                seekBar.setProgress(0); // sets seekbar to initial position.
                toggleViews();//implement this function to toggle your play/pause button

            }

        });

您没有设置侦听器:seekbar.setOnSeekBarChangeListener(this)