06. december 2002 - 22:57Der er
22 kommentarer og 1 løsning
lyd i applikation
Nu har jeg set på en del eksempler herinde fra og der er ingen jeg kan få til at virke. Jeg skal bruge en stump kode der kan afspille en .wav fil der ligger samme sted som koden ligger.
du skal blot have en audio fil på din hd. Jeg kan kun få det til at virke med en .wav fil
du angiver filnavnet i denne linje: String strFilename = "c:\\joke.wav";
/* * SimpleAudioPlayer.java * * This file is part of the Java Sound Examples. */
/* * Copyright (c) 1999 - 2001 by Matthias Pfisterer <Matthias.Pfisterer@web.de> * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as published * by the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */
public class SimpleAudioPlayer { private static final int EXTERNAL_BUFFER_SIZE = 128000;
public static void main(String[] args) { /* We check that there is exactely one command-line argument. If not, we display the usage message and exit. */
/* Now, that we're shure there is an argument, we take it as the filename of the soundfile we want to play. */ String strFilename = "c:\\joke.wav"; //String strFilename = args[0]; File soundFile = new File(strFilename);
/* We have to read in the sound file. */ AudioInputStream audioInputStream = null; try { audioInputStream = AudioSystem.getAudioInputStream(soundFile); } catch (Exception e) { /* In case of an exception, we dump the exception including the stack trace to the console output. Then, we exit the program. */ e.printStackTrace(); System.exit(1); }
/* From the AudioInputStream, i.e. from the sound file, we fetch information about the format of the audio data. These information include the sampling frequency, the number of channels and the size of the samples. These information are needed to ask Java Sound for a suitable output line for this audio file. */ AudioFormat audioFormat = audioInputStream.getFormat();
/* Asking for a line is a rather tricky thing. We have to construct an Info object that specifies the desired properties for the line. First, we have to say which kind of line we want. The possibilities are: SourceDataLine (for playback), Clip (for repeated playback) and TargetDataLine (for recording). Here, we want to do normal playback, so we ask for a SourceDataLine. Then, we have to pass an AudioFormat object, so that the Line knows which format the data passed to it will have. Furthermore, we can give Java Sound a hint about how big the internal buffer for the line should be. This isn't used here, signaling that we don't care about the exact size. Java Sound will use some default value for the buffer size. */ SourceDataLine line = null; DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat); try { line = (SourceDataLine) AudioSystem.getLine(info);
/* The line is there, but it is not yet ready to receive audio data. We have to open the line. */ line.open(audioFormat); } catch (LineUnavailableException e) { e.printStackTrace(); System.exit(1); } catch (Exception e) { e.printStackTrace(); System.exit(1); }
/* Still not enough. The line now can receive data, but will not pass them on to the audio output device (which means to your sound card). This has to be activated. */ line.start();
/* Ok, finally the line is prepared. Now comes the real job: we have to write data to the line. We do this in a loop. First, we read data from the AudioInputStream to a buffer. Then, we write from this buffer to the Line. This is done until the end of the file is reached, which is detected by a return value of -1 from the read method of the AudioInputStream. */ int nBytesRead = 0; byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; while (nBytesRead != -1) { try { nBytesRead = audioInputStream.read(abData, 0, abData.length); } catch (IOException e) { e.printStackTrace(); } if (nBytesRead >= 0) { int nBytesWritten = line.write(abData, 0, nBytesRead); } }
/* Wait until all data are played. This is only necessary because of the bug noted below. (If we do not wait, we would interrupt the playback by prematurely closing the line and exiting the VM.)
Thanks to Margie Fitch for bringing me on the right path to this solution. */ line.drain();
/* All data are played. We can close the shop. */ line.close();
/* There is a bug in the jdk1.3. It prevents correct termination of the VM. So we have to exit ourselves. */ System.exit(0); } }
Til olly: er du blind - må ikke bruge java.applet pakken. Til r9: Din kopieret koden giver følgende exception: javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:752) at LydTest2.main(LydTest2.java:22)
når jeg prøver med en mp3 fil, så får jeg følgende:
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:756) at SimpleAudioPlayer.main(SimpleAudioPlayer.java:83) Process terminated with exit code 1
MEN du skulle jo bruge en wav fil, og så virker koden fint.
public class UnsupportedAudioFileException extends Exception
An UnsupportedAudioFileException is an exception indicating that an operation failed because a file did not contain valid data of a recognized file type and format.
Since: 1.3
Og så må jeg hellere sige tak for i aften... Da der er en der ikke værdsætter hjælp...
Har selv fundet ud af det. Det var fordi den wav fil jeg brugte ikke var samplet i PCM. Udemærket r9. Men jeg har ikke helt fået mit svar endnu - hvad skal der stå i String strFilename = "???.wav"; - når jeg skal afspille en fil der ligger samme sted som koden???
// Method to play audioClip when alarm is active public static AudioClip getClip(String name) { URL u = null; AudioClip clip = null; try { u = new URL(name); clip = Applet.newAudioClip(u); }catch(Exception e){System.out.println(e.getMessage()); } return clip; } //Slut på metoden AudioClip clip = getClip("file:/c:/programmering/java/winmon/Alarm.wav");
Tjaaa jeg har erfaret at der er forskel. Serialiserede filer kan godt ligge i samme katalog, og behøver ingen sti. WAW, og XML kræver fuld sti. sådan har det været her. QD::
SourceDataLine line = null; DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat); try { line = (SourceDataLine) AudioSystem.getLine(info);
quaid: Jeg har ændret i koden som jeg postede 07/12-2002 00:33:33 i forhold til den kode jeg postede 06/12-2002 23:07:03. Jeg har aldrig sagt at det er mig som har lavet eksemplet.
halden ønskede at man kunne afspille en wav fil som ligger samme sted som hvor koden ligger. Den kan man med de 2 linje som jeg indsatte i koden:
URL url = this.getClass().getClassLoader().getResource("idiot.wav"); File soundFile = new File(url.getFile());
Det har jeg heller ikke anfægtet, kunne bare se at det kom derfra. Havde bare ikke set forandringen. Det vigtigste var jo at det virkede. Vil selv prøve den løsning, for det har ofte irriteret mig at man skulle give den en sti som så kunne give problemer hvis classem skulle køres på en anden maskine. QD::
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.