我正在使用这种Media-Player-Looping概念:loop-concept 我不知道如何暂停这个循环。当我使用setNextMediaPlayer(null)时,它将在完成当前音频文件后暂停。但为时已晚。有人暗示我吗?
修改
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mp = new MediaPlayer();
mp2 = new MediaPlayer(); //workarround f gapless looping
setDisc("classic");
...
throttle = (Switch) findViewById(R.id.throttle);
throttle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setDisc(curTitle);
}
});
throttle.setChecked(false);
...
onoff = (ToggleButton) findViewById(R.id.onoff);
onoff.setChecked(false);
onoff.setOnClickListener(this);
...
}
public void setDisc(String s){
String tmp = "";
if(throttle.isEnabled()==true && throttle.isChecked())
tmp="2";
myUri = getUri(s+tmp);
curTitle=s;
curPow=tmp;
try {
mp.reset();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(context, myUri);
mp.prepareAsync();
mp2.reset();
mp2.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp2.setDataSource(context, myUri);
mp2.prepareAsync();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
setMyLoop();
play();
}
});
}
});
} catch (...) {
...Exception Handling...
}
}
public void setMyLoop(){
/*Gapless Looping*/
final AssetFileDescriptor afd = getResources().openRawResourceFd(getRawId(this, curTitle.concat(curPow)));
mp.setNextMediaPlayer(mp2);
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mediaPlayer.prepare();
} catch (Exception e) {
Toast.makeText(context, getResources().getString(R.string.error1), Toast.LENGTH_LONG).show();
}
mp2.setNextMediaPlayer(mp);
}
});
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mediaPlayer.prepare();
} catch (Exception e) {
Toast.makeText(context, getResources().getString(R.string.error2), Toast.LENGTH_LONG).show();
}
mp.setNextMediaPlayer(mp2);
}
});
}
public void play(){
if(onoff.isChecked()){
mp.start();
}
}
@Override
public void onClick(View v) {
if(v == onoff){
if(onoff.isChecked()==false){
mp.setNextMediaPlayer(null);
mp2.setNextMediaPlayer(null);
}
else{
mp.setNextMediaPlayer(mp2);
mp2.setNextMediaPlayer(mp);
mp.start();
}
}
...
}
答案 0 :(得分:0)
我明白了:
/*Media Player stopping*/
public void mpAnhalten(){
if(mp2.isPlaying()){
mp2.pause();
curMP=true;
}
else {
if(mp.isPlaying()){
mp.pause();
}
curMP=false;
}
}
/*Media Player starting*/
public void mpStarten(){
if(curMP==true)
mp2.start();
else
mp.start();
}
我将所有prepareAsync()更改为prepare()。现在它的工作。