MediaManager.addCompletionHandler doesn't work on Android - android

I have another issue with MediaManager, I am trying to implement background music loop, and to achieve that I obviously use addCompletionHandler:
backgroundMedia = MediaManager.createBackgroundMedia(mediaPath);
MediaManager.addCompletionHandler(backgroundMedia, this);
backgroundMedia.setVolume(75);
backgroundMedia.play();
this code works in Simulator but doesn't work on the real Android device (6.0, 8.1)
Before that, I have tried to achieve the same with regular MediaManager.createMedia but as I mentioned in comments of my another question, it doesn't play two sounds in parallel, so either background music or sound effect will be played:
Background music (MediaManager.createBackgroundMedia) causes crashes at app closing/opening
So at this moment, I see no way to play background music in a loop continuously in Codename One, maybe somebody has the workaround for that?

On platforms other than Android background media is a synonym to regular media playback. We need this special API only for Android. I'm not sure if completion handler is meant to work with background media as the app might stop running while background media is playing.

Related

How to make background sound quiter

If you have an app like Spotify playing music in a background and try to listen to some sound in Google Chrome at the same moment, the background music becomes quieter at this very moment while Chrome playing the sound and then comes back to normal level.
I am wondering how I can do the same from my own Android app, namely, to make volume of all other applications lower for some time, but allow my application to play sounds with normal volume at the same time. I tried to find the answer, but usually all recommendations are about how to change volume globally.
This is a feature called audio ducking- the sound that gets quieter is said to be ducked. The way Android does it is through audio focus. Your app needs to request focus, and the other app's sound will be changed appropriately. The documentation on it is at https://developer.android.com/guide/topics/media-apps/audio-focus. The mechanism changed in Android 12, so you'll likely have to have different code for older versions and newer ones to make it all work.

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.

Playing music in PlayN: how to pause, resume?

I am playing some background music in my PlaynN game. Everything is fine except it does not pause when the activity is sent to the background. Music still plays when the game is not playing.
I can use Sound.stop() and Sound.play() whenever the window focus changes but then the music restarts from the beginning. I can instead use setVolume(0) and setVolume(1), but it still doesn't sound the way it is supposed to.
I cannot find a working example. I am currently considering the use of a platform specific music player.
Many thanks
To my knowledge, PlayN doesn't support pausing/resuming a Sound, probably because not all platforms support this. However, you might want to take a look at the Android-specific implementation of AndroidAudio, which seems to use a SoundPool backend. The GameActivity seems to already pause and resume sounds when the window gains/looses focus, so I'm not sure why it's not working for you - I believe it has for me. You might try calling these methods manually to test that they work, and if not consider filing an issue.

Using Services vs. MediaPlayer and SoundPool

So my friends and I are trying to make a game with sound effects and background music. One of us implemented the background music using Services but it's really unstable and causes the app to crash sometimes.
I was wondering what's the benefit of using a Service?
Also, I was wondering if we could just use a MediaPlayer to play the background music and a SoundPool to play the sound effects? ..or is that bad style or inefficient?
Thanks!
I was wondering what's the benefit of using a Service?
Using a Service to control your sounds is useful when you want your app to keep running the sounds even if it is not in the foreground. One good example is a Music App. It would be reasonable to keep the music running when the user hit the Home button for example.
Now, regarding your second question, yes you should use MediaPlayer for your game music and SoundPool for the effects. However, you should also consider putting them in a separate thread if you notices performance issues.
What works for me best (and I think is accepted in general) is to use the MediaPlayer for music and SoundPool for sound effects.
I believe you should try to avoid using a Service to play music or sounds (since the idea behind a service is that it should run in the background). Yet if you don't want to play any sound in the UI thread, you could create an additional thread. I personally call all my sounds from the main thread and it seems to work all fine.
I hope this helps.

Categories

Resources