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); }
}
}
}
Related
As described in documentation, the automatic ducking feature has been introduced in Android 8.0. Ducking means that if your music application has been interrupted by some short sound (like a notification, for ex.), your application will continue playing music, but music volume will be temporary lowered while short sound is playing.
I use a text-to-speech engine to read long articles, and want it to behave similar to music player, i.e. I want it to be automatically ducked by the system in android 8.0
I've got ducking to work fine without any additional code on Android 8.0 for music playing, but not for text-to-speech playing.
Here is a sample code
public class PlayerService extends Service {
//private MediaPlayer mp;
TextToSpeech tts;
#Override
public void onCreate() {
super.onCreate();
//...
//create foreground notification stuff omitted...
//...
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
am.requestAudioFocus(listener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
/*
mp = MediaPlayer.create(this, R.raw.music);
mp.setLooping(true);
mp.start();
*/
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tts.setAudioAttributes(
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
);
}
tts.speak("IF you happen to have read another book about Christopher Robin, you may remember that he once had a swan (or the swan had Christopher Robin, I don't know which) and that he used to call this swan Pooh. That was a long time ago, and when we said good-bye, we took the name with us, as we didn't think the swan would want it any more. Well, when Edward Bear said that he would like an exciting name all to himself, Christopher Robin said at once, without stopping to think, that he was Winnie-the-Pooh. And he was. So, as I have explained the Pooh part, I will now explain the rest of it. ",
TextToSpeech.QUEUE_ADD, null);
}
});
}
#Override
public void onDestroy() {
tts.stop();
/*
mp.stop();
mp.release();
*/
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
am.abandonAudioFocus(listener);
super.onDestroy();
}
private AudioManager.OnAudioFocusChangeListener listener = new AudioManager.OnAudioFocusChangeListener() {
#Override
public void onAudioFocusChange(int focusChange) {
//nothing to do here
}
};
}
If I'd remove text-to-speech stuff and uncomment MediaPlayer lines, ducking will work fine for music playing. But if using text-to-speech as presented, no ducking is happening - during the notification sound text-to-speech continues playing as usually, and no volume change is performed.
I want ducking (volume change) to happen also if I use text-to-speech. What am I doing wrong?
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?
The setVideoURI method of VideoView in Android seems to be blocking the UI thread. As soon as I call this method, the UI get's laggy, even on fast devices. Is there a way to improve performance here?
The only other thread with that topic I could find here:
https://groups.google.com/forum/#!topic/android-developers/eAAEAEDcksM
but it's quite old and doesn't have a satisfying answer.
VideoView.setVideoURI() starts a new thread for media playback, but it is the media decoding part which causes extra delay.The only solution that might be ok for you is using some NDK hacks, but doesn't worths for me
What i did was place the setVideoUri() method into a handler with a looper i.e.
new Handler(Looper.myLooper()).post(new Runnable(){
#Override
public void run(){
videoview.setVideoUri("uri");
}
});
this runs the code outside the main UI thread, but keeps it in a looper so the code can be executed without throwing an exception
I have found that VideoView not blocking UI thread. Actually in the receiver of "android.media.VOLUME_CHANGED_ACTION" blocking UI thread.
My problem code:
public void setVolume(int volume) {
try {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
ivVoice.setTag(volume != 0);
updateVoiceImage();
} catch (Exception e) {
e.printStackTrace();
}
}
setVolume called from volume receiver , it has called 1000+ times when playing a 5s mp4.
audioManager.setStreamVolume takes too long in main thread.
solution codeļ¼
public void setVolume(int volume) {
try {
//if volume not changed , do nothing.
if(volume != lastVolume){
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,volume,0);
ivVoice.setTag(volume != 0);
updateVoiceImage();
lastVolume = volume;
}
} catch (Exception e) {
e.printStackTrace();
}
}
So, please check your volume receiver. Maybe it can helps you.
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoURI(Uri.parse(url));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
i used this code may it will help you
From the Developer Guide I took this simple example:
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start(); // no need to call prepare(); create() does that for you
But there is no sound!
Any ideas?
Check your media volume in your mobile (or) below code for playing sound file :
This is Worked for me:
public void play()
{
Thread playThread = new Thread() {
public void run()
{
mediaPlayer = MediaPlayer.create(VastuActivity.this, R.raw.click);
mediaPlayer.start();
}
};
playThread.start();
}
call the method play();
You got everything right. The only reason it may not work is some problem with the sound, may be the file you are using is not supported(in that case you should see some exceptions thrown in all messages tab) or it's just your device
Ok well I have been fighting with this for a while now and have soundboard apps that I havn't ran into this problem in. I am developing a widget that allows for a sound to be played when a button is pressed on the widget.
I am assuming I am just not using the setOnPreparedListener properly but basically what is happening is that some of my sounds play correctly and more likely than not the sounds will cut off the last part noticeable by word or sound effect missing. Below is my code please let me know if you have any idea what's wrong with the code or post a working revision.
Thanx in advance.
public int onStartCommand(Intent intent, int flags, int startId) {
/** Get our place holder for quotes & sounds. */
int s = UpdateService.getQuoteNumber();
/** Check if a sound is playing if so... stop and reset it. */
if (mp != null && mp.isPlaying()){mp.stop();mp.reset();}
/** Create a new mediaplayer and set looping. */
mp = MediaPlayer.create(this, SoundsClicked.randomSound[s]);
mp.setLooping(false);
/** Try to prepare the sound/mediaplayer, if not report error & type. */
try {mp.prepare();}
catch (IllegalStateException e) {e.printStackTrace();Log.e(Integer.toString(s), "IllegalStateException");}
catch (IOException e) {e.printStackTrace();Log.e(Integer.toString(s), "IOException");}
/** Check if the sound is prepared, if so play it. */
mp.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
while (mp.isPlaying()) {};
}
});
return flags;}
This is called from a service (obviously from the onStartCommand) but figured I would throw that out there for those not familiar with some of the methods.
If you're using Android 2.2, there are apparently some problems with the MediaPlayer cutting off the last X% of at least some audio files. I'm running in to that problem with the app I'm currently working on. I haven't found if there is a solution or not.