流媒体实时音频 [英] Streaming Real time Audio

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

问题描述

我想有实时音频流,其上通过设备的MIC捕获音频,并将其发送到服务器机器人装置的功能性。我知道我的记录需要帮助,但以后的情况下实时地发送发送音频文件。可能是它可以通过不断地发送字节数组服务器来完成。如果是的话怎么样,或者任何其他的方式,请分享你的想法。谢谢。

编辑 -

Android客户端code: -

 公共类主要扩展活动{
    私人MediaRecorder记录;

    私人最终字符串变量=AudioTest;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        字符串主机=192.168.50.25;
        INT端口= 2004;

        Socket套接字= NULL;
        尝试 {
            插座=新的Socket(InetAddress.getByName(主机名),端口);

        }赶上(UnknownHostException异常E){

            Log.d(TAG,里面的UnknownHostException @@@@@@@@@@@@@@@@@@@@@@);

            e.printStackTrace();
        }赶上(IOException异常E){
            Log.d(TAG,内部IOException异常%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%);

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


        尝试 {
            Log.i(TAG,pfd.getFileDescriptor()的toString());
        }赶上(例外五){
            Log.d(TAG,内部MyException ################################);
        }

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

            e.printStackTrace();
        }赶上(IOException异常E){

            e.printStackTrace();
        }

        recorder.start();

    }
 

Java服务器code -

 公共类提供商{
    ServerSocket的providerSocket;
    Socket连接= NULL;
    ObjectOutputStream的了;
    ObjectInputStream中的;
    字符串消息;

    提供商(){
    }

    无效的run(){
        尝试 {
            // 1.创建一个服务器套接字
            providerSocket =新的ServerSocket(2004年,10);
            // 2.等待连接
            的System.out.println(等待连接);

            连接= providerSocket.accept();
            的System.out.println(从接收到的连接
                    + connection.getInetAddress()的gethostname())。
            // 3.获得输入和输出流
            OUT =新的ObjectOutputStream(connection.getOutputStream());
            了out.flush();
            在=新的ObjectInputStream(connection.getInputStream());
            的sendMessage(连接成功);
            经由输入和输出流// 4.两部分进行通信
            做 {
                尝试 {
                    消息=(字符串)in.readObject();
                    的System.out.println(客户端>+消息);
                    如果(message.equals(再见))
                        的sendMessage(再见);
                }赶上(ClassNotFoundException的classnot){
                    (接收到未知的数据格式)通信System.err.println;
                }
            }而(message.equals(再见)!);
        }赶上(IOException异常IOException异常){
            ioException.printStackTrace();
        } 最后 {
            // 4:关闭连接
            尝试 {
                附寄();
                out.close();
                providerSocket.close();
            }赶上(IOException异常IOException异常){
                ioException.printStackTrace();
            }
        }
    }

    无效的sendMessage(弦乐味精){
        尝试 {
            out.writeObject(MSG);
            了out.flush();
            的System.out.println(服务器>+味精);
        }赶上(IOException异常IOException异常){
            ioException.printStackTrace();
        }
    }

    公共静态无效的主要(字符串的args []){
        提供服务器=新的供应商();
        而(真){
            server.run();
        }
    }
}
 

解决方案

您可以使用套接字像这样:

 字符串主机=1.2.3.4;
INT端口= 865;

Socket套接字= NULL;

尝试 {
    插座=新的Socket(InetAddress.getByName(主机名),端口);
}赶上(例外五){

    e.printStackTrace();
}

ParcelFileDescriptor socketedFile = ParcelFileDescriptor.fromSocket(插座);
 

然后设置socketedFile到音频记录器的输出文件(socketedFile.getFileDescriptor())。这将它发送的字节数。

替代地,使其更稳定,写出从MediaRecorder数据到本地缓冲器,然后有一个单独的线程检查该缓冲区,并将其写入到套接字代替,以允许在插座连接小断开

看到这一问题的详细资料: Android的流音频服务器

显然,你则需要有在服务器上运行的应用程序收到您的字节,并把它转换成音频数据。

I want to have a functionality of real time audio streaming on android device which is capturing audio through the MIC of the device and send it to the server. I know to send a send a audio file after recording but in case of real time I need help. May be it can be done by sending byte array continually to the server. If so how or if any other way, Please share your ideas. Thanks.

EDIT-

Android Client Code:-

public class Main extends Activity {
    private MediaRecorder recorder;

    private final String TAG = "AudioTest";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String hostname = "192.168.50.25";
        int port = 2004;

        Socket socket = null;
        try {
            socket = new Socket(InetAddress.getByName(hostname), port);

        } catch (UnknownHostException e) {

            Log.d(TAG, "Inside  UnknownHostException@@@@@@@@@@@@@@@@@@@@@@");

            e.printStackTrace();
        } catch (IOException e) {
            Log.d(TAG, "Inside  IOException%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");

            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 {
            Log.i(TAG, pfd.getFileDescriptor().toString());
        } catch (Exception e) {
            Log.d(TAG, "Inside  MyException################################");
        }

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

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

        recorder.start();

    }

JAVA Server Code-

public class Provider {
    ServerSocket providerSocket;
    Socket connection = null;
    ObjectOutputStream out;
    ObjectInputStream in;
    String message;

    Provider() {
    }

    void run() {
        try {
            // 1. creating a server socket
            providerSocket = new ServerSocket(2004, 10);
            // 2. Wait for connection
            System.out.println("Waiting for connection");

            connection = providerSocket.accept();
            System.out.println("Connection received from "
                    + connection.getInetAddress().getHostName());
            // 3. get Input and Output streams
            out = new ObjectOutputStream(connection.getOutputStream());
            out.flush();
            in = new ObjectInputStream(connection.getInputStream());
            sendMessage("Connection successful");
            // 4. The two parts communicate via the input and output streams
            do {
                try {
                    message = (String) in.readObject();
                    System.out.println("client>" + message);
                    if (message.equals("bye"))
                        sendMessage("bye");
                } catch (ClassNotFoundException classnot) {
                    System.err.println("Data received in unknown format");
                }
            } while (!message.equals("bye"));
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } finally {
            // 4: Closing connection
            try {
                in.close();
                out.close();
                providerSocket.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    void sendMessage(String msg) {
        try {
            out.writeObject(msg);
            out.flush();
            System.out.println("server>" + msg);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    public static void main(String args[]) {
        Provider server = new Provider();
        while (true) {
            server.run();
        }
    }
}

解决方案

You can use sockets as so:

String hostname = "1.2.3.4";
int port = 865;

Socket socket = null;

try {
    socket = new Socket(InetAddress.getByName(hostname), port);
} catch (Exception e) {

    e.printStackTrace();
}

ParcelFileDescriptor socketedFile = ParcelFileDescriptor.fromSocket(socket);

Then set the socketedFile to the output file (socketedFile.getFileDescriptor()) of the audio recorder. This will send it up as bytes.

Alternatively to make it more stable, write out the data from the MediaRecorder to a local buffer and then have a separate thread check that buffer and write it to the socket instead, to allow for small disconnections in the socket connection.

See this question for more information: android stream audio to server

Obviously you then need to have an application running on a server to receive your bytes and turn that into audio data.

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

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