简体   繁体   English

从res / raw播放声音

[英]Play a sound from res/raw

I m making an app which is supposed to play a few sounds with the mediaPlayer. 我正在创建一个应用程序,它应该与mediaPlayer播放一些声音。 This is the code i use : 这是我使用的代码:

String[] name = {"sonar_slow","sonar_medium","sonar_fast"};
    String link = "/res/raw/" + name[state-1] + ".mp3";

    try {
        player.setDataSource(link);
        player.prepare();
        player.start();
    } catch(Exception e) {
        e.printStackTrace();
    }

I also tried this : 我也试过这个:

        if(state==1){
            player.create(this, R.raw.sonar_slow);
        }else if(state==2){
            player.create(this, R.raw.sonar_medium);
        }else if(state==3){
            player.create(this, R.raw.sonar_fast);
        }
        player.start();

But none of the above is working. 但以上都没有奏效。 My app is not crashing but the sound is not playing. 我的应用程序没有崩溃,但声音没有播放。 Any ideas ? 有任何想法吗 ?

There are two problems. 有两个问题。

Problem 1 问题1

You cannot reference resources inside your projects /res/raw directory in this fashion. 您无法以这种方式引用projects / res / raw目录中的资源。 The file "/res/raw/sonar_slow.mp3" in your project directory is not stored in "/res/raw/sonar_slow.mp3" in your apk. 项目目录中的文件“/res/raw/sonar_slow.mp3”未存储在apk中的“/res/raw/sonar_slow.mp3”中。 Instead of the following: 而不是以下:

MediaPlayer mp = MediaPlayer.create(this);  
mp.setSource("sonar_slow");

You need to use 你需要使用

MediaPlayer mp = MediaPlayer.create(this, R.raw.sonar_slow); 

Problem 2 问题2

The following is wrong: it calls a static method that does not modify the player . 以下是错误的:它调用一个不修改player的静态方法。

player.create(this, R.raw.sonar_slow); 

You should instead call 你应该打电话

player = MediaPlayer.create(this, R.raw.sonar_slow);

Full solution 完整解决方案

Below is a reusable AudioPlayer class that encapsulates MediaPlayer. 下面是一个可重复使用的AudioPlayer类,它封装了MediaPlayer。 This is slightly modified from "Android Programming: The Big Nerd Ranch Guide". 这在“Android编程:大书呆子牧场指南”中略有修改。 It makes sure to remember to clean up resources 它确保记住清理资源

package com.example.hellomoon;

import android.content.Context;
import android.media.MediaPlayer;

public class AudioPlayer {

    private MediaPlayer mMediaPlayer;

    public void stop() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    public void play(Context c, int rid) {
        stop();

        mMediaPlayer = MediaPlayer.create(c, rid);
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                stop();
            }
        });

        mMediaPlayer.start();
    }

}

How to play a file with MediaPlayer: 如何使用MediaPlayer播放文件:

MediaPlayer mp = MediaPlayer.create(this, R.raw.mysound); // sound is inside res/raw/mysound
mp.start();

This is a simple example of how to play a sound with the Android MediaPlayer . 这是如何使用Android MediaPlayer播放声音的简单示例。

You have two buttons hat each play a different sound. 你有两个按钮帽子,每个发出不同的声音。 The selecting of the sound and actually playing it is done in the manageSound() method. 选择声音并实际播放是在manageSound()方法中完成的。 The sounds "hello", "goodbye" and "what" are in the res/raw directory: res / raw目录中的声音“hello”,“goodbye”和“what”:

MediaPlayer mp        = null;
String hello         = "Hello!";
String goodbye        = "GoodBye!";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button buttonHello = (Button) findViewById(R.id.idHello);
    buttonHello.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            managerOfSound(hello);
        } // END onClick()
    }); // END buttonHello


    final Button buttonGoodBye = (Button) findViewById(R.id.idGoodBye);
    buttonGoodBye.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            managerOfSound(goodbye);
        } // END onClick()
    }); // END buttonGoodBye
} // END onCreate()


protected void manageSound(String theText) {
    if (mp != null) {
        mp.reset();
        mp.release();
    }
    if (theText.equals(hello))
        mp = MediaPlayer.create(this, R.raw.hello);
    else if (theText.equals(goodbye))
        mp = MediaPlayer.create(this, R.raw.goodbye);
    else
        mp = MediaPlayer.create(this, R.raw.what);
    mp.start();
}

Taken from here: http://www.badprog.com/android-mediaplayer-example-of-playing-sounds 取自这里: http//www.badprog.com/android-mediaplayer-example-of-playing-sounds

Furthermore, I would strongly recommend using SoundPool instead of MediaPlayer, for better Performance and usability. 此外, 我强烈建议使用SoundPool而不是MediaPlayer,以获得更好的性能和可用性。

http://developer.android.com/reference/android/media/SoundPool.html http://developer.android.com/reference/android/media/SoundPool.html

Please also check if your sound is muted - I know this sounds stupid, but it happens to the best of us ;) 请检查你的声音是否静音 - 我知道这听起来很愚蠢,但它发生在我们最好的人身上;)

You need to do it like this : 你需要这样做:

 try{
      mp.prepare();
    } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }           
    mp.start();

确保您在文件完成准备时才进行播放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM