Accessing an audio file in the RAW folder - android

I have created a new Java file within my project and i am trying to access a war audio file in my Raw folder. Does anyone have an idea what i am doing wrong?
MediaPlayer mediaPlayer;
Context context;
public Audio()
{
int resID = context.getResources().getIdentifier("morse","raw",context.getPackageName());
context.getResources().openRawResource(resID);
mediaPlayer = MediaPlayer.create(this.context, resID);
}

You have failed to start the media player. Try the following code,
MediaPlayer mediaPlayer;
Context context;
public Audio()
{
int resID = context.getResources().getIdentifier("morse","raw",context.getPackageName());
context.getResources().openRawResource(resID);
mediaPlayer = MediaPlayer.create(this.context, resID);
//start the media player
mediaPlayer.start();
}
Hope it helps!!!...

Instead of this:
int resID = context.getResources().getIdentifier("morse","raw",context.getPackageName());
context.getResources().openRawResource(resID);
mediaPlayer = MediaPlayer.create(this.context, resID);
Try This:
mediaPlayer = MediaPlayer.create(Your_Activity_Name.this,
R.raw.Audio_File_Name);
mediaPlayer.start();

Rename your audio file to .mp3 or .wav or some other audio formats.
Add your audio file to raw folder in res, res/raw/audio.mp3.
Clean your project.
Use the following:
mp = MediaPlayer.create(this, R.raw.audio);
mp.start();

Related

MediaPlayer with setAudioStreamType(AudioManager.STREAM_RING) not playing android

I am working on an Audio calling app and trying to play .mp3 file stored in raw folder.
I wanted this this file to be played as an Ringtone
For this I used mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
When I use this stream the .mp3 is not played (or may be playing I am not able to hear the sound )
If I don't specify any AudioStreamType , then the file is played as an MUSIC stream , which I don't want
Note: Volume of every type (Ringtone,Notification,Call,Music) is set to full in my device
My code
public MediaPlayer playRingtone(int fileName, boolean loopRingtone) {
mediaPlayer = MediaPlayer.create(mContext, fileName);
if (android.os.Build.VERSION.SDK_INT >= 21) {
AudioAttributes.Builder b = new AudioAttributes.Builder();
b.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
b.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN);
mediaPlayer.setAudioAttributes(b.build());
} else {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
}
mediaPlayer.setLooping(loopRingtone);
mediaPlayer.start();
mediaPlayer.setOnErrorListener(this);
mediaPlayer.setOnCompletionListener(this);
return mediaPlayer;
}

play mp3 file in resource folder not working

I want to play an mp3 file in my res/raw folder.
But i get error as "error (1, -2147483648)" and IOException on mp.prepare()
My code
try {
MediaPlayer mPlayer = MediaPlayer.create(NavigationHome.this, R.raw.notfy);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
I also tried with
try {
mp.setDataSource(NavigationHome.this, Uri.parse("android.resource://com.hipay_uae/res/raw/notfy"));
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
Another solution that I tried
AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
MediaPlayer player = new MediaPlayer();
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
player.prepare();
player.start();
These too didn't work for me.
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.
Try to initialize your media player before preparing it or setting data source to it
Play From external directory
String filePath = Environment.getExternalStorageDirectory()+"/folderName/yourfile.mp3";
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
mediaPlayer.start()
From raw folder
MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.song);
mediaPlayer.start();
Try this
String fname="your_filename";
int resID=getResources().getIdentifier(fname, "raw", getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.start();

Store audio files in an array

I am working on an offline android quiz app and i want to store audio files in an array and play audio for different questions in a quiz. Can array be used to store audio files ? If Yes,How?
Thanx to all who tried, i solved this probllem myself:
int [] songs;
MediaPlayer mediaPlayer;
int current_index = 0;
mediaPlayer = MediaPlayer.create(this, songs[current_index]);
Button play_button = (Button)this.findViewById(R.id.play);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "Playing sound...");
mediaPlayer.start();
current_index ;
}
});
Store the audio file in the array as shown and put the audio files in raw folder
res/raw
Array of audio
int[] clips= { R.raw.button_a, R.raw.button_2, R.raw.button_3, R.raw.button_4, R.raw.button_5, R.raw.button_6 };
Pick up the audio from this array

Play a file from the raw folder using Android mediaPlayer, using its resourceId

In Android, I can use MediaPlayer.create(context, R.raw.myFileName) to create an instance of the mediaPlayer, using a resource from the raw/ folder, and I can then use .start() to get that file to play. Later, I can use the various signatures for .setDataSource() to change the file that I want to play.
I can obtain the resourceId for a given file in the raw/ folder, using:
int resourceId = activity.getResources().getIdentifier("myFileName", "raw", activity.getPackageName());
Is it possible to use this integer resourceId to start playing that file instead of the current one? Or do I have to determine the full path to the file res/raw/myFileName.mid in order to change the track?
I am hoping that the solution will be something like this, with a real method instead of my invented equivalentToSetDataSourceUsingAResourceId() method name.
Resources resources = activity.getResources();
String packageName = activity.getPackageName();
int white = resources.getIdentifier("white", "raw", packageName);
int black = resources.getIdentifier("black", "raw", packageName);
MediaPlayer mediaPlayer = MediaPlayer.create(activity, white);
mediaPlayer.start();
// ... and some time later...
mediaPlayer.reset();
mediaPlayer.equivalentToSetDataSourceUsingAResourceId(black);
mediaPlayer.prepare();
mediaPlayer.start();
An alternative would be to destroy the current mediaPlayer instance and create a new one each time the sound file needs to change:
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
mediaPlayer = MediaPlayer.create(activity, black);
mediaPlayer.start();
This does not seem elegant to me.
According to MediaPlayer documentation setDataSource API's allowed only on fresh instance (IDLE state) of MediaPlayer. Which means you have to release() and recreate MediaPlayer from scratch if you wanna use only one instance at time.
Try do like this. Just pass id of your raw resource:
mMediaPlayer = MediaPlayer.create(this, R.raw.whatever);

How to play a recorded audio file?

I'm using MediaRecorder class to record an audio file and I used
final String MEDIA_OUTPUT_FILE = "MyOutPutFile";
mediaRecorder.setOutputFile(MEDIA_OUTPUT_FILE);'
to give a name to my output file.
I also know how to Play an Audio File from Resources:
MediaPlayer mpRes = MediaPlayer.create(getApplicationContext(), R.raw.audiofile);
mpRes.start();'
My Question Is, Now, how to play the recorded file while I dont know where my MEDIA_OUTPUT_FILE is saved?
Instead of using constant as argument for mediaRecorder.setOutputFile() use a variable like below.
private String filePath;
filePath = Environment.getExternalStorageDirectory().getAbsolutePath();
filePath += "/myrecording.mpeg";
Then pass the filePath variable as the argument as follow;
mediaRecorder.setOutputFile(filePath);
Then you can play the recorded file as follows;
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.start();
That's it.

Categories

Resources