ChannelState -1

Monkey Targets Forums/Android/ChannelState -1

Rone(Posted 2012) [#1]
HI,

I just stumbled int this ChannelState is always -1 thing. I solved it by simply comparing the time duration with the sample length...

Are there any drawbacks by doing it that way?

class gxtkChannel{
	int stream;		//SoundPool stream ID, 0=none
	float volume=1;
	float rate=1;
	float pan;
	int state;
	int milli;	// timestamp
	int lastLength; // length of the last played sample
};


gxtkSample LoadSample( String path ){
	
		StopMusic();
		MediaPlayer  mp=MonkeyData.openMedia( path );
		//mp.prepare();
		int length = mp.getDuration();
		mp.release();

	
		gxtkSample.FlushDiscarded( pool );
		int sound=MonkeyData.loadSound( path,pool );
		if( sound!=0 ) return new gxtkSample( sound, length );
		return null;
	}


int PlaySample( gxtkSample sample,int channel,int flags ){
		gxtkChannel chan=channels[channel];
		if( chan.stream!=0 ) pool.stop( chan.stream );
		float rv=(chan.pan * .5f + .5f) * chan.volume;
		float lv=chan.volume-rv;
		int loops=(flags&1)!=0 ? -1 : 0;

		//chan.stream=pool.play( sample.sound,lv,rv,0,loops,chan.rate );
		//chan.state=1;
		//return 0;
		
		//Ugly as hell, but seems to work for now...pauses 10 secs max...
		for( int i=0;i<100;++i ){
			chan.stream=pool.play( sample.sound,lv,rv,0,loops,chan.rate );
			if( chan.stream!=0 ){
				chan.state=1;
				chan.milli = MonkeyGame.app.MilliSecs();
				chan.lastLength = (int)((float)sample.length / chan.rate);
				return 0;
			}
//			throw new Error( "PlaySample failed to play sound" );
			try{
				Thread.sleep( 100 );
			}catch( java.lang.InterruptedException ex ){
			}
		}
		throw new Error( "PlaySample failed to play sound" );
	}


int ChannelState( int channel ){
		gxtkChannel chan=channels[channel];
		return (chan.state == 2 || chan.state == -1 )? chan.state : 
			MonkeyGame.app.MilliSecs() - chan.milli < chan.lastLength ? 1 : 0;
	}


class gxtkSample{

	int sound;
	int length;
	
	static Vector discarded=new Vector();
	
	gxtkSample( int sound, int length ){
		this.sound=sound;
		this.length = length;
	}



caffeinekid(Posted 2013) [#2]
I just ran into this too. Isn't it possible that Mark can fix it?

Or is it a platform problem?