How to play music files in raw folder continuously - android

Creating music player related application,In raw folder having 5 mp3 files.want to play continuously and individually ,
else if (theText == song3) {
mediaPlayer = MediaPlayer.create(this, R.raw.hiii);
} else if (theText == song4) {
mediaPlayer = MediaPlayer.create(this, R.raw.hiiii);
}
This is how i am playing individual songs but how to play 5 songs continuously.Help me.

1.make an array of id if songs in raw folder.
int arr[]={R.id.hiii,R.id.Hiiii,...........}
2.use this array in your code
mediaPlayer=MediaPlayer.create(this, arr[i]);
3.implement setOnCompletionListener(Context); to start another song after one is over and loop it

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

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

background sound during call in android

I want to play a sound automatically when I call to somebody
but I don't know the way for this.
It is possible because we have some application that play some noise (traffic and ...) to background of call but I could not find a code or something for this
example of sound play in background
this is a code to play music
MediaPlayer player;
AssetFileDescriptor afd;
try {
// Read the music file from the asset folder
afd = getAssets().openFd(“home.mp3″);
// Creation of new media player;
player = new MediaPlayer();
// Set the player music source.
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),afd.getLength());
// Set the looping and play the music.
player.setLooping(true);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
but can we use this during call?

How can I put a song to repeat in an android application?

I put in background of my android application a song. I don't know how much time the application is open. And I want to put this song to repeat. My code is:
MediaPlayer mySong;
mySong = MediaPlayer.create(X_0Activity.this, R.raw.tj);
mySong.start();
Uri mediaUri = createUri(context, R.raw.media); // Audiofile in raw folder
Mediaplayer mPlayer = new MediaPlayer();
mPlayer.setDataSource(context, mediaUri);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.prepare();
mPlayer.setLooping(true); // for repeat song
mPlayer.start();
mySong.setLooping(true) // repeat Song
mySong.start(),
And now you are ready with repeat mode on.
Use SoundPool, you can easily loop it any time you want! Here is a very good example: Play sound with SoundPool
In the
spool.play(soundID, volume, volume, 1, 0, 1f);
the number 0 represents the number you want to repeat the song. For infinite loop the suitable value is -1.

Categories

Resources