Resuming Music

Monkey Forums/Monkey Programming/Resuming Music

Shinkiro1(Posted 2011) [#1]
Because PlayMusic(path) with the same music currently playing will load it again and play from the beginning.

Maybe this behaviour should be changed? Or provide an optional parameter for playing from start.

Or is there a way I'm not aware of?


Soap(Posted 2011) [#2]
Is there any way to pause and resume music? We can change the octaves, active pan, but not pause and resume? :\


Samah(Posted 2011) [#3]
This is really target-dependent. I'm not sure that all targets would support it.


Soap(Posted 2011) [#4]
Ok, I went through every single native code file, and it turns out the exact opposite is true Samah. :\ For pausing and resuming of games, every single platform has a way to pause and resume audio channels. For instance, the Flash target has:

	internal function OnSuspend():void{
		for( var i:int=0;i<33;++i ){
			var chan:gxtkChannel=channels[i];
			if( chan.channel ){
				chan.pausepos=chan.channel.position;
				chan.channel.stop();
			}
		}
	}
	
	internal function OnResume():void{
		for( var i:int=0;i<33;++i ){
			var chan:gxtkChannel=channels[i];
			if( chan.channel ){
				chan.channel=chan.sample.sound.play( chan.pausepos,chan.loops,chan.transform );
			}
		}
	}


So based on that code I made this:

	public function PauseChannel( channel:int ):int{
		var chan:gxtkChannel=channels[channel];
		
		if( chan.channel ){
			chan.pausepos=chan.channel.position;
			chan.channel.stop();
			return 1;
		}
		
		return 0;
	}
	
	public function ResumeChannel( channel:int ):int{
		var chan:gxtkChannel=channels[channel];
		
		if( chan.channel ){
			chan.channel=chan.sample.sound.play( chan.pausepos,chan.loops,chan.transform );
			return 1;
		}
		
		return 0;
	}


And I added "Method PauseChannel( channel )" and "Method ResumeChannel( channel ) " the AudioDevice class and then added needed functions at the bottom of the audio.monkey file. And every other target has something just like Flash's "OnSuspend" and "OnResume".

Where do we petition for new features again?


anawiki(Posted 2011) [#5]
Great to see this thread! Maybe it will motivate Mark to add those commands officially to Monkey, because he already received iOS version of this functions and others from me :)


Pudsy(Posted 2011) [#6]
Nice work. I'd also like to see these added as official commands :)

There'd probably need to be some additional code to keep track of which channels are actually paused via the new commands so that OnResume() doesn't automatically fire them all off again.
ie. it should only auto-resume the ones what weren't manually paused before OnSuspend()


Soap(Posted 2011) [#7]
Apparently this wasn't worth of getting included with the latest update. Neither was making ChannelState return anything other than -1... Here's the 3 extra lines of code that actually make it usable.

public function ChannelState( channel:int ):int{
		var chan:gxtkChannel=channels[channel];
		if( chan.channel )
			return 1;
		return -1;
	}



marksibly(Posted 2011) [#8]
Hi,

> Apparently this wasn't worth of getting included with the latest update.

I will look at adding these soon, but audio is always fiddly and I want to make sure I do it right. I have to make sure it works with normal suspend/resume behavior AND that playing/pausing/resuming/stopping a paused channel all behave identically on all 5 targets. Your code above probably wont be enough.

> Neither was making ChannelState return anything other than -1... Here's the 3 extra lines of code that actually make it usable.

I don't think that'll work - if the sound ends normally without the use of StopChannel, chan.channel will still be non-null, even though ChannelState should return 0.

I tried comparing channel.position with sample.length to see if playback had reached the end of the sample, but weirdly it never quite did, even when sample had gone quiet (got close though). The error margin seemed to be related to update rate too, so I was worried about doing anything too clever here.


TheRedFox(Posted 2011) [#9]
Yes, even like it is now, strange things happening with glfw at times...