gpt4 book ai didi

android - 循环播放音频文件的问题

转载 作者:行者123 更新时间:2023-12-03 00:09:56 29 4
gpt4 key购买 nike

我怎样才能从mp.setLooping(true)完成的重复值。我可以使用OnCompletionListener来获取重复次数,但是它在循环之间会产生1秒的延迟,这在面向音乐的应用中很烦人。 OnCompletionListener循环不如mp.setLooping(true)平滑。

有什么建议么 ?预先感谢。

延迟1秒或循环之间有间隔的代码:

public class MainActivity extends Activity {
MediaPlayer mp1;
Button play;
int maxCount=10,n=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button) findViewById(R.id.button1);
mp = MediaPlayer.create(MainActivity.this, R.raw.soundfile);
play.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mpplay();
}
});

}
private void mpplay() {
mp1.start();
mp1.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp1) {
if (n <= maxCount) {
mp1.start();
n++;
if (n >= maxCount) {
n = 1;
mp1.stop();
}
}
}
});
}}

最佳答案

试试看:

MediaPlayer mp1;
double play_times = 10;
int sound_length;
Handler handler;
boolean sound_playing = false;
int times_played = 0;

public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.home);
handler = new Handler();
mp1 = MediaPlayer.create(this, R.raw.soundfile);
sound_length = mp1.getDuration();
mpplay();

}
private void mpplay() {
times_played = 0;
mp1.setLooping(true);
mp1.start();
sound_playing = true;
handler.postDelayed(loop_stopper, (int)(sound_length*(play_times-.5)));
handler.postDelayed(counter, sound_length);

}
private Runnable loop_stopper = new Runnable() {
public void run() {
sound_playing = false;
mp1.setLooping(false);
}
};
private Runnable counter = new Runnable() {
public void run() {
if (sound_playing) {
times_played++;
handler.postDelayed(counter, sound_length);
}
}
};

handler.postDelayed(loop_stopper,(int)(sound_length *(play_times-.5)));

该行将在最后一个所需的循环中途停止循环。由于这只是意味着它不会继续循环播放,因此声音文件在上一次重复播放时仍将完全播放。

关于android - 循环播放音频文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16381721/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com