Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play a sound from res/raw

I m making an app which is supposed to play a few sounds with the 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 ?

like image 464
mremremre1 Avatar asked Aug 15 '13 14:08

mremremre1


2 Answers

There are two problems.

Problem 1

You cannot reference resources inside your projects /res/raw directory in this fashion. The file "/res/raw/sonar_slow.mp3" in your project directory is not stored in "/res/raw/sonar_slow.mp3" in your apk. 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

The following is wrong: it calls a static method that does not modify the 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. This is slightly modified from "Android Programming: The Big Nerd Ranch Guide". 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();
    }

}
like image 56
Brian Attwell Avatar answered Oct 23 '22 11:10

Brian Attwell


How to play a file with 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.

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. The sounds "hello", "goodbye" and "what" are in the res/raw directory:

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

Furthermore, I would strongly recommend using SoundPool instead of MediaPlayer, for better Performance and usability.

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 ;)

like image 23
Philipp Jahoda Avatar answered Oct 23 '22 11:10

Philipp Jahoda