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();
}
}
}
I just added a
BufferedInputStream
and this solved the issue