gpt4 book ai didi

android - AudioStreaming 在 android 套接字

转载 作者:行者123 更新时间:2023-11-29 00:49:14 24 4
gpt4 key购买 nike

我正在尝试通过 android 中的套接字进行音频流传输。我在客户端使用 AudioRecord 获取录制的音频,在服务器端使用 AudioTrack 类播放原始数据。但我做不到。是否有任何其他程序可以执行此操作?

音频文件

public void run()
{

BufferedOutputStream bufferedStreamInstance = null;
try
{
Socket socket = new Socket("192.168.1.25", 1234);
bufferedStreamInstance = new BufferedOutputStream(
socket.getOutputStream());
} catch (FileNotFoundException e)
{
throw new IllegalStateException("Cannot Open File", e);
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
DataOutputStream dataOutputStreamInstance = new DataOutputStream(
bufferedStreamInstance);

int bufferRead = 0;
int bufferSize = AudioRecord
.getMinBufferSize(this.getFrequency(), this.getChannelConfiguration(), this.getAudioEncoding());
AudioRecord recordInstance = new AudioRecord(MediaRecorder.AudioSource.MIC, this.getFrequency(), this
.getChannelConfiguration(), this.getAudioEncoding(), bufferSize);
short[] tempBuffer = new short[bufferSize];

recordInstance.startRecording();
while (this.isRecording)
{
bufferRead = recordInstance.read(tempBuffer, 0, bufferSize);
if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION)
{
throw new IllegalStateException(
"read() returned AudioRecord.ERROR_INVALID_OPERATION");
}
else if (bufferRead == AudioRecord.ERROR_BAD_VALUE)
{
throw new IllegalStateException(
"read() returned AudioRecord.ERROR_BAD_VALUE");
}
else if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION)
{
throw new IllegalStateException(
"read() returned AudioRecord.ERROR_INVALID_OPERATION");
}
try
{
for (int idxBuffer = 0; idxBuffer < bufferRead; ++idxBuffer)
{
dataOutputStreamInstance.writeShort(tempBuffer[idxBuffer]);
}
} catch (IOException e)
{
throw new IllegalStateException(
"dataOutputStreamInstance.writeShort(curVal)");
}

}
recordInstance.stop();
try
{
dataOutputStreamInstance.close();
bufferedStreamInstance.close();

} catch (IOException e)
{
throw new IllegalStateException("Cannot close buffered writer.");
}
}

音频接收器

public void run()
{

musicLength = 66535;
music = new short[musicLength];
try
{
ServerSocket serverSocket = new ServerSocket(1234);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

dis = new DataInputStream(bis);
while (dis.available() > 0)
{
music[musicLength - 1 - i] = dis.readShort();
i++;
}
try
{
dis.close();
} catch (IOException e)
{
e.printStackTrace();
}
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, musicLength,
AudioTrack.MODE_STREAM);
audioTrack.play();
audioTrack.write(music, 0, musicLength);
} catch (Throwable t)
{
Log.e("AudioTrack", "Playback Failed");
}
}

最佳答案

您无法发送数据吗?或者无法在接收端正常播放?与 MediaRecorder 类一样,AudioRecorder 类可能会为表头留出一些空间,然后在录制完成后执行查找以填充表头。因为无法在套接字上查找,所以将 header 写入文件的头部。我认为您最好的做法是将文件写入 SD 卡,然后使用 hexeditor 将字节与工作音频文件进行比较。然后您应该能够确定 header 的格式,当收到它时,您可以查找文件的开头(或缓冲区的开头)并将 header 写入正确的位置。

关于android - AudioStreaming 在 android 套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4440364/

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