如何解码和播放gsm音频文件?

时间:2014-11-05 12:19:30

标签: java audio gsm javasound tritonus

几周前我已经发布了关于如何播放GSM文件的问题here。 为了使用jcifs解码和回放来自服务器的流,我遇到了很多问题。

以下代码适用于未压缩的音频wav文件,但它不适用于GSM文件。当我尝试播放gsm文件时,我收到以下错误。

要播放GSM文件,我只是将Tritonus jar添加到构建路径

注意:我已在构建路径中添加了以下必需的jar:

请注意,该文件是GSM文件,但扩展名为.wav

tritonus_share-0.3.6.jar Download
tritonus_gsm-0.3.6.jar Download

import java.io.*;
import javax.sound.sampled.*;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;

/**
 * Use SourceDataLine to read sound files from network storage using jcifs.
 */

public class SoundJCIFSLineTest 
{
   public static void main(String[] args) 
   {
      SourceDataLine soundLine = null;

      int BUFFER_SIZE = 64*1024;  // 64 KB

      // Set up an audio input stream piped from the sound file.
      try 
      {
         //Maked the connection fast 
         jcifs.Config.setProperty("resolveOrder", "DNS");

         String originPath="smb://192.168.1.1/audiofolder/audiofile.wav";       

         NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", "username", "password");

         //This is the audio file the needs to be downloaded from the network storage   
         SmbFile audioFile = new SmbFile(originPath, auth);
         SmbFileInputStream smbFileInputStream = new SmbFileInputStream(audioFile);

         BufferedInputStream buf = new BufferedInputStream(smbFileInputStream);

         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(buf);
         AudioFormat audioFormat = audioInputStream.getFormat();

         DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
         soundLine = (SourceDataLine) AudioSystem.getLine(info);
         soundLine.open(audioFormat);
         soundLine.start();

         int nBytesRead = 0;
         byte[] sampledData = new byte[BUFFER_SIZE];

         while (nBytesRead != -1) 
         {
            nBytesRead = audioInputStream.read(sampledData, 0, sampledData.length);
            if (nBytesRead >= 0) 
            {
               // Writes audio data to the mixer via this source data line.
               soundLine.write(sampledData, 0, nBytesRead);
            }
         }
      } 
      catch (UnsupportedAudioFileException ex) 
      {
         ex.printStackTrace();
      } 
      catch (IOException ex) 
      {
         ex.printStackTrace();
      } 
      catch (LineUnavailableException ex) 
      {
         ex.printStackTrace();
      } 
      finally 
      {
         soundLine.drain();
         soundLine.close();
      }
   }
}

错误:

 javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1119)
    at SoundJCIFSLineTest.main(SoundJCIFSLineTest.java:36)
Exception in thread "main" java.lang.NullPointerException
    at SoundJCIFSLineTest.main(SoundJCIFSLineTest.java:71)

这些是我正在尝试播放的文件的一些细节

Channels       : 1
Sample Rate    : 8000
Precision      : 16-bit
Duration       : 00:04:36.92 = 2215360 samples ~ 20769 CDDA sectors
File Size      : 450k
Bit Rate       : 13.0k
Sample Encoding: GSM

0 个答案:

没有答案