我的活动中有一个媒体播放器,我有两个按钮可以播放下一个和之前的声音。 我希望当用户选中下一个或上一个按钮时声音停止播放并显示有关下一个或上一个声音的一些细节,并通过单击播放按钮开始播放,为此我使用了reset()。但是当我想播放下一个声音时,它就无法播放。
package com.example.listeninglevel1;
public class PagesActivity extends SherlockActivity implements Runnable, OnClickListener, OnSeekBarChangeListener{
SubMenu sm;
public static int pos=1, lastPage=5;
ImageButton next, back, nextNoneSelect, backNoneSelect;
String txstory;
TextView txview;
public static RelativeLayout relSeek, relMain;
private String[] title = new String[6];
private SeekBar seekBar;
private ImageButton startMedia;
private ImageButton pauseMedia;
public static MediaPlayer mp = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Sherlock_Light);
super.onCreate(savedInstanceState);
setContentView(R.layout.pages);
AudioControl();
next = (ImageButton) findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (pos < lastPage) {
pos++;
}
mp.reset();
pauseMedia.setVisibility(View.INVISIBLE);
startMedia.setVisibility(View.VISIBLE);
}
});
back = (ImageButton) findViewById(R.id.back);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (pos > 1) {
pos--;
}
mp.reset();
pauseMedia.setVisibility(View.INVISIBLE);
startMedia.setVisibility(View.VISIBLE);
}
});
}
public void AudioControl(){
seekBar = (SeekBar) findViewById(R.id.seekBar1);
startMedia = (ImageButton) findViewById(R.id.play);
pauseMedia = (ImageButton) findViewById(R.id.pause);
seekBar.setOnSeekBarChangeListener(this);
startMedia.setOnClickListener(this);
pauseMedia.setOnClickListener(this);
}
public void run() {
int currentPosition= 0;
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);
}
}
public void onClick(View v) {
pauseMedia.setVisibility(View.VISIBLE);
seekBar.setVisibility(View.VISIBLE);
startMedia.setVisibility(View.INVISIBLE);
if (v.equals(startMedia)) {
if (mp != null && mp.isPlaying()) return;
if(seekBar.getProgress() > 0) {
mp.start();
return;
}
int[] arr = {R.raw.sound1,R.raw.sound2,R.raw.sound3,R.raw.sound4,R.raw.sound5};
int snum = pos -1;
mp = MediaPlayer.create(PagesActivity.this, arr[snum]);
mp.start();
seekBar.setProgress(0);
seekBar.setMax(mp.getDuration());
new Thread(this).start();
}
if (v.equals(pauseMedia) && mp!=null) {
mp.pause();
pauseMedia.setVisibility(View.INVISIBLE);
startMedia.setVisibility(View.VISIBLE);
}
}
@Override
public void onPause() {
super.onPause();
if (mp!=null) {
mp.pause();
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(fromUser) mp.seekTo(progress);
}
}
答案 0 :(得分:2)
您可以尝试使用
mp.pause();
mp.seekTo(0);
这会暂停MP播放器并返回歌曲开始。下次单击时,它将从头开始播放Media Player。
if (mp.isPlaying()){
mp.pause();
mp.seekTo(0);
}
else{
mp.start();
}
}