Sunday, 15 February 2015

events - Java Wav file Error (javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file) -



events - Java Wav file Error (javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file) -

i have 2 classes in java project 1 is

rebound

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class rebound { public static void main (string[] args) { jframe frame = new jframe ("rebound"); frame.setdefaultcloseoperation (jframe.exit_on_close); frame.getcontentpane().add(new reboundpanel()); frame.pack(); frame.setvisible(true); } }

and sec 1 reboundpanel

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.sound.sampled.*; import java.io.file; import java.io.ioexception; import javax.sound.sampled.audioformat; import javax.sound.sampled.audioinputstream; import javax.sound.sampled.audiosystem; import javax.sound.sampled.clip; import javax.sound.sampled.lineunavailableexception; import javax.sound.sampled.sourcedataline; import javax.sound.sampled.dataline; import javax.sound.sampled.lineevent; import javax.sound.sampled.linelistener; public class reboundpanel extends jpanel { private final int width = 600, height = 300; private final int delay = 20, image_size = 35; private imageicon image1, image2, image3; private timer timer; private int x1, y1, movex1, movey1, x2, y2, movex2, movey2, x3, y3; clip clip; dataline.info info; public reboundpanel() throws exception { timer = new timer(delay, new reboundlistener()); image1 = new imageicon ("happyface1.gif"); image2 = new imageicon ("happyface2.gif"); x1 = 0; y1 = 100; x2 = 40; y2 = 0; movex1 = movey1 = 3; movex2 = movey2 = 5; setpreferredsize (new dimension(width, height)); setbackground (color.black); timer.start(); image3 = new imageicon ("fire.gif"); file soundfile = new file("explosion.wav"); audioinputstream sound = audiosystem.getaudioinputstream(soundfile); dataline.info info = new dataline.info (clip.class, sound.getformat()); clip = (clip) audiosystem.getline(info); clip.open(sound); clip.start(); } public void paintcomponent (graphics page) { super.paintcomponent (page); image1.painticon (this, page, x1, y1); image2.painticon (this, page, x2, y2); image3.painticon (this, page, x2, y2); } private class reboundlistener implements actionlistener { public void actionperformed (actionevent event) { x1 += movex1; y1 += movey1; x2 += movex2; y2 += movey2; if (x1 <= 0 || x1 >= width-image_size) movex1 = movex1 * -1; if (y1 <= 0 || y1 >= height-image_size) movey1 = movey1 * -1; if (x2 <= 0 || x2 >= width-image_size) movex2 = movex2 * -1; if (y2 <= 0 || y2 >= height-image_size) movey2 = movey2 * -1; if (math.abs(y1-y2) <= image_size-2 && math.abs(x1-x2)<= image_size-2) { movey2 = movey2 * -1; movex2 = movex2 * -1; movey1 = movey1 * -1; movex1 = movex1 * -1; x3=x2; y3=y2-2*image_size; clip.loop(clip.loop_continuously); } if (math.abs(y1-y2) >= 4*image_size && math.abs(x1-x2) >= 4*image_size) { x3=-200; y3=-200; if (clip.isrunning()) clip.stop(); } repaint(); } } }

when seek run programme next error

javax.sound.sampled.unsupportedaudiofileexception: not sound input stream input file @ javax.sound.sampled.audiosystem.getaudioinputstream(audiosystem.java:1187) @ reboundpanel.<init>(reboundpanel.java:47) @ rebound.main(rebound.java:21) exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ reboundpanel$reboundlistener.actionperformed(reboundpanel.java:99) @ javax.swing.timer.fireactionperformed(timer.java:312) @ javax.swing.timer$dopostevent.run(timer.java:244) @ java.awt.event.invocationevent.dispatch(invocationevent.java:251) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:721) @ java.awt.eventqueue.access$200(eventqueue.java:103) @ java.awt.eventqueue$3.run(eventqueue.java:682) @ java.awt.eventqueue$3.run(eventqueue.java:680) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:691) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:244) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:163) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:151) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:147) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:139) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:97)

according code goal create 2 happy faces move in frame , when both collide sound along image of fire/explosion should show , disappear

please help have show project instructor...

just because file wav doesn't mean supported javasound. wav file container format, , actual raw info (the samples) may in different formats, including signed/unsigned pcm @ variety of bit depths or may in compressed format. can't find compatibility table javasound, compatibility table jmf (which give idea) here:

http://www.oracle.com/technetwork/java/javase/formats-138492.html

the solution convert wav file more generic format, 16-bit pcm using conversion programme can read current file (sox, adacity, quicktime, protools, etc).

to more precise, may want create wav file bit depth of 16-bits (little endian -- default, if don't see don't worry), stereo or mono should work. may want consider mutual sample rates such 44,100 hz, or 48,000 hz (most computers nowadays should back upwards both rates, former more common).

java events audio javasound

No comments:

Post a Comment