流式传输实时音频 [英] Streaming Real time Audio

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

问题描述

我希望在 android 设备上具有实时音频流的功能,该功能通过设备的 MIC 捕获音频并将其发送到服务器.我知道在录制后发送音频文件,但在实时情况下我需要帮助.可能可以通过向服务器连续发送字节数组来完成.如果是这样,如何或如果有任何其他方式,请分享您的想法.谢谢.

编辑-

Android 客户端代码:-

public class Main extends Activity {私人 MediaRecorder 录音机;私人最终字符串标签 = "AudioTest";/** 在第一次创建活动时调用.*/@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);字符串主机名 = "192.168.50.25";int 端口 = 2004;套接字 socket = null;尝试 {socket = new Socket(InetAddress.getByName(hostname), port);} catch (UnknownHostException e) {Log.d(TAG, "Inside UnknownHostException@@@@@@@@@@@@@@@@@@@@@@@");e.printStackTrace();} catch (IOException e) {Log.d(TAG, "内部 IOException%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");e.printStackTrace();}ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);记录器 = 新的 MediaRecorder();recorder.setAudioSource(MediaRecorder.AudioSource.MIC);recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);recorder.setOutputFile(pfd.getFileDescriptor());尝试 {Log.i(TAG, pfd.getFileDescriptor().toString());} 捕获(异常 e){Log.d(TAG, "Inside MyException###############################);}尝试 {recorder.prepare();} catch (IllegalStateException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}录音机开始();}

JAVA 服务器代码-

公共类提供者{ServerSocket providerSocket;套接字连接=空;ObjectOutputStream 输出;对象输入流中;字符串消息;提供者(){}无效运行(){尝试 {//1. 创建一个服务端套接字providerSocket = new ServerSocket(2004, 10);//2. 等待连接System.out.println("等待连接");连接 = providerSocket.accept();System.out.println("从收到的连接"+ connection.getInetAddress().getHostName());//3. 获取输入和输出流out = new ObjectOutputStream(connection.getOutputStream());out.flush();in = new ObjectInputStream(connection.getInputStream());sendMessage("连接成功");//4. 两部分通过输入和输出流进行通信做 {尝试 {message = (String) in.readObject();System.out.println("client>" + message);如果(消息.等于(再见"))sendMessage("再见");} catch (ClassNotFoundException classnot) {System.err.println("接收到的数据格式未知");}} while (!message.equals("bye"));} 捕捉(IOException ioException){ioException.printStackTrace();} 最后 {//4: 关闭连接尝试 {附寄();关闭();providerSocket.close();} 捕捉(IOException ioException){ioException.printStackTrace();}}}无效发送消息(字符串味精){尝试 {out.writeObject(msg);out.flush();System.out.println("服务器>" + msg);} 捕捉(IOException ioException){ioException.printStackTrace();}}公共静态无效主(字符串参数[]){提供者服务器 = new Provider();而(真){server.run();}}}

解决方案

您可以这样使用套接字:

String 主机名 = "1.2.3.4";内部端口 = 865;套接字 socket = null;尝试 {socket = new Socket(InetAddress.getByName(hostname), port);} 捕获(异常 e){e.printStackTrace();}ParcelFileDescriptor socketedFile = ParcelFileDescriptor.fromSocket(socket);

然后将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天全站免登陆