All MP3 media from an array is not playing properly: Android Studio - android

I have created an activity, in which different mp3 sounds have to be played onFling a TextView. I created an array with all media required. Sometimes this plays all the media properly but most of the time it stop playing in between.
private int[] voice ={R.raw.media1,R.raw.media2,R.raw.media3,R.raw.media4,R.raw.media5,R.raw.media6,R.raw.media7,R.raw.media8};
media = MediaPlayer.create(BaseActivity.this, voice[num]);
media.start();

Updating here as I have got the solution. I was facing such issues as I was not releasing the recourses. After using below code, issue resolved:
private int[] voice ={R.raw.media1,R.raw.media2,R.raw.media3,R.raw.media4,R.raw.media5,R.raw.media6,R.raw.media7,R.raw.media8};
if(media!=null){
media.stop();
media.release();
media=null;
}
media = MediaPlayer.create(BaseActivity.this, voice[num]);
media.start();

Related

Set AudioStreamType for an audio file played from Resource (raw folder)

[First App] I am creating a sort of Alarm app that allows user to select alarm sound from either sd-card or app-supplied sounds. Since, the app essentially plays alarms, I want the volume to be 'alarm volume' of the device. I am able to achieve this for sd card sounds. But, I am unable to setAudioStreamType for raw resource sounds.
I am using following code :
MediaPlayer m_player = new MediaPlayer();
m_player.setAudioStreamType(AudioManager.STREAM_ALARM);
switch (bin_name) { //bin_name = various user selectable music files
default:
m_player = MediaPlayer.create(context, R.raw.blu);
break;
}
m_player.setLooping(true);
m_player.start();
My blu.mp3 plays at media volume only. Upon checking the documentation for MediaPlayer.create(Context context, int resid), I found this :
Note that since prepare() is called automatically in this method, you cannot change the audio stream type (see setAudioStreamType(int)), audio session ID (see setAudioSessionId(int)) or audio attributes (see setAudioAttributes(AudioAttributes) of the new MediaPlayer.
I also tried finding code samples for above method but none of them showed how to set AudioStreamType to AudioManager.STEAM_ALARM. I will accept answers with alternative ways that simply play the sound with infinite loop. How to achieve this ?
As the documentation you are referring to says, you must create and prepare the MediaPlayer yourself. Haven't tried with the STREAM_ALARM but I'm using following snippet to play on STREAM_VOICE_CALL
Uri uri = Uri.parse("android.resource://com.example.app/" + R.raw.hdsweep);
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
mMediaPlayer.setDataSource(context, uri);
mMediaPlayer.prepare();
mMediaPlayer.start()

I cannot change a video using MediaPlayer without reseting

I am working with streaming video and I want to change from one source to another dinamically.
First I set the video uri to the VideoView
view.setVideoURI(myUri);
And I know that I am capable of changing it afterwards by doing (this is in onPrepare method but it could go somewhere else where I have access to the MediaPlayer).
#Override
public void onPrepared(MediaPlayer mp)
{
Uri newUri = getOtherUri();
mp.reset();
mp.setDataSource(getApplicationContext(), newUri);
mp.prepare();
mp.start();
}
The thing is, I want to change the source without reseting the mediaPlayer (I do not want to disturb the user).
I tried to create a new VideoView with the new Uri and then change one object for the other, and likewise with the media player. However, none of that seems to work.
Is it possible to replace a video while it is playing in Android?
Any comments would be appreciated.
There is no option to reset the video without using mediaplayer.stopPlayback() or mediaplayer.reset. The reason is that; the previous object of the mediaplayer has to be released before you can play other video .
I want to change the source without reseting the mediaPlayer (I do not want to disturb the user).
Well, this cannot be achieved as the mediaplayer has to be reset. So there will be lag while changing videos. And to satisfy you, you can see these behavior in any videoplayer app like youtube or mxplayer.
So the only thing you can do is to show progressbar while loading or changing video.
Hope it helps. Cheeers.:)

Radio streaming doesnot match the original streaming in android?

I am using simple MediaPlayer to stream a radio streaming..
try {
MediaPlayer media = new MediaPlayer();
media.setAudioStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE);
media.setDataSource("http://indiespectrum.com:9000");
media.prepareAsync();
media.start();
}
catch(Exception e)
{
//Getting Exception
}
This works fine but the audio played is somewhat slow then the original audio streamed i.e if a song is originally at 2:15 the mediaplayer is playing at 2:05 .There is around 10 sec difference .Why is there a difference and what can be done to remove this?

sounds not playing with mediaplayer after playing some sounds

I'm developing an android application which is collection of 100 sound effects.
After I play for instance 25 of the sounds, I can't play anymore and I have to close the application and wait for some minutes then open the application and now I can play other sounds.
final MediaPlayer mp81 = MediaPlayer.create(MainActivity.this, R.raw.a81);
I play the sound using the below code:
mp81.start();
I stop the playing sound using the below code:
mp81.seekTo(0);
I also used stop() method but the problem were still existing.
is there any other method i have to add?
Please note: consider using SoundPool for playing short sounds.
Regarding your use-case: you initialize your MediaPlayer instance using the static create() method which means you create a new MediaPlayer object for each sound instead of reusing an existing instance and just changing the data source. This might negatively affect the performance of your app. I suggest that you create an array of paths to your sounds and then instantiate the MediaPlayer like this:
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(yourArray[x]);
mp.prepare();
mp.start();
} catch(Exception e){
e.printStackTrace();
}
Consult the MediaPlayer Document for more information.

Android : how to play multiple audios using single media player object

I need to create only one media player object Mediaplayer mp = new Mediaplayer();
Using this I need to play multiple audio files one after the other for that i am using handler
and getting the duration.
If i create multiple media player objects it shows error(1, -17)
I also need to display images related to audio files.
Did you rule out SoundPool?
http://developer.android.com/reference/android/media/SoundPool.html
It typically good for short audio clips.
Implement the OnCompletionListener and register it with your MediaPlayer instance.
After the media has been played out it will call this callback method onCompletion
void onCompletion(MediaPlayer mp){
//Here you stop it.
mp.stop();
//Reset the data source path to the new file
mp.setDataSource(<uri>);
mp.prepare(); // or mp.prepareAsync();
// start the mediaplayer after the prepare has completed.
}
Release the mediaplayer instance once you are done playing all the files.

Categories

Resources