【问题标题】:How do I play an mp3 in the res/raw folder of my android app?如何在我的 android 应用程序的 res/raw 文件夹中播放 mp3?
【发布时间】:2023-03-03 08:56:23
【问题描述】:

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

MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile); 尝试 { mPlayer.prepare(); mPlayer.start(); } 捕捉(IOException e){ // 稍后处理 }

【问题讨论】:

  • 把logcat的输出放在这里,否则我们无法识别错误。

标签: android media-player


【解决方案1】:

在启动活动时,即 onCreate 输入以下代码。

  public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);         
        setContentView(R.layout.main);

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

    }

当停止活动时,即 onDestroy 输入以下代码。

   public void onDestroy() {

    mPlayer.stop();
    super.onDestroy();

}

希望对你有帮助:)

【讨论】:

【解决方案2】:

您可能更喜欢使用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);
}

【讨论】:

  • 音池用于短时(5 秒)的声音,不适用于长音乐或声音。
  • @AtifMukhtiar:是的!只是问题中提到的 200KB MP3 的票。
  • 我有 14 秒的 174KB .ogg 的声音。但是声音播放 5 秒钟,声音是用 44KHz 创建的。然后我创建了一个 16KHz 的声音,我可以播放 10 秒。
  • 对不起,它不适合你。看起来有 1MB 的缓冲区限制。我想这将适用于从 MP3 或 OGG 解压缩后的声音。 stackoverflow.com/questions/13377604/…
【解决方案3】:

这是我在项目中使用的静态方法。 我将它添加到我的 Utils 类中:

    public static void playSound(final Context context, final SoundType type)
    {

            new Thread(new Runnable()
            {

                @Override
                public void run()
                {
                    MediaPlayer mediaPlayer = new MediaPlayer();
                    int resId = -1;
                    switch (type)
                    {
                    case INCOMING_NOTIFICATION:
                        resId=R.raw.noti_sound;
                        break;
                    case SEND_BETTING_SLIP:
                        resId=R.raw.slip_sent;
                        break;
                    case TRIVIA_RIGHT_ANSWER:
                        resId=R.raw.game_bonus;
                        break;
                    case TRIVIA_WRONG_ANSWER:
                        resId=R.raw.whistle_referee_trivia_bad_answer;
                        break;
                    }

                    if (resId != -1)
                    {
                        mediaPlayer = MediaPlayer.create(context, resId);
                        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        mediaPlayer.setLooping(false);
                        mediaPlayer.start();

                        while (mediaPlayer.isPlaying() == true)
                        {
                        }
                    }
                }
            }).start();

        }
}

现在我定义了一个 Enum (SoundType) 并将 mp3 文件放在 raw 文件夹下 res 文件夹。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-30
    • 2013-09-27
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 2011-12-20
    相关资源
    最近更新 更多