public class MainActivity extends Activity {
private MediaPlayer musicPlayer;
private File musicFile;
//音乐文件存放地址
private String musicPath="/sdcard/ftpdata/Apologize.mp3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
musicFile = new File(musicPath);
//判断文件是否存在
if (musicFile.exists()) {
musicPlayer = MediaPlayer.create(this, Uri.parse(musicFile.getAbsolutePath()));
musicPlayer.start();
}else {
System.out.println("the file no exist!");
}
//监听音频播放完的代码,实现音频的自动循环播放
musicPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
musicPlayer.start();
musicPlayer.setLooping(true);
}
});
}
Anroid开发 之 音频文件的循环播放
最新推荐文章于 2023-05-07 18:05:31 发布