2012-12-29 34 views
6

Mein Code mp3 in wav zu konvertieren ist:mp3 zu wav Konvertierung in Java

package audio1; 

import java.io.File; 
import javax.sound.sampled.AudioFileFormat; 
import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 

public class NewClass { 
    public static void main(String [] args){ 
     try{ 
      AudioFileFormat inputFileFormat = AudioSystem.getAudioFileFormat(new File("c:\\1.mp3")); 
      AudioInputStream ais = AudioSystem.getAudioInputStream(new File("c:\\1.mp3")); 

      AudioFormat audioFormat = ais.getFormat(); 

      System.out.println("File Format Type: "+inputFileFormat.getType()); 
      System.out.println("File Format String: "+inputFileFormat.toString()); 
      System.out.println("File lenght: "+inputFileFormat.getByteLength()); 
      System.out.println("Frame length: "+inputFileFormat.getFrameLength()); 
      System.out.println("Channels: "+audioFormat.getChannels()); 
      System.out.println("Encoding: "+audioFormat.getEncoding()); 
      System.out.println("Frame Rate: "+audioFormat.getFrameRate()); 
      System.out.println("Frame Size: "+audioFormat.getFrameSize()); 
      System.out.println("Sample Rate: "+audioFormat.getSampleRate()); 
      System.out.println("Sample size (bits): "+audioFormat.getSampleSizeInBits()); 
      System.out.println("Big endian: "+audioFormat.isBigEndian()); 
      System.out.println("Audio Format String: "+audioFormat.toString()); 

      AudioInputStream encodedASI = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, ais); 

      try{ 
       int i = AudioSystem.write(encodedASI, AudioFileFormat.Type.WAVE, new File("c:\\converted.wav")); 
       System.out.println("Bytes Written: "+i); 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 

Aber ich folgende Ausgabe bekommen:

File Format Type: MP3 
File Format String: MP3 (.mp3) file, byte length: 9631340, data format: MPEG1L3 48000.0 Hz, unknown bits per sample, stereo, unknown frame size, 41.666668 frames/second, , frame length: 10030 
File lenght: 9631340 
Frame length: 10030 
Channels: 2 
Encoding: MPEG1L3 
Frame Rate: 41.666668 
Frame Size: -1 
Sample Rate: 48000.0 
Sample size (bits): -1 
Big endian: true 
Audio Format String: MPEG1L3 48000.0 Hz, unknown bits per sample, stereo, unknown frame size, 41.666668 frames/second, 
java.lang.ArrayIndexOutOfBoundsException: 1 
     at org.tritonus.sampled.convert.javalayer.MpegFormatConversionProvider$DecodedMpegAudioInputStream$DMAISObuffer.append(MpegFormatConversionProvider.java:386) 
     at javazoom.jl.decoder.Obuffer.appendSamples(Unknown Source) 
     at javazoom.jl.decoder.SynthesisFilter.compute_pcm_samples(Unknown Source) 
     at javazoom.jl.decoder.SynthesisFilter.calculate_pcm_samples(Unknown Source) 
     at javazoom.jl.decoder.LayerIIIDecoder.decode(Unknown Source) 
     at javazoom.jl.decoder.LayerIIIDecoder.decodeFrame(Unknown Source) 
     at javazoom.jl.decoder.Decoder.decodeFrame(Unknown Source) 
     at org.tritonus.sampled.convert.javalayer.MpegFormatConversionProvider$DecodedMpegAudioInputStream.execute(MpegFormatConversionProvider.java:307) 
     at org.tritonus.share.TCircularBuffer.read(TCircularBuffer.java:138) 
     at org.tritonus.share.sampled.convert.TAsynchronousFilteredAudioInputStream.read(TAsynchronousFilteredAudioInputStream.java:194) 
     at javax.sound.sampled.AudioInputStream.read(AudioInputStream.java:292) 
     at com.sun.media.sound.PCMtoPCMCodec$PCMtoPCMCodecStream.read(PCMtoPCMCodec.java:506) 
     at com.sun.media.sound.SunFileWriter$NoCloseInputStream.read(SunFileWriter.java:199) 
     at java.io.SequenceInputStream.read(SequenceInputStream.java:208) 
     at java.io.SequenceInputStream.read(SequenceInputStream.java:211) 
     at java.io.InputStream.read(InputStream.java:101) 
     at com.sun.media.sound.WaveFileWriter.writeWaveFile(WaveFileWriter.java:247) 
     at com.sun.media.sound.WaveFileWriter.write(WaveFileWriter.java:145) 
     at javax.sound.sampled.AudioSystem.write(AudioSystem.java:1354) 
     at audio1.NewClass.main(NewClass.java:33) 

Kann mir jemand helfen, was ich falsch mache?

+0

Es existiert Java-Konverter und es ist Quelle ist [diese] (http://code.google.com/p/mp3transform/ source/browse/Stamm/mp3/src/mp3/wav/WavConverter.java? r = 6). Kann sein, dass Sie einfach die Quelle kopieren und einfügen sollten? – Daniil

+0

Ich würde sagen, das eigentliche Problem ist die Quelldatei. Probiere einige der auf meiner [Medienseite] (http://pscode.org/media/#sound) verfügbaren MP3s aus. –

+0

Könnte Ihre MP3 eine variable Bitrate haben? Vielleicht ist der Media-Decoding-Code nicht so fortschrittlich –

Antwort

9
public static byte [] getAudioDataBytes(byte [] sourceBytes, AudioFormat audioFormat) throws UnsupportedAudioFileException, IllegalArgumentException, Exception{ 
     if(sourceBytes == null || sourceBytes.length == 0 || audioFormat == null){ 
      throw new IllegalArgumentException("Illegal Argument passed to this method"); 
     } 

     ByteArrayInputStream bais = null; 
     ByteArrayOutputStream baos = null; 
     AudioInputStream sourceAIS = null; 
     AudioInputStream convert1AIS = null; 
     AudioInputStream convert2AIS = null; 

     try{ 
      bais = new ByteArrayInputStream(sourceBytes); 
      sourceAIS = AudioSystem.getAudioInputStream(bais); 
      AudioFormat sourceFormat = sourceAIS.getFormat(); 
      AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels()*2, sourceFormat.getSampleRate(), false); 
      convert1AIS = AudioSystem.getAudioInputStream(convertFormat, sourceAIS); 
      convert2AIS = AudioSystem.getAudioInputStream(audioFormat, convert1AIS); 

      baos = new ByteArrayOutputStream(); 

      byte [] buffer = new byte[8192]; 
      while(true){ 
       int readCount = convert2AIS.read(buffer, 0, buffer.length); 
       if(readCount == -1){ 
        break; 
       } 
       baos.write(buffer, 0, readCount); 
      } 
      return baos.toByteArray(); 
     } catch(UnsupportedAudioFileException uafe){ 
      //uafe.printStackTrace(); 
      throw uafe; 
     } catch(IOException ioe){ 
      //ioe.printStackTrace(); 
      throw ioe; 
     } catch(IllegalArgumentException iae){ 
      //iae.printStackTrace(); 
      throw iae; 
     } catch (Exception e) { 
      //e.printStackTrace(); 
      throw e; 
     }finally{ 
      if(baos != null){ 
       try{ 
        baos.close(); 
       }catch(Exception e){ 
       } 
      } 
      if(convert2AIS != null){ 
       try{ 
        convert2AIS.close(); 
       }catch(Exception e){ 
       } 
      } 
      if(convert1AIS != null){ 
       try{ 
        convert1AIS.close(); 
       }catch(Exception e){ 
       } 
      } 
      if(sourceAIS != null){ 
       try{ 
        sourceAIS.close(); 
       }catch(Exception e){ 
       } 
      } 
      if(bais != null){ 
       try{ 
        bais.close(); 
       }catch(Exception e){ 
       } 
      } 
     } 
    } 

Hier steht sourceBytes für MP3-Datei oder WAV-Datei. audioFormat ist das PCM-Format, in das Sie konvertieren möchten. Außerdem müssen wir mp3spi.jar, tritonus_mp3.jar, jl * .jar, tritonus_share.jar von javazoom.com im Klassenpfad ablegen. Hoffe das kann anderen helfen.

Java 7 Version:

public static byte [] getAudioDataBytes(byte [] sourceBytes, AudioFormat audioFormat) throws UnsupportedAudioFileException, IllegalArgumentException, Exception { 
    if(sourceBytes == null || sourceBytes.length == 0 || audioFormat == null){ 
     throw new IllegalArgumentException("Illegal Argument passed to this method"); 
    } 

    try (final ByteArrayInputStream bais = new ByteArrayInputStream(sourceBytes); 
     final AudioInputStream sourceAIS = AudioSystem.getAudioInputStream(bais)) { 
     AudioFormat sourceFormat = sourceAIS.getFormat(); 
     AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels()*2, sourceFormat.getSampleRate(), false); 
     try (final AudioInputStream convert1AIS = AudioSystem.getAudioInputStream(convertFormat, sourceAIS); 
      final AudioInputStream convert2AIS = AudioSystem.getAudioInputStream(audioFormat, convert1AIS); 
      final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { 
      byte [] buffer = new byte[8192]; 
      while(true){ 
       int readCount = convert2AIS.read(buffer, 0, buffer.length); 
       if(readCount == -1){ 
        break; 
       } 
       baos.write(buffer, 0, readCount); 
      } 
      return baos.toByteArray(); 
     } 
    } 
} 

Maven:

<dependency> 
    <groupId>com.googlecode.soundlibs</groupId> 
    <artifactId>mp3spi</artifactId> 
    <version>1.9.5-1</version> 
</dependency> 
<dependency> 
    <groupId>com.googlecode.soundlibs</groupId> 
    <artifactId>jlayer</artifactId> 
    <version>1.0.1-1</version> 
    <exclusions> 
     <exclusion> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
     </exclusion> 
    </exclusions> 
</dependency> 
+2

nur ein wirklich gutes Beispiel dafür, wie awesome Try-mit-Ressourcen ist: P. Ersetzen Sie die gesamten Closing-Sachen durch einen Versuch (// instanziate Closeables; ...; ...;) { } catch (Ex ...) {}. und der Code ist eine Schönheit. –

+1

@MartinBraun wahr .. mein Code ist zu lang ..: P – UDPLover

+0

Nicht sicher, ich verstehe diesen Code. Wie geben wir an, dass das zurückgegebene Byte-Array Daten im WAV-Format sein wird? – IcedDante