This is the code I'm using to create a notification sound. However, the media player plays the sound with a lower volume when the function is called for the first time. If this function is immediately called again, it then plays with the desired volume which is the stream volume.
Uri notification = RingtoneManager.getActualDefaultRingtoneUri(context,
RingtoneManager.TYPE_NOTIFICATION);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(context, notification);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
mMediaPlayer.setOnPreparedListener(new
MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.setVolume(1, 1);
if(!mMediaPlayer.isPlaying())
mMediaPlayer.start();
}
});
mMediaPlayer.setOnCompletionListener(new
MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mMediaPlayer.release();
}
});
mMediaPlayer.prepareAsync();
How to play the sound with the right volume at all times?
Related
Today I added a static MediaPlayer to my Activity.
It is static so I can stop it from outside the class it is written in.
But for some reason the MediaPlayer stops after the second or third loop without an error in Logcat.
This is my code:
public static MediaPlayer player;
public static void SoundPlayer(Context ctx,int raw_id){
player = MediaPlayer.create(ctx, raw_id);
player.setLooping(true); // Set looping
player.setVolume(100, 100);
player.start();
}
setLooping after start()
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
player.setLooping(true); // Set after start()
Can anyone give me a link of function to play audio file using user define time.. I am using handler but something is missing..
I am also done infinite time audio play but we need user defined times audio play.. Thanks...
Infinite time play audio is done using below function..
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shri_krushna_fast);
// mMediaPlayer.setVolume(0.2f, 0.2f);
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shri_krushna_fast);
mMediaPlayer.start();
}
});
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
}
I just wanted how to play audio x times in android using Thread??
I am updating my function but i was not got 100% on this..so plz help
my updated function is as bellow
public void Repeat_mantra()
{
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shri_krushna_fast);
// mMediaPlayer.setVolume(0.2f, 0.2f);
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shri_krushna_fast);
for(count=1;count<5;count++)
{
mMediaPlayer.start();
while(mMediaPlayer.isPlaying());
}
}
});
// mMediaPlayer.setLooping(true);
mMediaPlayer.start();
}
But this function is running then other clicks/or user interface are not appear because of "while(mMediaPlayer.isPlaying());" so any help me for how to update my function with Thread??
I want to play Sound Files from raw folder using MediaPlayer but I don't want to use MediaPlayer.create() method, since I want to play it multiple times and want to retain MediaPlayer object. I was successful in compiling the following code but it plays nothing and I can't hear any sound.
public void STARTSOUND(SOUND sound) throws IOException {
switch(sound) {
case SOUND_MENUBG:
Uri uri = Uri.parse("R.raw.mainmenu");
PLAYSOUND(uri);
break;
}
}
public void PLAYSOUND(Uri file) throws IOException {
mPlayerLoopSound.setDataSource(GameManager.getInstance().getCurrentActivity(),file);
mPlayerLoopSound.prepareAsync();
mPlayerLoopSound.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (!mPlayerLoopSound.isPlaying()){
mPlayerLoopSound.setVolume(1.0f, 1.0f);
//start the player
mPlayerLoopSound.start();
}
}
});
}
The onPrepared() method never gets called..
Am I doing anything wrong?
private void playButtonSound(final Context context, final int resourceId)
{
final MediaPlayer mediaPlayer = MediaPlayer.create(context, resourceId);
if (mediaPlayer != null)
{
mediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mediaPlayer)
{
if (mediaPlayer != null)
{
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
});
mediaPlayer.start();
}
}
Call this method like this.
playButtonSound(PopScreenActivity.this, R.raw.soundfilename);
First, hopefully you're initializing your MediaPlayer with:
mPlayerLoopSound = new MediaPlayer();
Next, make sure you call:
mPlayerLoopSound.setAudioStreamType(AudioManager.STREAM_MUSIC);
which:
Sets the audio stream type for this MediaPlayer. See AudioManager for a list of stream types. Must call this method before prepare() or prepareAsync() in order for the target stream type to become effective thereafter.
Then, in your OnPreparedListener(), when onPrepared() is called, notice the parameter it has: MediaPlayer mp. This is the MediaPlayer that is ready for playback, so you can use it:
#Override
public void onPrepared(MediaPlayer mp) {
if(!mp.isPlaying()) {
mp.setVolume(1.0f, 1.0f);
mp.start();
}
}
I've implemented a MediaPlayer using the following code but it's never calling onCompletion. I'm streaming mp3 from my LAN. Any idea what's wrong?
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
mMediaPlayer.start();
}
});
mMediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
// Nothing here gets executed
mMediaPlayer.start();
}
});
mMediaPlayer.reset();
mMediaPlayer.setDataSource(url);
mMediaPlayer.prepareAsync();
I'm building a radio application on android, I want know how to make my 'play' button clickable when my MediaPlayer is prepared?
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// Prepared
}
}