I have two activities, the one "Main.java" that initially loads I have a couple mediaPlayers that are created which control background music and a voice over.
The second activity is a "Settings.java" which is accessed through a settings cog icon, where I want to use On/Off toggle switches to allow the user to mute the two different mediaPlayer's.
The problem is, when I try to access the mediaPlayer name in the second activity, it can't find it since it's in a different activity. I know this is a rather simple problem but this is my first app and first project in Java. Any help appreciated!
You could save a preference, say 'isPlayerOn' in your SettingsActivity and check this preference in your PlayerActivity's onResume. You can make SettingsActivity extend PreferenceActivity so you don't write extra code in there.
You could use this on your settings page
public void onClick(View v) {
boolean alertWarning = ((ToggleButton) v).isChecked();
if (alertWarning) {
// unMute();
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, false);
} else {
// mute();
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, true);
}
Related
I want to create one time activity window to my Android app.
In the window have two buttons.the buttons are connect to two different pages.
If you download app and run for the first time, you see this window first.then you have to select one button.
When you click the button, you will connect to the target page.
If you close the app, and open it again you can't see the window with two buttons.
The only thing you can see, the window that you previously selected with the button.
This is the thing, I want to do.
I tryied many codes based on YouTube videos.
But I didn't get the progress, that I want.
This the demo app for my course project.
I stuck with this problem.
you can download my project :-
Dropbox - https://www.dropbox.com/s/r4iztx8onqu2ctz/demo.zip?dl=0
Zippyshare - https://www85.zippyshare.com/v/bnXekEoz/file.html
You can save a bit in SharedPrefrence "isFirstRun" set it to true when the application is first run and go through your first activity.. for future run you can check if the "isFirstRun" bit is true skip the first activity.
More about SharedPrefrences here.. https://developer.android.com/reference/android/content/SharedPreferences
In your MainActivity :
SharedPreferences settings = getSharedPreferences("prefs", 0);
boolean firstRun = settings.getBoolean("firstRun", true);
if (firstRun) {
startActivity(new Intent(getApplicationContext(), IntroActivity.class));
}
Create new Activity and named IntroActivity,and in Onclick Button :
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", false);
editor.apply();
finish();
}
});
What you could also do is use install referer receiver then store the value in shared preferences and use that once you open activity https://developers.google.com/android/reference/com/google/android/gms/tagmanager/InstallReferrerReceiver
I'm trying to create an application that will only let you in a another activity if you plug in your headphones. I got a code, that works, but the problem is that it's checking the headphone is plugged in or not when the activity starts only. I want the application to always check the headphone state. Not only when the activity creates. How to do this?
AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if(audioManager.isWiredHeadsetOn()) {
Intent intent1= new Intent(this,Main2Activity.class);
startActivity(intent1);
}
Here's the code, it's fully functional, but how to make it to always check the state of the headphones if you in the activity? Because the app only checks the state of the headset when it's starts, so you have to plug in your headphone at first, and then start the application to make the new intent work.
Thanks for every answer.
Hello working with text to speech.
I am targeting API 16 so I launch this intent to start TTS settings on the device
public Intent launchTTSSettings(){
Intent TTSSettings = new Intent();
TTSSettings.setAction("com.android.settings.TTS_SETTINGS");
TTSSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return TTSSettings;
}
My app goes in the background.
I change some TTS settings
My app comes back into foreground, I notice that my app still uses the old TTS settings unless i close and open again the app again.
I want to way to listen to any changes to TTS settings maybe a broadcast receiver, but I could not find anything related to that.
So is there any way i can gracefully respond to changes in TTS settings?
I was thinking i could destroy my TTS instance and re-make it but the root of the problem is how do I know if a setting was actually changed
Thanks for reading
Came with a solution which is not really great but it seems to work as it abides by the changes
Please note I have not introduced a progress dialog and wait until onInit called but here is the logic
So here i launch my text to speech settings intent and set a flag to true
startActivity(mTextToSpeechHelper.launchTTSSettings());
mTTSSettingsHasChanged = true;
Then in on resume
#Override
protected void onResume() {
super.onResume();
if(mTTSSettingsHasChanged){
mTTSSettingsHasChanged = false;
mTextToSpeechHelper.destroy();
mTextToSpeechHelper = null;
mTextToSpeechHelper = new TextToSpeechHelper();
}
}
note that destroy does this because my TTS stuff is in a helper class
public void destroy(){
if(mTextToSpeech != null){
mTextToSpeech.stop();
mTextToSpeech.shutdown();
}
}
What is not good is that the user could go into settings but not change anything which would resort in the old instance being destroyed and a new one made for no productive reason
I have develop a audio recorder which is working perfectly in foreground as well as background.But there is a problem when i am running audio recorder in background and start another app which used the sound(like video recorder, music player, video player etc) so the app will crash. So i want to add a functionality in audio recorder that when the other app (like video recorder, music player, video player etc) which are using microphone or speaker are running in foreground so audio recorder automatically go to the pause state.Thank in advance.
If u are still not understand my problem so please used your mobile and play the music player than go to the home. So the music player is running.And open the another app which are not using mic & speaker so music player is still running but when u r open another app which are using mic & speaker(like video recorder, music player, video player etc) so music player will pause. This type functionality i wanted.Thanx in advance
Finally lot of search & googling i have got the answer of this question. I am using audiofocuschangelistener. The code is there
AudioManager.OnAudioFocusChangeListener audioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
switch(focusChange)
{
case AudioManager.AUDIOFOCUS_GAIN:
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
//resume your activity
break;
case AudioManager.AUDIOFOCUS_LOSS:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
///pause your activity
break;
}
}
};
and do this in onCreate()
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int result = audioManager.requestAudioFocus(audioFocusChangeListener, AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
//start your activity
}
And for more information u can follow below link
http://developer.android.com/training/managing-audio/audio-focus.html
As far as I understood from your question, you need your application to stop whatever it's doing (sound recording probably?) when it's not the foreground application.
You need to override onPause method in your main activity so that when your activity goes into the background (the new activity comes) you can stop your recorder.
Furthermore, you can resume your activity's task when user starts interacting with your application again by overriding onResume method.
I have an intent that launches an Audio Player Intent, and plays an Audio File. The Audio Player offcourse is external to my application. When the user presses the back button, my activity is again displayed to the user. I want the Audio Player to close when the user presses the back button. How can i control this.
There are two options to achieve your requirement
call "finish()" after startActivity() which is used to start audioplayer. finish() function will close your application and when user press back button from audioplayer, Android will display previous activity most probably home screen.
User CLEAR_TOP flag when you start the activity. Clear top flag will clear all top activity so when user press back from audio player, Android OS will display Home Screen.
Intent i = new Intent(......);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Thank You
I placed this code on the OnResume() of my calling activity and it works like a GEM.
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
AudioManager mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if (mAudioManager.isMusicActive())
{
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
this.sendBroadcast(i);
}