Here is what I want to achieve in my app: I have 3 menus in the startup page: PLAY , INSTRUCTIONS , HIGH SCORE.
I have theme music playing in the background with the help of MediaPlayer object. I just want that the theme music should be playing for the PLAY and INSTRUCTIONS menu, uninterrupted, i.e, if the activity changes from MAIN MENU to INSTRUCTIONS and vice-versa , the music should be playing uninterrupted. But the music should stop as soon as the PLAY activity is started. I don't know the best way to do it, but all I can think of is sending the MediaPlayer object that started the theme music to PLAY's oncreate and stop it there. is there any way to do this?
You want to use a Service to play the music (and to do so off of the main thread). Here are relevant pages on
Services
Media playback
Related
I have an application which uses exoplayer to play videos.
When the user is on player page and presses the power button to close the screen I want the audio to keep playing in the background and notification should be visible to user with controls of play, pause , video metadata etc similar to what we have for every music playing app.
I can keep the audio of video playing from exoplayer using setPlayWhenReady(true).
But I am stuck in for notification. Should I be using MediaBrowserServiceCompat or I will have to create custom notification to handle it?
I think what you should do is to create ForegroundService and create notification as you mentioned to have the ForegroundService working. I think this post might help you:
https://androidwave.com/foreground-service-android-example/
Use a foreground service to implement something like this.This can help you
I am able to play audio using exoplayer inside and Activity
My Activity that play the music. The activity also contains MediaSessionCompat.Callback that let for exemple notification to play that audio is available in this gist
and also using App Widget.
PendingIntent playPausePendingIntent = MediaButtonReceiver.buildMediaButtonPendingIntent(context,
PlaybackStateCompat.ACTION_PLAY_PAUSE);
views.setOnClickPendingIntent(R.id.btnPlay, playPausePendingIntent);
views.setImageViewResource(R.id.btnPlay, icon);
My problem is how can I play ongoing music inside an ImageButton when I leave the activity?
Please find image below.
Best practice for audio apps is having the player in a foreground services. This service runs independent from activities and hence the player survives when your activity is destroyed.
So to answer your question: your image button starts a foreground Service or sends a message to a service to play audi.
You may consider using a MediaBrowserService which is actually designed for this: https://developer.android.com/guide/topics/media-apps/audio-app/building-an-audio-app.html
The Anroid Universal Audio Player (UAMP) is a sample app which does just this: https://github.com/googlesamples/android-UniversalMusicPlayer/tree/master/mobile/src/main/java/com/example/android/uamp
I am making an app in which I've made a Service which plays Music from URLs. The thing is that my music Service is playing music correctly BUT when user plays any song with Native music player then BOTH(Native Player and My Music Service) are playing music simultaneously. I want to stop My Music Service when user started playing music with native player.
Is there any Broadcast Intent which i can register to Detect the
music player is started?
Is it possible to detect Music player Started?
Any Other Solution?
Any suggestions would appreciated.
I'll suggest a different approach, that I believe it's the correct approach.
the issue on your approach is that you're suggesting to check for one specific app. And there're tons of different music players, plus radio players, plus video players, plus games... and all of those should stop your music in case they want to play something.
So how you do it?
It's all explained in the Android Developers website.
You have to register an OnAudioFocusChangeListener, so, whenever a different app request to have the audio focus, your app can stop the music.
Step 1: Detect if the user has opened native music app. For this , you need to know the package name of your native music app.
Then refer to my answer here: Android how to know an app has been started and range apps priority according the starting times
Using that , the list taskinfo will have the list of all running activities, and as explained there, the first element of the list will be the activity in the foreground.
STEP 2: Once you detect native music app being activated using STEP 1 (by polling for it in the background) , then stop your app's service.
NOTE: You should do this in a background (using asynctask) or another service.
NOTE 2: The limitation of this method is that you can't actually stop the music player when the user clicks play in the native music app, since this method will help you detect only if the native music app is opened or not.
First,I'm trying to do a music plug-in.
Then,the app need acquire state of other app's background music (playing or pause/stop).
1.If other app's background music is play ,then my plug-in not do anything.
2.If the background music is pause/stop,then my plug-in will have some operation.
I have seached a lot about the android music.but I have find the way to get current state of music play.I can only get isActive.but this method whether on play or pause ,the return value are same:true.that make me craze for one week.
then i try otherway but in the upper layer the player are control by mediaplay,which i can get the class object from.
So could anyone tell me is there a way that can tell the music play state? thanks for your any help.
I'm trying to create a really simple application for Android that will play a music file (I'm really only just starting to get into Android). I have only one Activity that starts when the application starts, and it starts playing the music file. What I need is that the activity always runs (plays the music), whether you press Back or Home buttons, unless you specifically tell it to shut down from Settings menu, and if you try to run it again, it should just restore that activity to the front (basically, how every other player out there works). What happens for me, though, is that when I press back to return to the menu screen for instance, and click on the app again, it runs another instance of the activity (which I can tell, because the music doubles). What can I do to prevent this? Many thanks.
For playing music in the background I would recommend you using a service.
Specify android:launchMode= "singleInstance" in your manifest file. This means that your activity is your entire application.
Don't forget to save the state of your time of the music. Use SharedPreferences for saving an integer with the second when the sound ended playing and just restore the state in onResume() method.
Unfortunately, you cannot play music after you press back button as the activity is destroyed. You must start a service if you wish to do that as the other answer suggests. The reason is that you need a Context object to play music and it will no longer be available after onDestroy() method is called.
http://developer.android.com/guide/topics/media/mediaplayer.html
Here you can find examples of playing media files in a service.