java - javax.sound.sampled.UnsupportedAudioFileException playing OGG files with javazoom -
i'm attempting play ogg audio files using answer question here: playing ogg files in eclipse
using javazoom library downloaded , configured project, attempting use xav's answer code:
import java.io.file; import javax.sound.sampled.audioformat; import javax.sound.sampled.audioinputstream; import javax.sound.sampled.audiosystem; import javax.sound.sampled.dataline; import javax.sound.sampled.sourcedataline; import javazoom.spi.propertiescontainer; public class oggplayer { public final string filename; public boolean muststop = false; public oggplayer(string pfilename) { filename = pfilename; } public void play() throws exception { muststop = false; file file = new file(filename); audioinputstream audioinputstream = audiosystem.getaudioinputstream(file); if (audioinputstream == null) { throw new exception("unable audio input stream"); } audioformat baseformat = audioinputstream.getformat(); audioformat decodedformat = new audioformat(audioformat.encoding.pcm_signed, baseformat.getsamplerate(), 16, baseformat.getchannels(), baseformat.getchannels() * 2, baseformat.getsamplerate(), false); audioinputstream decodedaudioinputstream = audiosystem.getaudioinputstream(decodedformat, audioinputstream); if (!(decodedaudioinputstream instanceof propertiescontainer)) { throw new exception("wrong propertiescontainer instance"); } dataline.info datalineinfo = new dataline.info(sourcedataline.class, decodedformat); sourcedataline sourcedataline = (sourcedataline) audiosystem.getline(datalineinfo); sourcedataline.open(decodedformat); byte[] tempbuffer = new byte[4096]; // start sourcedataline.start(); int nbreadbytes = 0; while (nbreadbytes != -1) { if (muststop) { break; } nbreadbytes = decodedaudioinputstream.read(tempbuffer, 0, tempbuffer.length); if (nbreadbytes != -1) sourcedataline.write(tempbuffer, 0, nbreadbytes); } // stop sourcedataline.drain(); sourcedataline.stop(); sourcedataline.close(); decodedaudioinputstream.close(); audioinputstream.close(); } public void setmuststop(boolean pmuststop) { muststop = pmuststop; } public void stop() { muststop = true; } }
the code makes sense me, having trouble using it. can't seem class read ogg file, , i've modified various ways in attempts make function. read wav files in same project, reading files with
audioinputstream audioin = audiosystem.getaudioinputstream(thread.currentthread().getcontextclassloader().getresource("res/audio/" + filename + ".wav"));
i've attempted variations of this, using file class, reading resources, , can locate ogg file in project, keep throwing exception
javax.sound.sampled.unsupportedaudiofileexception: not audio input stream input url
this fourth or fifth attempt in getting audio play non-wav audio in project. tried playing mp3s mediaplayer , few other ways, , second method method of trying play ogg files. i'm trying play large, long music tracks background, , cannot run them other wav sound effects due file size.
any in playing these ogg files immensely appreciated. feel close goal attempt, seems simple , solid, refuses read files correctly. thank in advance assistance.
here's method read mp3 , ogg files - play files vorbis format not flac; have put check there - not files ending .ogg supported:
import javazoom.spi.vorbis.sampled.file.vorbisaudiofilereader; import javazoom.spi.mpeg.sampled.file.mpegaudiofilereader; import org.tritonus.share.sampled.file.taudiofilereader; import org.tritonus.sampled.file.auaudiofilereader; audioinputstream createoggmp3(file filein) throws ioexception, exception { audioinputstream audioinputstream=null; audioformat targetformat=null; try { audioinputstream in=null; if(filein.getname().endswith(".ogg")) { vorbisaudiofilereader vb=new vorbisaudiofilereader(); in=vb.getaudioinputstream(filein); } else if(filein.getname().endswith(".mp3")) { mpegaudiofilereader mp=new mpegaudiofilereader(); in=mp.getaudioinputstream(filein); } audioformat baseformat=in.getformat(); targetformat=new audioformat( audioformat.encoding.pcm_signed, baseformat.getsamplerate(), 16, baseformat.getchannels(), baseformat.getchannels() * 2, baseformat.getsamplerate(), false); audioinputstream=audiosystem.getaudioinputstream(targetformat, in); } catch(unsupportedaudiofileexception ue) { system.out.println("\nunsupported audio"); } return audioinputstream; }
it returns audio input stream can use follows:
if(filein.getname().endswith(".ogg") || filein.getname().endswith(".mp3")) { audioinputstream=createoggmp3(filein); } else { // wav audioinputstream=audiosystem.getaudioinputstream(filein); }
your decoded audio format is
decodedformat=audioinputstream.getformat();
and can continue sourcedataline.
Comments
Post a Comment