Dextrell Posted August 11, 2013 Author Share Posted August 11, 2013 ```package com.adventurequest.io.sound;import java.util.logging.Logger;public class Audio{ public static final Logger logger = Logger.getLogger(Audio.class.getName()); private String extensionType; private String fileName; public Audio(String theExtensionType) { if(isValidExtension(theExtensionType)) { setExtensionType(theExtensionType); logger.info("Audio extension " + theExtensionType + " has been constructed"); } else { logger.info("Extension Not supported!"); } } private boolean isValidExtension(String theExtensionType) { if(theExtensionType.equals(AudioExtension.MIDI) || extensionType.equals(AudioExtension.MP3)) { return true; } else { return false; } } private String setExtensionType(String theExtensionType) { if(theExtensionType.equals(AudioExtension.MIDI)) { extensionType = theExtensionType; return extensionType; } return null; } public String getExtensionType() { return extensionType; } public String setFileName(String theFileName) { fileName = theFileName; return fileName; } public String getFileName() { return fileName; } public void play(Midi midiFile) { midiFile.play(); }}``````package com.adventurequest.io.sound;import java.io.File;import java.io.IOException;import javax.sound.midi.InvalidMidiDataException;import javax.sound.midi.MidiSystem;import javax.sound.midi.MidiUnavailableException;import javax.sound.midi.Sequence;import javax.sound.midi.Sequencer;import javax.sound.midi.Synthesizer;/** * Midi file playing. * @author Tony. */public class Midi { /** * File directory. */ private static final String defaultFileDirectory = "res/music/"; private File fileDirectory; private String archiveDirectory; /** * The name of the midi file. */ private String fileName; /** * The Sequence object. */ private Sequence sequence; /** * The Sequencer object. */ private Sequencer sequencer; /** * The Synthesizer object. */ @SuppressWarnings("unused") private Synthesizer synthesizer; /** * Constructs a Midi object using the default file directory. * @param theFileName the name of the file */ public Midi(String theFileName) { archiveDirectory = defaultFileDirectory + theFileName; fileDirectory = new File(archiveDirectory); } /** * Constructs a midi object using the specified filedirectory * @param theFileDirectory specified file directory to the file archive. * @param theFileName name of the midi file. */ public Midi(String theFileDirectory, String theFileName) { archiveDirectory = defaultFileDirectory + theFileDirectory + theFileName; fileDirectory = new File(archiveDirectory); } /** * Sets the name of the midi file * @param theFileName the name of the file * @return fileName */ public String setFileName(String theFileName) { return fileName = theFileName; } /** * Starts the midi file to play. */ public void play() { try { sequence = MidiSystem.getSequence(fileDirectory); // Get default sequencer. sequencer = MidiSystem.getSequencer(); sequencer.setSequence(sequence); sequencer.open(); sequencer.start(); } catch (InvalidMidiDataException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch(MidiUnavailableException e) { e.printStackTrace(); } } /** * Stops the midi file from playing. */ public void stop() { sequencer.stop(); sequencer.close(); } public String getFileName() { return fileName; }}``````package com.adventurequest.io.sound;public abstract class AudioExtension{ public static final String MIDI = ".mid"; public static final String MP3 = ".mp3";}```Pretty simple to use it works fine on my 2D game (currently supports Midi Files only) i will add volume support and mp3 support later on I'm still progressing on this :D Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now