Android - sounds are buggy, stop playing midway, play only on random occasions when invoked

for long music, the result was worse with the older openfl versions. The music doesn’t really pause, it’s just as if it’s mute, until you tell it to play again.

Also, from the profiling, do you have any advice to optimize my code for performance?

Pooling objects to avoid allocation may help in reducing GC pause, though that’s difficult to do in everywhere. Many small objects are also allocated in library side.

If mute issue occurs with older versions, then the problem might be in OGG/WAV loader.
Does that issue occur with any sound file you tested?
Would you mind posting sound file in question (and ideally minimal code that reproduces the issue), if possible?

I found this issue from GitHub but this was opened two years ago, and is already closed.

Does sampling frequency and number of channels(stereo/mono) matter here?

Here’s my SFX.hx file that I use to play small sound effects. It has changed since I last posted. I call the init function when the game starts, and I call play function to play a particular sound effect. On android, atm, each sound effect plays on once - on flash it works fine. If I swap the #cpp and #else code, then the sound effects play at some occasions only - on android.

(I will post the music code as well soon)

class SFX
{

public function new() 
{
	
}


public static var EXTENSION:String;

public static var soundNames:Array<String>;
#if cpp
public static var sounds:Array<AudioSource>;
#else
public static var sounds:Array<Sound>;
#end

public static var DIAL:String = "audio/dial";
public static var DIAL_SELECT:String = "audio/dial_select";


public static function init() {
	EXTENSION = ".wav";
	
	
	soundNames = [DIAL, DIAL_SELECT];
	sounds = new Array();
	
	for (i in 0...soundNames.length) {
		#if cpp
		sounds.push(new AudioSource(lime.Assets.getAudioBuffer(soundNames[i] + EXTENSION)));
		#else
		sounds.push(openfl.Assets.getSound(soundNames[i] + EXTENSION));
		#end
	}
}

public static function play(name:String) {
	
	sounds[soundNames.indexOf(name)].play();
	
}

Here’s my code for the music (the link to music: music ) :

public var musicTransform:SoundTransform = new SoundTransform(MUSIC_MAX_VOL);

musicChannel = new SoundChannel();

#if flash
MUSIC_EXTENSION = ".mp3";
#else 
MUSIC_EXTENSION = ".ogg";
#end

public function changeMusicFile(filename:String = null, position:Float = 0) {
	if (filename == null) filename = nextMusicType;
	musicChannel.stop();
	musicChannel.removeEventListener(Event.SOUND_COMPLETE, onMusicLoopEnd);
	var snd:Sound = Assets.getMusic("music/" + filename + MUSIC_EXTENSION);
	musicChannel = snd.play(snd.length>position?position:0, 0, musicTransform);
	musicChannel.addEventListener(Event.SOUND_COMPLETE, onMusicLoopEnd);
	lastMusicType = filename;
	nextMusicType = filename;
}

public function onMusicLoopEnd(e) {		
	changeMusicFile();
}

hey was my code above helpful in any way?
(maybe i am not implementing it correctly?)

I’ve tried playing your file on my Android phone, but nothing strange happened with my simple test program. Not sure what else I could try next.

Maybe my phone’s buggy, but I did try on Windows earlier and the same thing was happening there. I’ll try on another person’s phone and get back

I’m experiencing the same problem in latest lime (3.4.1) & openfl (4.4.1):

  • long ogg’s stops randomly, but event onComplete is working ok as it starts again once it’s supposed to be completed (I’m manually looping sounds).
  • short wav’s also stops randomly.

I’m testing with a Nexus 5X using android 7.0 stock rom

Are you able to build from source? If so, try commenting out the delete buffer and delete source calls for the OpenAL garbage collection (then running openfl rebuild android again)

https://github.com/openfl/lime/blob/develop/project/src/audio/openal/OpenALBindings.cpp#L31

perhaps something is getting collected too soon?

Also, does limiting the number of active sound channels make a difference?

Ok, I managed to build lime and openfl from source (it’s important to use ndk-11c, neither 9b nor 13b worked).

I tried commenting and decommenting both lines and found that commenting

lime_al_delete_source (source);

solved the issue, so I think it’s something related to gc.

lime_al_delete_source does not disable hxCFFI finalizer with val_gc(source, 0), so this could be the cause of premature deletion issues. lime will end up calling alGenSources() twice with the same source id.
As a result, lime may stop unrelated sources since OpenAL implementations could reuse the same source id when new source is requested with alGenSources().

This problem might be more noticeable on Android because it tends to have much lower amount of memory. If you write an app that allocates memory very often, that will also cause frequent garbage collection (though this is hard to avoid).

I believe this PR will fix your audio issues. Clearing finalizer is forgotten here, and that will cause double deletion.

Merged, thank you :slight_smile:

I’ll try it tomorrow, thank you!

Sorry for the late response, tomorrow was a week later :slight_smile:

It seems to be fixed now, thank you!

I’m experiencing the same problem at least on Windows target. (OpenFL 4.5.1, Lime 3.5.1)
My music simply stops playing (mute?), if any other sound is playing.
Let’s say I have music playing and I click some button that play “click” sound - music just stops playing.
When I click button again, I hear a bit of music and “click” sound at the same time and then no music again.

UPDATE: I’ve done some more test and it’s sound problem, but some bugs because of my code still written for legacy mode. So, fixed now. :slight_smile: