.isWiredHeadsetOn() repeat check? - android

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.

Related

How to reopen app correctly from foreground service?

I've been facing a problem for a while regarding startActivity(intent). What i'm developing is a kind of key word detection like "ok google" that triggers an alert when the user says the word. To achieve that when the user is not using the app i have a LifecycleService that runs in foreground and listens to the user. When the user says the word and the app is killed it opens the activity i need using startActivity from the service, but the problem is that if i keep listening and change to other activity (using the app normally) the detection works (because i hear the sound i put when the word is recognized) but the app doesn't start the activity it should (although the startActivity(intent) is called). I'm pretty sure that the problem has to be with the context that maybe is not the correct one when i open the app with startActivity from the service, but i don't know how to fix it. I tried to user some other variables like applicationContext or the androidContext() from Koin but it is not working.
class SpeechRecognitionService : LifecycleService() {
...
//onStartCommand starts the audio recognizer and startAlert() is triggered when the alert is recognized. It is always correctly called
private fun startAlert() {
//This is not showing MainActivity although i execute it
startActivity(MainActivity.getDialogIntent(this))
//I always hear this audio when the app detects word
val audio = MediaPlayer.create(this, R.raw.alert_detected_audio)
audio.start()
}
}
The MainActivity.getDialogIntent(this) is just a common Intent
fun getDialogIntent(context: Context): Intent {
val intent = Intent(context, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra(SHOW_ALERT_DIALOG_KEY, true)
return intent
}
The problem only happens if the app is started with the voice recognition (after being killed). If i for example kill the app but open the app again pressing the app icon it works correctly. If i start the app with voice (so, from the startActivity i have above) i worked that time and open the app but when i change to other activity it fails to start.
You need to use a proper "launcher Intent". The easiest way to get one is to call
PackageManager pm = getPackageManager()
Intent intent = pm.getLaunchIntentForPackage("my.package.name")
Use this Intent to start your MainActivity instead of calling MainActivity.getDialogIntent(this)

mediaPlayer single Instance

I got a bug on my MediaPlayer.
When i click on an item of my playlist, it create a mp3Player. If i press back the music play in background (all is normal). But if i choose another song from my app, the app create a new mediaplayer:
Then i got 2 music playing !
I tried to use "singleTask", "singleInstance", and it's not working.
I tried "intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);"
None of this work!
Maybe if i could resume the previous Activity, i could destroy it and then create a nex mediaplayer no?
Can someone help please? :D
You play music in backround it means you use Service ?
Problem is you need reconnect with Service (Service play music).
How to reconnect service android
Or try. Media Playback , EG: Universal Android Music Player Sample
you can simply make your activity android:launchMode="singleInstance" in manifest.
and then in your activity overide below method
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// do what you want here
}
in above method, you can reset value of media player

How to mute music in a mediaplayer created in a different activity?

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);
}

Pause spotify playback intent

I am looking for a way to pause Spotify playback. I have tried searching, and using the AudioManager, but I believe that only works if it is part of your activity. Does anyone know a way to pause Spotify playback?
How to stop or pause Pandora and Spotify
That issue is similar and shows how to pause and stop most media players.
I found that this works best:
Intent pauseSpotify = new Intent("com.spotify.mobile.android.ui.widget.PLAY");
pauseSpotify.setPackage("com.spotify.music");
sendBroadcast(pauseSpotify);
Essentially, what you are doing is, creating a new intent which action is the built in "PLAY" action from the spotify app. Next you set the package and finally call the intent.
I got the idea from an article and applied it to normal android.
//play
Intent intent = new
Intent("com.spotify.mobile.android.ui.widget.PLAY");
intent.putExtra("paused",true);
intent.setPackage("com.spotify.music");
sendBroadcast(intent);
//pause
Intent intent = new
Intent("com.spotify.mobile.android.ui.widget.PLAY");
intent.putExtra("paused",false);
intent.setPackage("com.spotify.music");
sendBroadcast(intent);
I've analyzed Spotify's apk file and found the command file.
This would work perfectly on version 8.4.77
You can skip song by replacing "PLAY" to "NEXT".
I tried "PREVIOUS" but it didn't work, guessing it as spotify's default design.

Android playing Audio Files

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);
}

Categories

Resources