Uninterrupted background music across all activities in Android Studio game - java

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.

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

Store data locally on app close, not when put into background

I am working on an app that i want the user to be able to run it in the background, but i need to save some data whenever the app is completely closed. I have been looking at using SharedPreferences. I am having issues with onPause() and onStop() since those are called whenever the app is put in the background. I need to save data only when the app is completely closed and not when the user brings another app to the foreground and puts mine in the background.
That's not how Android works. You could be closed entirely hours from now. Or weeks. Or not at all and the user turns off the phone causing you to never save. The entire design of Android and its Activities is such that you should always be able to restore your state, and you shouldn't know or care that your app is being terminated. In fact there is no signal that an application is being terminated.
You could try doing it in onDestroy, but it isn't really a great idea. There's situations where onDestroy is not called.

Adding libgdx to an Android Native Application

I have a simple kids app which teaches things such as colors, numbers, etc... which I am currently developing. It uses what I would consider "standard android java programming (Single Xml/java class)." I also have a simple game with a Dinosaur that jumps over letters using libgdx.
My question is, I would like to have the game as part of the "overall" app. I would like to know if it is even possible to add a libgdx game to an android native application. I tried adding the appropriate files to my app, but this caused a compile error, could someone point me to a place where I could find help on this issue, or perhaps let me know if what I am wanting to do is even possible? Thanks.
---To clairify, my main app has a menu which takes you to activities. I would like the libgdx game to be one of the activities.
---Edit to response---I compile using Gradle and have more errors than I can count. I've since removed the libgdx game from the app so I am not sure of specific errors, but iirc there were over 100, many of which were R issues, which I could have figured out, but I couldn't find anything on either S.O. or the web saying it was even possible to add libgdx to a non-libgdx app.
You can integrate your App with your libgdx game.
AndroidLauncher.java is your libgdx Activity and you can move from one activity to another using Intent.
intent = new Intent(this, AndroidLauncher.class);
startActivity(intent);
and movement from libgdx AndroidLauncher.java, you need to call it from inside the core project classes. You need to use an interface.
https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code
If your game is sub view of your App.The AndroidApplication class (which extends activity) has a method named initializeForView(ApplicationListener, AndroidApplicationConfiguration) that will return a View you can add to your layout.
Pros.-Code is already you have,only you need to integrate.
Cons.-you need to maintain OpenGL Context Loss. Libgdx do for you.
In Libgdx your entire game is just a single Activity. You can start it as any other activity from your code.
Please follow Can I run an Activity before running libgdx? for details.

Integrating multimedia with LibGDX

According to the LibGDX web page:
Android applications can have multiple activities. Libgdx games should usually only consist of a single activity. Different screens of the game are implemented within libgdx, not as separate activities. The reason for this is that creating a new Activity also implies creating a new OpenGL context, which is time consuming and also means that all graphical resources have to be reloaded.
However, this isn't impossible to do - just undesirable.
I'm building an app that is designed to hold a few games built in LibGDX, as well as some information alongside it - video/audio, text with illustrations, and so on, describing the development of the games.
Is it a better idea to build this as a native android application that launches and tears down LibGDX whenever the user wants to play a game? Or should I write the entire app through LibGDX and do try and do the non-game stuff using UI libraries and so on?
I'm sorry I can't nail this down to a more specific question. I'm really interested to know before I embark down the wrong road.
The consensus in a linked Reddit thread here is that setting up and tearing down a LibGDX activity isn't such a huge overhead, particularly if the games are small (which they are in my case).
I'm going to opt for this solution, and simply shut down the activity whenever the user stops playing a game.

Categories