我是 Android 的新手,我刚刚在 Java 中创建了 Thread 对象,其中包含 run 方法。我认为可以在这里创建 MediaPlayer
对象并从 Android 目录: /data/data/application/files
播放一些 mp3 格式的歌曲>。我的目标是一首接一首地演奏。每个指定的时间段(如 20 秒)。
但是,我不清楚如何创建 MediaPlayer
对象(我应该将什么作为 context
传递?)
而且我不知道如何在指定的时间段内播放一首歌曲,然后再切换到下一首。
也许 MediaPlayer
不适合这样的任务?如果有任何建议,我将不胜感激。
最佳答案
我有这个闹钟代码
private void playSound(Context context, Uri alert, Boolean alarm) {
try {
final AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0
&& alarm == true) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
alarmisplaying = true;
} else {
mMediaPlayer.stop();
alarmisplaying = false;
}
} catch (IOException e) {
System.out.println("OOPS");
}
作为上下文,您可以传递 getApplicationContext(),如果您想停止播放,只需传递 false 作为警报(如果您开始播放声音,则传递 true)。
关于android - 从android中的线程播放mp3文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16299714/