java - audio reading in jar executable file -


in eclipse when run it, works fine , audio okay, have problem when create .jar executable file. audio file in package , read getresourceasstream want let know. here problem..

inputstream input = getclass().getresourceasstream("/optician/funny-doorbell.wav"); audioinputstream audioin; try{ clip clip; audioin = audiosystem.getaudioinputstream(input); clip=audiosystem.getclip(); clip.open(audioin); clip.start(); } catch (unsupportedaudiofileexception | ioexception e1) { e1.printstacktrace(); } catch (lineunavailableexception e1) { e1.printstacktrace(); } 

in first case when run eclipse, works fine, when run .jar executable file : reset/mark not supported.

second case same :

bufferedinputstream input = (bufferedinputstream) getclass().getresourceasstream("/optician/funny-doorbell.wav"); 

so same, point try bufferedinputstream problem : exception in thread "awt-eventqueue-0" java.lang.classcastexception: sun.new.www.protocol.jar.jarurlconnection$jarurlinputstream cannot cast java.io.bufferedinputstream

i tried in linux , windows doesn't works. problem ?

i think question has been asked , answered before. see accepted answer here detailed explanation: java.io.ioexception: mark/reset not supported

that said, believe can fix code modifying first line follows:

inputstream input = new bufferedinputstream(getclass().getresourceasstream("/optician/funny-doorbell.wav")); 

the reason you're seeing difference in behavior in eclipse, getresourceasstream returning inputstream supports read/mark. when run out of jar, you're getting implementation of inputstream not support read/mark (jarurlinputstream).

if wrap returned input stream in new bufferedinputstream, you'll have read/mark support in stream when you're running in jar , code work everywhere.

also, you're getting classcastexception because you're trying cast input stream returned getresourceasstream() bufferedinputstream. don't cast it; instead, wrap returned input stream in new bufferedinputstream() did in code snippet above.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -