How lower music volume on playing my app voice guidance in Android and after that increase music again? - android-mediaplayer

I am working on an android map app that like Waze or any other app uses voice guidance to guide people to their destination.
My problem is that I want if user play music with his/her phone music volume decrease and voice guidance play with upper volume and after finishing guiding user music volume increase again and get focus of speakers?
Is this can be down I searched a lot and saw some implementation of it but none of them helped me in this special condition.
Thanks all of you in advance.

AudioManager am = (AudioManager)getContext().getSystemService(AUDIO_SERVICE);
mp = new android.media.MediaPlayer();
mp.setDataSource("/data/data/" + getContext().getPackageName() + "/rasa.wav");
mp.prepare();
mp.start();
AudioManager.OnAudioFocusChangeListener focusChangeListener =
focusChange -> {
};
int result = am.requestAudioFocus(focusChangeListener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mp = new android.media.MediaPlayer();
mp.setOnCompletionListener(mediaPlayer -> {
am.abandonAudioFocus(focusChangeListener);
});
mp.setDataSource("/data/data/" + getContext().getPackageName() + "/rasa.wav");
mp.prepare();
mp.start();
}

Related

Mute iOS microphone input with ReactNative

I am using Agora's ReactNative library for a group calling project, I need the user to be able to mute/unmute his phone microphone. Currently there is only a function to muteLocalAudioStream which does mute the whole stream, that includes the background sounds, as the streamer can add background sound to the call.
For Android I managed to mute/unmute the microphone with the below hack:
#ReactMethod
public void muteMic(){
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
if (audioManager.isMicrophoneMute() == false) {
audioManager.setMicrophoneMute(true);
} else {
audioManager.setMicrophoneMute(false);
}
}
However, I couldn't do it for iOS, I appreciate your help on this.
You can change recording level with a call to
// volume: [0,100]
RtcEngine.adjustRecordingSignalVolume(volume)

Video event issue: User touching screen before time to play

Hi this question is for anyone who may be able to show me how to resolve this user issue. I have a script that plays a video with the user able pause and resume on touch. This was easy enough to do and works fine. At a certain point the script pauses the video for the user for a set time so the user has time to read the information on the screen. The intent is for them to be able to to resume playing the video after reading the information. If the user waits long enough, the touch to resume works fine. But, because some users read faster than others it is obvious I need to detect the touch even when the video was paused by the script - this is the part I need help with. Currently, if the video is paused by the script and the user touches the screen before the set time for the pause expires, the play button will show but pressing it to play will not resume the script until that time expires. I presume I need a listener inside the function the script used to pause the video. I am not quite sure the best way to handle that. Here is a snippet of my approach so far:
var myVideo = document.getElementById("myVideo");
function playPause() {
var el = document.getElementById("playButton");
if (myVideo.paused) {
myVideo.play();
el.className ="";
} else {
myVideo.pause();
el.className = "playButton";
}
}
myVideo.addEventListener("click", playPause, false);
var pause42 = function(){
if(this.currentTime >= 42 && !myVideo.paused) {
this.pause();
// remove the event listener after you paused the playback
this.removeEventListener("timeupdate",pause42);
}
};
myVideo.addEventListener("timeupdate", pause42);
Any help is appreciated!
FYI problem solved. Posting the answer here if it helps others.
var pause42 = function(){
if(this.currentTime >= 15 && !myVideo.paused) {
this.currentTime = 42;
this.pause();
// remove the event listener after you paused the playback
this.removeEventListener("timeupdate",pause42);
}
};
myVideo.addEventListener("timeupdate", pause42);

setOngoing(true) is not working in Oreo+ Notifications

