Background Music Player While Switching Between Activities in Android - android

I am making an music streaming application with music player in it. Now i want that users should be able to switch between activities while listening to music in the player. Also i would like to control and display info about the track being played in one activity.
I have tried implementing the MediaPlayer but is stops playing when i navigate to another activity. Any code example or pseudocode is welcome.
Thanks.

The best option for this is Service.., See this Tutorial.You can learn more Here about services

Related

How to disable audio capabilities for entire app

I have an app that plays videos using the video_player package. These videos have sound, but I don't want to play it. To achieve this, I use:
controller.setVolume(0.0)
The problem is whenever I open the app, even before playing a video, any background music stops.
I figure there must be a way to say "this app does not have sound capability" and then the OS won't pause any background music.
Is there a flutter specific way to do this, or should I look for iOS and Android specific solutions?
Thanks!

How to turn off all other music when my Android app is playing music?

I was wondering how to exactly stop all other music that may be playing when my app is opened whenever there is music being played in my application. I have a couple instances where I just have sound effects, so is there anyway I can stop music that may be playing already whenever these sound effects pop up? I know I'm not giving any code, however, I have no idea how to approach this and haven't found many other questions asking similar things.Thanks!
Look at the AudioManager.requestAudioFocus API. Remember to release the focus when you're done with it, or other apps may not be able to play sounds correctly.

How to keep MediaPlayer going no matter what

So I've been working on a music player for Android and I've run into a problem I'm having trouble fixing. When the user starts a song, I store the currently playing song in a public variable in my "Player" class. This is used to determine what song to play next, among other things.
The problem is that the app sometimes crashes when the user opens a lot of other memory intensive apps. The MediaPlayer stops playing, and the reference to the currently playing song is lost. I'm pretty sure this happens because these other apps claim the memory that my app was using.
My question is: How can I make sure the MediaPlayer always keeps on playing? The standard Android music player doesn't seem to have this problem so it should definitely be possible to keep it playing at all times somehow.
The best you can do is host the MediaPlayer in a foreground Service.

Restoring Android Activity playing media

I'm working on a project that includes a media player. All is going fine, I select the media to be played from a play list and send it to the player activity and press play. The audio plays just like it should. Now, when I leave that activity (with the audio still playing) I'm unable to get back to that same activity, clicking the same item from the playlist starts a new activity with the old media playing in the background still. How can bring back an old activity?
I don't get your question at all. However, look at the state diagram here:
http://developer.android.com/reference/android/media/MediaPlayer.html
And the other state diagram here:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Combine the MediaPlayer states and the Activity states in order to fulfill your need.

Android: Detect another application has started playing audio

My music application constantly plays music in the background, however I'd like to be able to detect when another application starts playing audio (such as the YouTube app) so I can pause/mute/stop the audio in my application.
This will allow a user to continue browsing the web whilst listening to music, but then if they wish to watch a video at any point, they can do so without audio conflict.
One solution might be to listen for a broadcast which states when an application begins using the AudioManager. Does such an Intent Action exist?
Edit: As in the answer provided below, there appears to be a method of detecting the loss of audio focus in 2.2 with AudioManager.OnAudioFocusChangeListener.
Great, but is there a solution for the more common versions of Android? Ideally 1.5+.
http://developer.android.com/reference/android/media/AudioManager.OnAudioFocusChangeListener.html
this thread also has additional information that might get you heading in the right direction.
http://groups.google.com/group/android-developers/browse_thread/thread/db6822d84feaac6/219d8cba07795c61?hl=en&lnk=gst&q=OnAudioFocusChangeListener#219d8cba07795c61

Categories

Resources