Media player on Android studio - java

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

Related

Uninterrupted background music across all activities in Android Studio game

I am currently working on a game in Android Studio and am having issues with implementing a constant soundtrack across all activities. The way I want it to work is that when the app is started up, an audio file will begin playing and looping and will continue looping throughout all activities, and essentially never end (or restarted when changing activities). At some point I would also add a mute/silent button as well. My question really is, how can I create a class for this sound file to play on repeat upon booting the app and also continue without interruption when changing activities. As it currently stands I have a few lines declaring a Mediaplayer object and setting it to loop and playing it, within each activity.. I realize this is not an efficient implementation but I am brand new to Android Studio and still figuring this out.
Follow this tutorial:
https://www.codeproject.com/Articles/258176/Adding-Background-Music-to-Android-App
And start the Music Service in your MainActivity, if there are still any issues, then let me know.

Strategy for transparent Android app that allows 3rd party app behind it to update

I have an Android app that is transparent that monitors for an event and then displays a message and toggles the background between transparent and a translucent red. The app itself is working exactly as I was hoping. The problem is that only the top most app (my app) is active and updating the view. The app displays over a 3rd party app that runs a live video stream. I want the live video stream app to continue to update while my transparent app is on top. Since I have no control over the other app and it doesn't support multi-window approach, I am looking for a different approach or work around to accomplish the same thing.
I have read of people trying to use a system popup based on a service. Does anyone have experience trying to solve a similar problem and found a good approach for designing the app or a work around?
Try to google about Activity lifecycle. When another app comes as the top application (let's say your transparent app) the last playing application goes to onPause until the user bring the last app back to the top again.
The short answer is: what you want is not possible.
any question?

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.

how to work with a MediaPlayer perfectly in android

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.

Categories