Skip to main content
gamedev.net
🔒 Locked

[java] Java Applet playing Midi!?

Started by Thanhda 0 replies 750+ views
Original Post
Thanhda
Thanhda
okay this is annoying me, for some time, i wanted to get simple code to play midis in java! and i have found only 1 place that works, but you got to press a button to make it play. i've tried numerous times to get it to play on its own but cant seem to get it to work, if you can help me out that would be great. heres the code http://www.iam.unibe.ch/~scg/Resources/Java/Mirror/JavaTutorial11052000/sound/playing.html
Thanhda TieDigital Shock, Technical Director & Business Relations15-75 Bayly Street West, Suite #173Ajax, Ontario, L1S 7K7Tel: 416.875.1634http://www.digitalshock.net
Darragh
Darragh
That applet doesn't seem to work for me either.

I have some code however for playing Midi files- its simple enough to do. Just pass in the name of the midi file to the playSong() method and you're away:

import java.applet.AudioClip;import java.applet.Applet;import java.net.URL;import java.io.File;class MidiMusic{static AudioClip currentMusic;static void endSong(){if	( currentMusic != null )	{	currentMusic.stop();	}}static void playSong(String midiFile){currentMusic = loadSound(midiFile);if	( currentMusic != null )	{	currentMusic.loop();	System.out.println("Now playing song:"+midiFile);	}}static AudioClip loadSound(String fileName){AudioClip loadedSound = null;try	{		File soundFile = new File(fileName);		URL fileURL = soundFile.toURL();		loadedSound = Applet.newAudioClip(fileURL);	}	catch ( Exception e )	{		System.out.println("Couldn't load sound '" + fileName + "'");		}return(loadedSound);}}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.