安卓:音频流通过TCP套接字 [英] Android: Streaming audio over TCP Sockets

查看:309
本文介绍了安卓:音频流通过TCP套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的应用程序,我需要在Android手机上的MIC录音,并通过TCP发送给另一台Android手机,它需要的地方进行播放。

我使用 AudioRecord AudioTrack 类。这个伟大的工程与文件 - 写音频使用 DataOutputStream类的文件,并使用从它读的DataInputStream

不过,如果我从一个插座,而不是一个文件获得同样的数据流,并试图写它,我得到一个例外。

我很茫然,了解什么可能会错误。任何帮助将大大AP preciated。

编辑:问题是一样的,即使我使用较大缓冲的尝试(65535字节,160000字节)

这是在code:

录音:

  INT BUFFERSIZE = AudioRecord.getMinBufferSize(11025,AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioFormat.ENCODING_PCM_16BIT);

AudioRecord recordInstance =新AudioRecord(MediaRecorder.AudioSource.MIC,11025,AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioFormat.ENCODING_PCM_16BIT,缓冲区大小);

byte []的tempBuffer =新的字节[BUFFERSIZE]

recordInstance.startRecording();

而(/ * isRecording * /){
      bufferRead = recordInstance.read(tempBuffer,0,BUFFERSIZE);
      dataOutputStreamInstance.write(tempBuffer);
}
 

是DataOutputStream上面得到的:

 的BufferedOutputStream BUFF =新的BufferedOutputStream(OUT1); // OUT1是套接字的OutputStream的
DataOutputStream类dataOutputStreamInstance =新DataOutputStream类(BUFF);
 

能否请你看看,让我知道它是什么,我可能是错在这里做什么?

谢谢

解决方案

我得到这个工作,有一定的帮助,也只有部分。

我开始了与code在<一个href="http://emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html">http://emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html,改变文件的流套接字的流,并改变了isAvailable()来,如果(inputStream.read(字节阵列)!= -1)。

音频直播通过TCP现在正在发生。

但是,

我所听到的另一端是噪音,我现在狩猎正确一套AudioRecorder和AudioTrack参数 - 频率,频道的配置和编码,音频源等

如果您对此有什么想法,请让我知道。

谢谢

编辑: 这是我的一部分,一个愚蠢的错误。 除了所有我上面已经说过,使用的InputStream上播放机侧和OutputStream在记录器侧,和字节数组,而不是短裤,它会工作。 :)

For my app, I need to record audio from MIC on an Android phone, and send it over TCP to the other android phone, where it needs to be played.

I am using AudioRecord and AudioTrack class. This works great with a file - write audio to the file using DataOutputStream, and read from it using DataInputStream.

However, if I obtain the same stream from a socket instead of a File, and try writing to it, I get an exception.

I am at a loss to understand what could possibly be going wrong. Any help would be greatly appreciated.

EDIT: The problem is same even if I try with larger buffer sizes (65535 bytes, 160000 bytes).

This is the code:

Recorder:

int bufferSize = AudioRecord.getMinBufferSize(11025, , AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); 

AudioRecord recordInstance = new AudioRecord(MediaRecorder.AudioSource.MIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);

byte[] tempBuffer = new byte[bufferSize];

recordInstance.startRecording();

while (/*isRecording*/) {
      bufferRead = recordInstance.read(tempBuffer, 0, bufferSize);
      dataOutputStreamInstance.write(tempBuffer);
}

The DataOutputStream above is obtained as:

BufferedOutputStream buff = new BufferedOutputStream(out1); //out1 is the socket's outputStream
DataOutputStream dataOutputStreamInstance = new DataOutputStream (buff);

Could you please have a look, and let me know what is it that I could be doing wrong here?

Thanks,

解决方案

I got this working, with some help, and only partially.

I started off with the code at http://emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html, changed File's streams to Socket's streams, and changed the isAvailable() to if(inputStream.read(byteArray) != -1).

Live streaming of audio over TCP is happening now.

However,

All I hear at the other end is noise, and I am now hunting for correct set of parameters for AudioRecorder and AudioTrack - the frequency, channel config and encoding, audio source etc.

If you have any idea about this, please let me know.

Thanks,

EDIT: It was a stupid error on my part. In addition to all I have said above, use inputStream on player side and outputStream on recorder side, and byte arrays instead of shorts and it will work. :)

这篇关于安卓:音频流通过TCP套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