How do you check if music is playing by using a broadcast receiver?

You don’t need a broadcast receiver for this – AudioManager is your friend: AudioManager.isMusicActive() does the job you want, have a closer look here for details: AudioManager here is an example: AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE); if(manager.isMusicActive()) { // do something – or do it not }

How to stop other apps playing music from my current activity?

AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE); // Request audio focus for playback int result = am.requestAudioFocus(focusChangeListener, // Use the music stream. AudioManager.STREAM_MUSIC, // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { // other app had stopped playing song now , so u can do u stuff now . } Audio focus is assigned in turn to … Read more

How to put media controller button on notification bar?

Here is the example above done correctly to the new API In your main, when you want to start a notification instantiate the class: NotificationPanel nPanel = new NotificationPanel(MyActivity); And when you want to cancel notification: (as it is an onGoing notification) nPanel.notificationCancel(); Then create the class for the notification caller: public class NotificationPanel { … Read more

READ_EXTERNAL_STORAGE permission for Android

You have two solutions for your problem. The quick one is to lower targetApi to 22 (build.gradle file). Second is to use new and wonderful ask-for-permission model: if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { // Should we show an explanation? if (shouldShowRequestPermissionRationale( Manifest.permission.READ_EXTERNAL_STORAGE)) { // Explain to the user why we need to read the contacts } … Read more

Android background music service

Do it without service https://web.archive.org/web/20181116173307/http://www.rbgrn.net/content/307-light-racer-20-days-61-64-completion If you are so serious about doing it with services using mediaplayer Intent svc=new Intent(this, BackgroundSoundService.class); startService(svc); public class BackgroundSoundService extends Service { private static final String TAG = null; MediaPlayer player; public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { super.onCreate(); player = MediaPlayer.create(this, R.raw.idil); … Read more