gpt4 book ai didi

java - 如何从网络存储中检索音频

转载 作者:行者123 更新时间:2023-12-02 05:13:35 24 4
gpt4 key购买 nike

以下代码旨在从网络存储读取音频文件并在 swing 应用程序中播放它们

该代码使用 java SourceDataLine 和 jcifs 来检索音频

问题是我需要定义缓冲区的标记/重置

错误:

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)

代码:

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();
}
}
}

最佳答案

我刚刚添加了一个 BufferedInputStream 这解决了问题

BufferedInputStream buf = new BufferedInputStream(smbFileInputStream);

关于java - 如何从网络存储中检索音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27146979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com