I'd like to know if my Specific MediaPlayer instance is playing sound/music.
MediaPlayer has the isPlaying method but it returns true even if the player is buffering and not playing.
Any idea how to know if MediaPlayer really plays sound?
Thanks.
You can check the state of AudioPlayer by isMusicActive() method by respective method.
The code snippet is as below :
AudioManager ar = (AudioManager) getSystemService(AUDIO_SERVICE);
if(ar.isMusicActive()) {
//used
}
Related
I'm creating a quiz app just for learning, and I've got a problem.
In my mainactivity I have a media player. It plays music even going through other activities, and I have a mute button to stop the music. I also have a sound effect when I successfully hit the right answer (on a question which is in other activity). But I don't know how to mute this SFX along with the background music when I press the mute button. Any hints? ;)
If it has a way to mute my entire application will be so good. I found some ways to mute, but this way will mute the entire system. And it's
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
If you want to mute only the running apps sound, then you better do this:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); //Use your mediaplayer instance instead of this. Also maintain a single instance of mediaplayer for the entire app.
mp.start();
For Mute
mp.setvolume(0,0);
& Unmute or full volume
mp.setvolume(0,1);
Reference:
http://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float, float)
You can just use AudioManager.setStreamMute(). Feel free to use the code below.
//mute audio
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
//unmute audio
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
Try this. It may be of help to you.
AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
manager.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
In my app, I am trying to alter the volume of both(left/right headphone music streams) coming out of the device with a Seekbar.
The AudioManager Class can access the music stream coming from the device:
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxValue = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curValue = am.getStreamVolume(AudioManager.STREAM_MUSIC);
But the AudioManager .setStreamVolume() class can only change the volume coming from both ears together.
I figured out how to set the volume separately with the MediaPlayer class but how do I link the MediaPlayer class's .setDataSource() method to the stream coming out of the android device?
I looked everywhere and still haven't found an answer. Any help is appreciated!
You use setAudioStreamType() after you setDataSource(), but before calling
prepare
I play an audio file from a URL using MediaPlayer. The file plays fine, however after playback (if the app hasnt't been explicitly killed), alarm ringtone is silent. What do I need to clear/reset on end of playback to make this not happen?
Code for the file I am using to control the voicemail playback.
http://hastebin.com/ehijobuwoc.coffee
You can try getStreamVolume at start of playback then use setStreamVolume to set it back after your playback has finished.
http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume(int, int, int)
Ok have found something that works. Solves both the alarm issue and allows me to play the voicemail through the earpiece.
I needed to setMode back to what it was when I started, in onStop():
// set mode back to normal, and speakerphone back to true.
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(prevMode);
am.setSpeakerphoneOn(true);
and in init:
private void initMediaPlayer() {
// Make the media play through earpiece instead of through speaker(default)
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
prevMode = am.getMode();
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
am.setSpeakerphoneOn(false);
Thanks #DreadfulWeather for leading me to this answer, with some helpful questions.
I am developing an app. I need to play the music when i am in a call. But i am facing with the problem that whenever i try to play the music in a call it is not that much loud as it is when there is no call.
Is there any way to control the background music when in a call. My music volume becomes softer when i am in a call but it becomes louder as soon as i end the call.
final AudioManager mAudioManager = (AudioManager) ctx
.getSystemService(AUDIO_SERVICE);
final int originalVolume = mAudioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
mAudioManager
.setStreamVolume(
AudioManager.STREAM_MUSIC,
mAudioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
0);
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mAudioManager.setMode(AudioManager.MODE_NORMAL);
Class audioSystemClass = Class
.forName("android.media.AudioSystem");
Method setForceUse = audioSystemClass.getMethod(
"setForceUse", int.class, int.class);
// First 1 == FOR_MEDIA, second 1 == FORCE_SPEAKER. To go
// back to the default
// behavior, use FORCE_NONE (0).
setForceUse.invoke(null, 1, 1);
this is my code.
I see that you're trying to use the "force music streams to the loudspeaker"-method I posted in an answer to another question.
The problem is that during voice calls, music streams will follow the voice call's routing and ignore the FOR_MEDIA force flag. So what you perceive as lower volume is most likely the result of the music being played from the earpiece instead of from the loudspeaker.
To my knowledge, there's no way of routing the music to the loudspeaker during an ongoing call without also routing the voice call to the loudspeaker (i.e. using setSpeakerPhoneOn).
Depending on how your phone manufacturer/carrier implemented Android AudioPolicyService, some implementation lowers STREAM_MUSIC volume when MODE_IN_CALL/MODE_IN_COMMUNICATION is in session.
I have not tried this, but a likely workable solution is to use STREAM_VOICE_CALL instead of STREAM_MUSIC. Your audio stream should then play along with the in call audio stream just fine.
Hey I'm trying to play a R.raw file thru my earpiece instead of the main phone speaker on my app but everyhting I throw at it isn't working. WIht my current code... it just plays very light static through the earpiece and not the R.raw I want.
Here is my code below and I set it in the manifest: <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setSpeakerphoneOn(false);
am.setMode(AudioManager.MODE_IN_CALL);
MediaPlayer tellSecret = MediaPlayer.create(this, R.raw.secret);
tellSecret.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
tellSecret.start();
Any thoughts?
Try setting the Audio Manager and the Media Player modes to be AudioManager.STREAM_MUSIC.
do not use 'create' api for media player, instead use 'setDataSource' and 'prepare' apis seperately. Also call 'setAudioStreamType' api on mediaplayer instance with STREAM_VOICE_CALL before calling 'setDataSource'.
In the end after starting media player,
call setSpeakerphoneOn(false) on audio manager.