I saw lots of similar questions, but I couldn't figure out the best way in my case.
I want to play some background music in my app (looped). It should start (if a sharedPreference is set) at the start of the app. If the user disable a checkbox, it should stop (if he enabled it, it should start again, preferences is set here...).
Nearly everything worked fine by using an MediaPlayer. But I don't know, how to stop the music if the user tabs the home button. Because of playing music across Activities, I think it doesn't seem wise to stop in onPause() or onDestroy()?!
I read a lot about AsyncTask and Services, because I know it's not good to play the music in the MainThread instead of an WorkerThread. But I couldn't figure out the best solution and how to handle it.
Is there a way to play music until I say explicitly "stop" (in any Activity) or the user tabs the home screen button without editing each Activity?
Related
I'm developing a chromecast streaming app for Android. In Activity A the user chooses between different playable items and then is moved to Activity B, which holds the Chromecast controls. When the user hits the back button he is presented with Activity A from where he can choose another video while the casting of the first one is still running on the TV. The problem is that after the back button is pressed, Activity B which holds the controls (for example: seek bar with the video progress) is destroyed and there is no way the user can interact with the destroyed controls anymore. Is there a way I can keep Activity B alive? I'm a novice developer and I have no experience with fragments, so I'm looking for a solution which does not involve their usage...
If you want to use the streaming without any UI showing, you need to create a Service and bind to it with your activities. Let the service handle everything. Like this you're not dependent on the UI being shown.
I strongly suggest you read the Cast UX Checklist where this issue is brought up and addressed. The ways to address that are:
Having a persistent controller on each page
Using the Media Router Controller Dialog when casting
When not in the application but casting, using Notification
When on the lock screen, using the Lock Screen media controller buttons
On Android, you can use the CastCompanionLibrary that provides all of these for you with minimal coding.
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.
I am just messing around with an app that streams audio and I wanted to give it a feature similar to Pandora/Google Music/etc where if you press home or lock the screen the audio continues to play in the background.
How exactly can I accomplish this? Is it through a broadcast receiver or a service? If I knew more closely what I was looking for Google would be more helpful.
Thanks!
It's a service. Anytime you want to do something that takes a lot of time like playing music, downloading a lot of data from a server, etc. it should be a service. The basic technique is to always have the service play the music and then have your activity connect to it to show the status and update the tracks, etc.
I have seen this post here (http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state). I am having a similar problem I believe.
I have an app which listens to an audio stream (using the mediaPlayer object). If I press the Home button it will continue streaming and hide my app. Then, at a later point I can go back to my app and press stop when I'm done. This is what I want. If however I press the Back button, when I later open my app again the app has been redrawn from fresh. Text boxes, buttons, everything has reset like I've just opened the app for the first time so I can't stop my audio stream. Clicking stop does nothing because the app has 'forgotten' it is streaming (the stream runs under a separate handler from the main UI thread, so I'm guessing since its been 'reset' it has lost track of its handlers?).
Why does this happen with the Back button, and how can I stop it?
Move the streaming functionality into an Android Service. Use an Activity to bind to the Service and to interact with it.
basically, what am doing is that am running a flash file (playing music) in the background of the app using webview, and when I want to stop the music , I just load "about:blank"
I want to keep the flash file running (music) when my app gets paused (which works fine so far) the problem is when the app resumes, pressing on pause button launches a new webview instead of changing the link in the previous one and the sound doesn't stop
the only solution I found was to kill the process and restart the app, but that's not practical, any idea of how to still be able to use the same webview when resuming the app ?
Update: guess I wasn't clear enough, I have a webview widget stated as "gone" , I only need the audio form the flash file I'm running in the webview so the "play" button loads a URL that contains the flash file (so the user only hears music, and doesn't notice that I'm using a webview), when I press the back/home button the music doesn't stop (I like it that way) but when I go back and press on play again it loads another url in a new page, you get double music
There are a few possible causes for this. One is simply that multiple instances of your activity are being created. Assuming you don't want this, add android:launchMode="singleTask" or android:launchMode="singleInstance" to your main <activity> in AndroidManifest.xml. More info here:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
If that doesn't help, another possibility is that your application is creating a new instance of the WebView -- either because the relevant code is in the wrong place (e.g. onStart()), or because the app has actually been destroyed and re-created by Android rather than paused and resumed -- and the old WebView thread is still hanging around for some reason. More information (such as code samples) would be helpful in identifying the problem, if that's the case.
Not sure about this solution but at least you should try this once
protected void onResume() {
super.onResume();
activityTermsAndConditionBinding.webView.releasePointerCapture();
activityTermsAndConditionBinding.webView.invalidate();
}