Android的流音频服务器 [英] android stream audio to server

查看:134
本文介绍了Android的流音频服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解插座的工作,特别是我需要一些code样品进行服务器端接收由mediarecorder从设备发送的数据流。

非常感谢您的任何帮助。

我真正的最终目的是在交谈的设备,听它在PC上,只在一个方向。

在那一刻我能够用下面的code发出的数据流:

 字符串主机=192.168.1.10;
INT端口= 8000;
Socket套接字= NULL;
    尝试 {
        插座=新的Socket(InetAddress.getByName(主机名),端口);
    }赶上(UnknownHostException异常E){
        e.printStackTrace();
    }赶上(IOException异常E){
        e.printStackTrace();
    }

ParcelFileDescriptor PFD = ParcelFileDescriptor.fromSocket(插座);

记录=新MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEn codeR(MediaRecorder.AudioEn coder.AMR_NB);
recorder.setOutputFile(pfd.getFileDescriptor());

    尝试 {
        。录音机prepare();
    }赶上(IllegalStateException异常E){
        e.printStackTrace();
    }赶上(IOException异常E){
        e.printStackTrace();
    }

recorder.start();
 

解决方案

看起来不错,但我个人preFER缓冲音频设备上,并将其发送追平了记录到的从另一个线程的服务器,而不是插座直接像你一样。由于本地缓存可以让你优雅地处理连接中断。

想象一下,你正在录制和用户经过一个隧道,失去网络连接 - 如果你流直接,插座将关闭,用户将恼火:-)但是,如果你是在本地缓存中的数据,你可以重新建立连接,并继续从你留下来发送声音到服务器,并希望用户甚至不知道在连接中断刚发生,因为它只是神奇地运行。

在为了得到那个工作,你必须写记录到本地缓存,并有一个独立的线程检查新数据的缓冲区,并发送到服务器尽快。

I would like to understand how socket work, especially I need some code samples for server side to receive the stream sent by mediarecorder from the device.

Thank you very much for any help.

My real final intent is to talk in device and listen it on PC, just one direction.

At moment I am able to send out the stream using the following code:

String hostname = "192.168.1.10";
int port = 8000;
Socket socket = null;
    try {
        socket = new Socket(InetAddress.getByName(hostname), port);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);

recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(pfd.getFileDescriptor());

    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

recorder.start();

解决方案

Seems ok, but I would personally prefer to buffer the audio on the device and send it to the server from another thread instead of tying the recorder to the socket directly like you did. Because buffering locally will allow you to handle connection breaks gracefully.

Imagine you're recording and user goes through a tunnel and loses internet connection -- if you're streaming directly, the socket would close and the user would be annoyed :-) However, if you are buffering the data locally, you can reestablish the connection and continue sending the audio to the server from where you left off, and hopefully the user doesn't even have to know that a break in the connection just happened, because it just magically works.

In order to get that to work, you'd have to write the recording to a local buffer and have a separate thread check for new data on that buffer and send to the server as soon as possible.

这篇关于Android的流音频服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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