我在我的android应用程序的res / raw文件夹中有一个小的mp3。我试图从Eclipse的模拟器中运行它。它被认为是R文件中的一个资源,但是当我尝试准备/开始时,我的活动崩溃了!还有什么我需要改变,也许在清单?
MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this,R.raw.mysoundfile);
尝试{
mPlayer.prepare();
mPlayer.start();
} catch(IOException e){
//稍后处理
}
发布于 2018-04-02 10:07:25
你可能更喜欢使用SoundPool类。它可以在播放声音时减少延迟,并提供其他细节,例如当有太多不能一次播放的声音时,可以对声音进行优先排序。
从文档:
SoundPool是一组样本,可以从APK中的资源或文件系统中的文件加载到内存中。SoundPool库使用MediaPlayer服务将音频解码为原始16位PCM单声道或立体声流。这允许应用程序随压缩流一起提供,而不必在播放过程中承受CPU负载和解压延迟。
例如:
/**
* How many sounds can be played at once.
*/
private static final int MAX_SOUND_POOL_STREAMS = 4;
/**
* Modify this as part of your own priority scheme. Higher numbers mean higher
* priority. If you don't care, it's okay to use the same priority for every
* sound.
*/
private static final int NORMAL_PRIORITY = 10;
private int mySoundId;
@Override
public void setupContent() {
this.soundPool = new SoundPool(MAX_SOUND_POOL_STREAMS,
AudioManager.STREAM_MUSIC, 100);
this.mySoundId = this.soundPool.load(this.getApplicationContext(),
R.raw.mySound, 1);
}
@Override
private void playMySound() {
this.soundPool.play(this.mySoundId, 1, 1, NORMAL_PRIORITY, 0, 1);
}
https://stackoverflow.com/questions/-100007893
复制相似问题