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 :)
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"))
Where is the file directory res/raw?
You need to create new directory named raw
inside res
folder of your project. Then put your .mp3 file inside res>raw folder of your project
I am using MediaPlayer for custom sound. And This is Working fine for me. This is working for all devices.
private MediaPlayer player;
For play custom sound:
try {
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.siren);
player = MediaPlayer.create(this, uri);
player.setLooping(true); // This will play sound in repeatable mode.
player.start();
} catch (Exception e) {
e.printStackTrace();
}
For stop sound:
if (player != null)
player.stop();
This is working for me. Hope this will also helps you.
Android: playing audio files in /res/raw by file name
MediaPlayer player = new MediaPlayer();
String path = "android.resource://" + getPackageName() + "/raw" + filename;
try {
player.setDataSource(context, Uri.parse(path));
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
try it with or without the extention ".mp3"
hope i could help you
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.
Related Topics
Force Override Camera Image on Android
Android Adb Doesn't See Device
How to Avoid the Frame Layout Is Pushed Up When the Soft Keyboard Appears
How to Fix White Screen on App Start Up
Android No on Screen Keybord ( Only Voice )
Limitations on Opening Pdf File in Android
Full Screen Without Navigation & Status Bars
How to Dismiss Notification After Action Has Been Clicked
How to Hide Action Bar for Fragment
How to Disable Recyclerview Items from Clicking
How to Disable Spinner in Android and Enable It on Button Click
How to Connect Existing Android Studio Project to Existing Github Repository
How to Get Root Access on Android Emulator
Android Studio - How to Find Valid Certification Path to Requested Target
Retrofit 2.0 - How to Get Response Body for 400 Bad Request Error