PlayMusic

Monkey Forums/Monkey Programming/PlayMusic

therevills(Posted 2011) [#1]
Ive been trying to get the PlayMusic command to work on Android... and Ive finally got it to work by changing my sound from an ogg to an mp3...

Is this right?


dave.h(Posted 2011) [#2]
i read somewhere that we arnt allowed to use mp3 files in our programs because of some sort of licence agreement which if true would make using mp3 in andriod a dodgy issue i hope im wrong about this


therevills(Posted 2011) [#3]
Yeah mp3s has got a strange licence...

Okay... When I try this code:



In HTML5, I hear a loud "Ahoy there", then "ahoy there" quieter repeated, in Android I hear "Ahoy there" and a previous sound file (intro.mp3)... it seems it doesnt clear out of memory and it doesnt want to play "Ahoy there" as music...


therevills(Posted 2011) [#4]
Has anyone had success with PlayMusic? Im having a hard time with it...


Leo Santos(Posted 2011) [#5]
I've been using it with MP3 and seems to work fine with iOS and HTML5, don't remember if it works with GLFW and never tried on Android. Nothing fancy, just playing and stopping the music and fading in and out using SetMusicVolume().


therevills(Posted 2011) [#6]
Yeah HTML5 looks good.... but Android is giving me a headache, if I play an ogg it doesnt work, then if I play a mp3 it works, I then try the ogg and it plays!?!?!

I thought it must be something with Diddy, so I wrote a simple Mojo app and it works with mp3 in Android (with the above problems). I then wrote a simple Diddy app which works with mp3 in Android(again with the above problems)... so I copied the code and put it into my game and it doesnt work!!

With the following code, it displays -1:
Field musicOk:Int
...
musicOk = PlayMusic("music/test.mp3")
...
DrawText musicOk, 10, 10


Looking thru the generated code this means it couldnt load the music - but its the same code which does work in a simple app!!!! ARRRGGHHH!!

Mojo's PlayMusic

	int PlayMusic( String path,int flags ){
		StopMusic();
		music=MonkeyData.openMedia( path );
		if( music==null ) return -1;
		music.setLooping( (flags&1)!=0 );
		music.setVolume( musicVolume,musicVolume );
		music.start();
		return 0;
	}


Which in turns calls MonkeyGame.java openMedia:

	static MediaPlayer openMedia( String path ){
		path="monkey/"+path;

		try{
			MediaPlayer mp=new MediaPlayer();
			mp.setDataSource( getAssets().openFd( path ).getFileDescriptor() );
			mp.prepare();
			return mp;
		}catch( IOException e ){
		}
		return null;
	}



skid(Posted 2011) [#7]
That code looks wrong, I would have thought a mediaplayer must be in prepared state before start is called.


therevills(Posted 2011) [#8]
Isnt it?

Isnt this the order of the code:

PlayMusic > music=MonkeyData.openMedia( path ) > mp.prepare() > music.start()


Midimaster(Posted 2011) [#9]
In HTML5, I hear a loud "Ahoy there", then "ahoy there" quieter repeated, in Android I hear "Ahoy there" and a previous sound file (intro.mp3)... it seems it doesnt clear out of memory and it doesnt want to play "Ahoy there" as music...



I recognized the same problems in HTML5 and published the app "MonkeyPiano" to find out, whether the community can confirm this, but did not get any responses. If you play the same sound often it seems to "add" the waves. You can hear this in getting louder when repeating the sounds.

You can test this behaviour with the MonkeyPiano-app here in apps-board.


therevills(Posted 2011) [#10]
Hey Midimaster, I was actually expecting that in HTML5, because Im playing a sound and music with the same file.

Ive tracked down the issue with my game and music... and its something to do with resources in Android.



If I have 4 image files and the 1 music file in the data folder, the music wont play. If I remove one of the image files, the music plays.

Im going to raise a bug :P