I am creating a custom notification with channel_id and start it as foreground. I have background music playing .. My problem is when user swipe notification it disappears. I have also setOngoing(true). i want notification to stay when music is playing.
NOTE: It only happens in Oreo+. in lower versions it is working fine.
public void CustomNotification() {
remoteViews = new RemoteViews(getPackageName(), R.layout.player_noti_layout);
notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("default",
getString(R.string.player_channel),
NotificationManager.IMPORTANCE_LOW);
channel.enableVibration(true);
channel.enableLights(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channel.setDescription("Notification, Play/pause & Next/Prev");
notificationmanager.createNotificationChannel(channel);
}
builder = new NotificationCompat.Builder(this, "default");
Notification foregroundNote;
// Set icon
foregroundNote = builder.setSmallIcon(R.drawable.ic_radio)
.setLargeIcon(R.drawable.cool_1)
// Set ticker message
.setTicker(getResources().getString(R.string.app_name))
// Dismiss notification
.setAutoCancel(false)
.setOngoing(true)
.setContent(remoteViews)
.setContentTitle("title").
setContentText("text")
.build();
foregroundNote.flags |= Notification.FLAG_ONGOING_EVENT;
foregroundNote.flags |= Notification.FLAG_NO_CLEAR;
foregroundNote.contentView = remoteViews;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
startForeground(2, foregroundNote);
} else
notificationmanager.notify(2, foregroundNote);
}
In a few words, that's due to fragmentation and customization. Probably that phone has some kind of setting to configure that behavior. As someone said one day: Some phones are just -trash-. Don't waste your time trying to fix an issue for every possible phone.

How many load states of Titanium video player's have and the meanings each of them?

guys! I need your help! Recently, I'm trying to add the loadstate event listener to the video player, however, when I'm console the event evt to check the load state I found there're different kinds in IOS and Android platform and unexpected, the state meanings are different too. As far as I known,
Android load states video player only have two kinds: 0 and 1
① the 0 load state seems like preloading
② the 1 load state seems like can play normally
IOS load states of video player have four kinds: 1, 2, 3 and 5
I only know that the state 5 comes when the network is not connect
Here is my code:
var activityIndicator = Ti.UI.createActivityIndicator({
visible: false,
width: Ti.UI.SIZE,
height: Ti.UI.SIZE,
message: 'loading...'
});
videoPlayer.addEventListener('loadstate', function(evt){
console.info('---------load state---------');
console.info(evt.loadState);
if(evt.loadState == 5){
activityIndicator.show();
}
});
I had checked the API, however there's no answer. Anyone of you have a good knowledge about it? Thanks in advance!
Welcome.
--EDITED--
OK, maybe I misunderstood the question, sorry. In the latest version of Titanium SDK, the API talks with the native iOS class MPMoviePlayerController, then you can check the loadState directly in the Apple documentation
enum {
MPMovieLoadStateUnknown = 0,
MPMovieLoadStatePlayable = 1 << 0,
MPMovieLoadStatePlaythroughOK = 1 << 1,
MPMovieLoadStateStalled = 1 << 2,
};
typedef NSInteger MPMovieLoadState;
Also, you can check Android MediaPlayer API documentation
If you have more interested about the implementation, remember, we are open-source hehe, then take a while examining directly on the Titanium SDK repository -> here
--
Well, while the videoPlayer API has not been normalised (maybe will not), you can simple manage this issue creating a conditional flow, e.g:
videoPlayer.addEventListener('loadstate', function(evt){
if(OS_IOS){...}
else if(OS_ANDROID){...}
else {...}
});

Playing different Sound files in Appcelerator Titanium makes the audio got noise

I have created an app using appcelerator for Iphone , which buy click on buttons it will play a relative sound , here is the code, but the problem is when i play the audio many times and play different audios using this function the sound starts to lag and have noise inside, can anybody help me with it , Thanks.
var soundplaying = 0;
var sound;
function playaudio(url) {
if (soundplaying == 0) {
sound = Ti.Media.createSound({});
sound.setUrl('../assets/audio/' + url);
sound.addEventListener('complete', function() {
sound.release();
soundplaying = 0;
});
sound.play();
soundplaying = 1;
}
}
(i have tried to release the sound object after each time but still no use, I tried to createSound only once but seems the titanium dose not support changing url for Media.Sound) dynamically.
I could have solve this issue temporary , by changing the audio files format to .m4a (aac).
i was using mp3 earlier.