Playing music in PlayN: how to pause, resume? - android

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.

Related

MediaManager.addCompletionHandler doesn't work on 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.

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.

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.

How do I stop the sound restarting when a collision occurs

Currently my code plays a sound when there is contact between any objects in my game using andEngine and Box2D and the walls, the problem I have is that when contact is made with any objects it starts again I understand why this is happening. What I want to do if possible is keep playing the sound while also playing it for another collision. I think I may need to use threads but I am unsure how to do this in java for android.
#Override
public void beginContact(Contact contact)
{
Rattle.this.mExplosionSound.play();
}
UPDATE: I don't seem to have been able to fix the issue but I know that what I need to do is play this sound multiple times simultaneously, I have tried threads and the soundPool but got no luck with either still not sure what to do.
It seems you have only one instance of the sound. Then you call play on the same sound twice, therefore it stops playing and starts from the beginning.
Imagine having one CD player - you press play, the music starts to play. You press play again and the player starts reading the track from the beginning. What you need is two players, therefore you may need two instances of the same sound.
(I don't have much experience with sound on Android so I may be wrong, please take the advice with a grain of salt.)
Even I was developing a game some time back and I found a similar problem. You should use SoundPool to play short sounds like crashing and collisions. here are some good linkswhich helped me. Link 1, link 2.

Android: Detect another application has started playing audio

My music application constantly plays music in the background, however I'd like to be able to detect when another application starts playing audio (such as the YouTube app) so I can pause/mute/stop the audio in my application.
This will allow a user to continue browsing the web whilst listening to music, but then if they wish to watch a video at any point, they can do so without audio conflict.
One solution might be to listen for a broadcast which states when an application begins using the AudioManager. Does such an Intent Action exist?
Edit: As in the answer provided below, there appears to be a method of detecting the loss of audio focus in 2.2 with AudioManager.OnAudioFocusChangeListener.
Great, but is there a solution for the more common versions of Android? Ideally 1.5+.
http://developer.android.com/reference/android/media/AudioManager.OnAudioFocusChangeListener.html
this thread also has additional information that might get you heading in the right direction.
http://groups.google.com/group/android-developers/browse_thread/thread/db6822d84feaac6/219d8cba07795c61?hl=en&lnk=gst&q=OnAudioFocusChangeListener#219d8cba07795c61

Categories

Resources