How to retrieve audio from network storage

217 views Asked by At

The following code is aimed to read audio files from a network storage and play them back in a swing application

The code makes use of java SourceDataLine and jcifs to retrieve the audio back

The problem is that i need to define the mark/reset for the buffer

Error:

java.io.IOException: mark/reset not supported
    at java.io.InputStream.reset(InputStream.java:347)
    at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:135)
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1111)
    at SoundJCIFSLineTest.main(SoundJCIFSLineTest.java:36)
Exception in thread "main" java.lang.NullPointerException
    at SoundJCIFSLineTest.main(SoundJCIFSLineTest.java:71)

Code:

import java.io.*;
import javax.sound.sampled.*;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;

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.2/server/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);

         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(smbFileInputStream);
         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();
      }
   }
}
2

There are 2 answers

0
QGA On BEST ANSWER

I just added a BufferedInputStream and this solved the issue

BufferedInputStream buf = new BufferedInputStream(smbFileInputStream);
1
Phil Freihofner On

If you can address your network file via a URL or FileName, you can use forms of getAudioInputStream that do NOT require Markability/Resetability. This test is only performed when InputStream is used as the input parameter. If so, you can eliminate both the InputStream and BufferedInputStream steps.

However, I am not experienced with getting files over a network, so I'm not entirely sure a URL (my most usual method of opening audio data files) will work. Seems like it should though.