how to work with a MediaPlayer perfectly in android - java

I am working in an online radio streaming project, where I am playing audio with mediaplayer from an URL. The music is playing and working fine. But I am stuck with the play,pause functionality regarding to the state of the player activity. I want such that when the app will be exited by home button press then music will keep playing, but when a phone call rings then the music will be paused and after the phon call the music will resume. And also when user opens another app then the music will continue playing in background. (as the default music player of android works)
But still what I did results almost my desired destiny, but something is missing, like I have started the music in onResume and stopped the music in onDestroy.By doing this the music keeps playing in background when I press home button, but when a phone call comes and recieved it continues playing which creates a disgusting situation, but when another app is turned on then the music goes off. How should I manage the MediaPlayer that will work like what I want? Any suggession?
Note: I am not using any service here, just an AsyncTask class to play the music.

The topic of MediaPlayer as background service is covered in multiple sources such as this one.
http://www.codeproject.com/Articles/258176/Adding-Background-Music-to-Android-App
You can also find this in the Android official documentation here
http://developer.android.com/guide/topics/media/mediaplayer.html
Hope that helps.

Related

Android App based on a service stop after some time in background

I've created an app, based on this link https://www.geeksforgeeks.org/services-in-android-with-example/, for playing an audio file than have 10 hours in background. But, after block or minimize, unfortunately, the sound stops after some time.
Do someone have an idea why this happening? Is there a way that keeps the app playing the sound after block or minimize, just like Spotify does?
I have made a repo for that app. For now, I have not added features to add controls when the app is running but will be coming soon. But, for now, when the app goes in background, the audio starts playing with the following controls:
Start
Pause
Reset
You can find the repo over here.

Media player on Android studio

I am doing a simple application on Android studio that plays a song. I managed to make a song play when I get to the specific activity. My question is- when I go out of my app(I leave it working in the background) and go back into it, or when I rotate the phone, the song starts playing from the begging. How do I make the song keep on playing from the place it stopped when I went out of my app/ how do I make the song keep on playing when I rotate the phone?
You may want to look into MVVM design pattern as live data is life-cycle independent, What is happening is common. When you rotate your screen or go out and back into the app, the activity cycle is being affected. Your activity literally restarts thus restarting your song.
Here are a few links that may help you on your way. Best of luck.
https://developer.android.com/guide/components/activities/activity-lifecycle
https://blog.mindorks.com/mvvm-architecture-android-tutorial-for-beginners-step-by-step-guide

How to set MediaPlayer always active even if I change Activity?

I have an application that has multiple activities and I've set a MediaPlayer for each of them with the same jingle.
I was asking myself, there's a way to keep the music in the first MediaPlayer of the first Activity going on for all the time while the application is running even if it switches activities?
Use foreground service with notification, it runs in background which we can play media player. Here is the example
ForeGround Service start and stop

How to add a music player to my app?

So I made a website someday, and now via webview and the YouTube Api I made it possible to listen to the music on my phone.
But... as soon as I close my app it stops playing the music.
How can I possibly add a music player and make it so the music keeps playing after the phone goes on standby mode?
Thank you very much!
Even thought I haven't deal with Youtube API for Android, I think that what you are trying to do is impossible. Even the Youtube App doesn't keep music playing after the phone goes on standby mode. That's their policy about music videos on Youtube.

android-developing music player

I'm developing an Android music player and so my app should be able to detect volume key presses even when no Activity of my app is open and the screen is off(a background service is playing the media). I was thinking that it seems crystal clear that i should detect volume key presses from my service but as I searched the net, I got that it is impossible(e.g. as described here). so what should I do?!!!
You'll need to create a service, because activities don't run when screen is off
You need to run player in service. Also, you must set media type as music.

Categories