首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在我的android应用程序的res / raw文件夹中播放mp3?

如何在我的android应用程序的res / raw文件夹中播放mp3?
EN

Stack Overflow用户
提问于 2018-04-02 00:59:44
回答 2查看 0关注 0票数 0

我在我的android应用程序的res / raw文件夹中有一个小的mp3。我试图从Eclipse的模拟器中运行它。它被认为是R文件中的一个资源,但是当我尝试准备/开始时,我的活动崩溃了!还有什么我需要改变,也许在清单?

MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this,R.raw.mysoundfile);

尝试{
mPlayer.prepare();
mPlayer.start();
} catch(IOException e){
//稍后处理
}
EN

Stack Overflow用户

发布于 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);
}
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100007893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档