How to Play an Mp3 in the Res/Raw Folder of My Android App

How to play the audio files directly from res/raw folder?

add this code in onItemClickListener.

listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {
TextView txtView=(TextView)view.findViewById(R.id.txt_view);
String fname=txtView.getText().toString().toLowerCase();

int resID=getResources().getIdentifier(fname, "raw", getPackageName());

MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.start();
}
});

How do I play an mp3 in the res/raw folder of my android app?

When starting the activity i.e on onCreate put the following code.

  public void onCreate(Bundle savedInstanceState) {

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

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

}

When stopping the activity i.e on onDestroy put the following code.

   public void onDestroy() {

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

}

Hope it helps :)

play mp3 file in resource folder not working

It will help more if you can post the StackTrace in your question.

But, as per the information in your question, the below code should work for playing the media file from the raw resource folder.

If you use the create() method, prepare() gets called internally and you don't need to explicitly call it.

MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.notify);
mediaPlayer.start();

But, the point to consider is that prepare() generally throws an IllegalStateException, and in your case, you are getting an IOException. So it would be worth checking if the file is in fact present in raw folder and/or the file is corrupt.

How to play a different sound file from resraw

You can do something like this :

var a1 = R.raw.a1
var a2 = R.raw.a2
var a3 = R.raw.a3

Or you can do it with JAVA like this :

int setMusic(String mMusic){
return this.getResources().getIdentifier(mMusic, "raw", this.getPackageName());
}

and call it like this :

mp = MediaPlayer.create(this, setMusic("a1"))

or

mp = MediaPlayer.create(this, setMusic("a2"))

Play a sound from res/raw

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

}

Android: OnClick play random .mp3 from /res/raw file

You have a few problems here.

First, calling toString() on a Field will give you a string representation of the object instance, such as "public static final int com.lena.button.R$raw.laptopkeyboard1", which is not very useful. Presumably, you want getInt().

Second, a raw resource is not an asset, and so you do not use openFd(). Instead, use the static create() method to create your MediaPlayer instance, passing in the int you get from getInt() on your Field.

Third, reflection is slow. Please do not do it more than once. Use R.raw.class.getFields() once, caching the results. Or, better yet, consider not using reflection at all, and instead using your own literal Java int[]:

static int[] SOUNDZ={R.raw.boom, R.raw.chaka, R.raw.laka};

(substituting in your own sound resources, of course)



Related Topics



Leave a reply



Submit