I've got an app where Ii want to sound the alarm ringtone and raise the volume each second.
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
player = MediaPlayer.create(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
After initializing AudioManager and MediaPlayer, I've got a method which is executed each second when some circumstances occur. The method is the next one:
private void sound() {
if (!player.isPlaying()) {
try {
player.setLooping(true);
player.setVolume(5,5);
} catch (Exception e) {
e.printStackTrace();
}
player.start();
} else {
audioManager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION,AudioManager.ADJUST_RAISE,0);
}
}
As you can see, the first time the method is executed the sound begins, that works OK. But when is already started and I want to raise the volume, the adjustStreamVolume makes nothing.
Any help or advice?
Related
I have tried all methods mentioned in the following links
How to shut off the sound MediaRecorder plays when the state changes
Need to shut off the sound MediaRecorder plays when the state changes
but none of them work.
Anyone knows how to achieve that ?
Though I am too late to answer it. It may still help peoples who all are googling the same problem.
Before starting media recorder add following two lines of code ..
Its gonna mute phones sound..
//mute phone
AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mediaRecorder.start();
After starting media recorder wait one or two seconds and un-mute the phone, u may use following runnable...
new Thread(new UnmuterThread()).start();
//timer thread to un-mute phone after 1 sec
//This is runnable inner class inside your activity/service
class UnmuterThread implements Runnable{
#Override
public void run() {
synchronized (this){
try {
wait(1000);
} catch (InterruptedException e) {
} finally {
//unmute the phone
AudioManager audioManager1 = (AudioManager) context.getSystemService(AUDIO_SERVICE);
audioManager1.setRingerMode(AudioManager.RINGER_MODE_NORMAL); }
}
}
}
I'm developing a test app that when the automated testing process finishes an alarm sound is played to notify the operator.
This sound is a 3secs duration alarm ringtone, and I need to be played in loop until the user touches the phone screen.
This is the way I implement the mediaplayer:
mp = MediaPlayer.create(MainActivity.this, R.raw.alarm);
try {
mp.prepare();
mp.setLooping(true);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
I have defined the mp.setLooping that should make the sound play continuosly, but it doesn't.
Then, to stop the sound touching the screen:
#Override
public boolean onTouchEvent (MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
/*If sound is playing, stops*/
if (mp.isPlaying()) {
mp.stop();
}
return true;
}
return super.onTouchEvent(event);
}
So if mp.setLooping is not working, how should I make the sound play continuosly?
When you instantiate a MediaPlayer with create(), it is returned in a prepared state and you must not call prepare() on it. In your case, this is throwing an Exception, and therefore mp.setLooping(true) isn't being called.
mp = MediaPlayer.create(MainActivity.this, R.raw.alarm);
mp.start();
mp.setLooping(true);
I am having an odd issue where my audio file sometimes plays and sometimes does not play. The catch is that when it decides to not play, the DDMS gives me an:
E/MediaPlayer﹕ Should have subtitle controller already set
Because this is one-to-one with the music not playing, I have determined that this is probably the issue...
If the music is not playing and I hit the volume button it begins to play.
If I wait about 30 seconds of no-play, it begins to start again (not looping).
Whats going on here? I am on KitKat using
player = new MediaPlayer();
AssetFileDescriptor afd = null;
try {
afd = getAssets().openFd("Theme.mp3");
} catch (IOException e) {
e.printStackTrace();
}
try {
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} catch (IOException e) {
e.printStackTrace();
}
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
player.setLooping(true); //restart playback end reached
//player.setVolume(1, 1); //Set left and right volumes. Range is from 0.0 to 1.0
player.start(); //start play back
Looking at a previous discussion on StackOverflow, and the referenced Android commit where this was introduced, the code above might not completely initialize the MediaPlayer object.
The KitKat example code for media playback suggests that you should call:
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
immediately after you construct the MediaPlayer, and before you call its setDataSource method.
I had the same issue and I fixed it by adding the following right after instantiating MediaPlayer.
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (mp == mediaPlayer) {
mediaPlayer.start();
}
}
});
Previously I was implementing MediaPlayer.OnPreparedListener and overriding onPrepared() but it didn't work.
I hope this helps!
This should fix your problem (did for me): Replace the line that says "player.start()" following the rest of your code with an async callback like so:
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
This error is just a Log.e, not a real error. It shouldn't cause your player to not play, I'm guessing it's just because the player hadn't finished preparing when you try to call start().
E/MediaPlayer﹕ Should have subtitle controller already set
Its been a long time since I was working on this app. Here is what I ended up doing to get this to work. (Tested on KitKat and Lollipop). I think switching from MediaPlayer to APMediaPlayer was part of the trick.
#Override
public void onDestroy() {
if(player != null) {
player.release();
player = null;
}
super.onDestroy();
}
#Override
public void onStart() {
super.onStart();
if(player != null) {
player.start();
}
else {
player = new APMediaPlayer(this); //create new APMediaPlayer
player.setMediaFile("Theme.mp3"); //set the file (files are in data folder)
player.start(); //start play back
player.setLooping(true); //restart playback end reached
player.setVolume(1, 1); //Set left and right volumes. Range is from 0.0 to 1.0
}
}
#Override
public void onResume() {
super.onResume();
if(player != null) {
player.start();
}
}
set in manifest file may help you
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I want to play a notification sound and my problem it that the sound loops forever, when it should sound only once.
I've tried two ways:
notification.sound = Uri.parse("content://media/internal/audio/media/38");
and
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mMediaPlayer.setDataSource(this, Uri.parse("content://media/internal/audio/media/38"));
mMediaPlayer.setLooping(false);
mMediaPlayer.prepare();
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Log.v(Utils.TAG, "onprepared");
mp.start();
}
});
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.v(Utils.TAG, "end, we should release");
mp.stop();
mp.release();
}
});
In the second case, I never see the trace "end, we should release", the audio is played over and over and over again.
Any idea?
Thank you very much
UPDATE:
I've tried two devices and:
It loops forever on a Galaxy Nexus with ICS 4.0.4
It works fine on a HTC Hero 2.2.1
There are sounds with "built-in" loops which will play for ever. This is different to setting the MediaPlayer to looping. If you start playing a sound with a built-in loop, it will never finish. These are mostly ring tones (as opposed to alarm tones or notification tones). You can set the RingtoneManager to return only notification tones or alarm tones with myringtonemanager.setType(RingtoneManager.TYPE_ALARM|RingtoneManager.TYPE_NOTIFICATION)
which will exclude most looping sounds, but unfortunaley this is not guaranteed to exclude them all on any device. :-(
Therefore the solution of Tiago Almeida is a good work-around (and i've voted it up) though it will also truncate all sounds which have just a few (and no infinite) loops.
Try this:
try {
mp.setDataSource(uri.toString());
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.prepare();
mp.start();
mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
public void onSeekComplete(MediaPlayer mp) {
mp.stop();
mp.release();
}
});
} catch (Exception e) {
e.printStackTrace();
}
One more workaround for this problem
boolean soundPlayedOnce = true;
if(soundPlayedOnce){
mp.start();
soundPlayedOnce = false;
}
set this boolean to true right before you want to play sound
I am working in an android tab which has tabs activities, I have made tab groups for every tabs, in one activity i have media player for playing audio song. everything is fine except
i cann't control the volume of media player and I am not seeing volume control dialog of android device. here is my code,
mp=new MediaPlayer();
mp.setWakeMode(getParent(), PowerManager.PARTIAL_WAKE_LOCK); //for powermanagement
try {
if(GlobalConfig.notInternet==true)
{
mp.setDataSource("/sdcard/abc/"+file);
}else{
mp.setDataSource(url2);
}
mp.prepare();
} catch (IllegalArgumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mp.start();
please help me. thanks in advance
The following code should be used to managing media sound...
AudioManager mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
//It gives max volume of ur device
int max_volume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//It gives current volume of device
int current_volume=mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
//Here you can set volume
int progress=your set value;
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress, 0);