将音频从Android传输到桌面应用程序

时间:2022-03-01 19:00:48

What the title says really. I need to stream the audio from the microphone on the telephone and play it in a desktop application (also Java code) on a computer.

标题真的说了什么。我需要从电话上的麦克风流式传输音频,然后在计算机上的桌面应用程序(也是Java代码)中播放。

Using UDP or TCP does not matter for me, whatever works best. Phone and computer will be on same NAT anyway so transmission will work fine.

使用UDP或TCP对我来说无关紧要,无论什么效果最好。无论如何,手机和电脑将在同一个NAT上,因此传输工作正常。

I have a fair idea of how to send the stream data from the device using this code:

我很清楚如何使用以下代码从设备发送流数据:

MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

String host = "10.0.2.2";
int port = 5740;

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

    ParcelFileDescriptor pfd = ParcelFileDescriptor.fromSocket(socket);
    recorder.setOutputFile(pfd.getFileDescriptor());
    recorder.prepare();
    recorder.start();

    Log.d(TAG, "Sending audio for 20 seconds..");
    Thread.sleep(20000);

    } catch (Exception e) {     
    // TODO Auto-generated catch block
        e.printStackTrace();
            }

The problem is, how do I play this stream in my java application on the PC? Or is there a better way to stream the sound?

问题是,如何在PC上的java应用程序中播放此流?或者有更好的方式来传输声音吗?

I have mined the internet for information on this but without any good results but surely somebody must have accomplished this before?

我已经在互联网上挖掘了这方面的信息,但没有任何好的结果,但肯定有人必须先完成这个吗?

Thanks in advance for any kind of help!

在此先感谢您的任何帮助!

2 个解决方案

#1


2  

I have done something very close to what you have been trying to do. I am using gstreamer on my server listening to a udp port. There is also a relay server written in java which is basically nothing more than a loopback socket. There is one server port which waits for a mobile client connections, upon receiving data, it dumps them all using DatagramPackets (java class for udp packets) into gstreamer's udp port. The only catch is to find the proper decoder for your gstreamer pipeline.

我做了一些非常接近你一直试图做的事情。我在服务器上使用gstreamer监听udp端口。还有一个用java编写的中继服务器,它基本上只是一个环回套接字。有一个服务器端口等待移动客户端连接,在接收数据时,它使用DatagramPackets(udp数据包的java类)将它们全部转储到gstreamer的udp端口。唯一的问题是为你的gstreamer管道找到合适的解码器。

#2


1  

Can you write a client that just connects to your phone on that port and receives data?

你能编写一个只连接到该端口上的手机并接收数据的客户端吗?

Now, there are no mp4 java decoders, so you'll need to use another format. Take look at some sample apps using JavaLayer or JOgg. They both work with any InputStream, so as long as you can open a socket, you can play back your stream.

现在,没有mp4 java解码器,所以你需要使用另一种格式。使用JavaLayer或JOgg查看一些示例应用程序。它们都可以与任何InputStream一起使用,因此只要您可以打开套接字,就可以播放您的流。

Also, I'm not sure about Android, but don't you need to open a ServerSocket and wait for connections?

另外,我不确定Android,但是你不需要打开ServerSocket并等待连接吗?

#1


2  

I have done something very close to what you have been trying to do. I am using gstreamer on my server listening to a udp port. There is also a relay server written in java which is basically nothing more than a loopback socket. There is one server port which waits for a mobile client connections, upon receiving data, it dumps them all using DatagramPackets (java class for udp packets) into gstreamer's udp port. The only catch is to find the proper decoder for your gstreamer pipeline.

我做了一些非常接近你一直试图做的事情。我在服务器上使用gstreamer监听udp端口。还有一个用java编写的中继服务器,它基本上只是一个环回套接字。有一个服务器端口等待移动客户端连接,在接收数据时,它使用DatagramPackets(udp数据包的java类)将它们全部转储到gstreamer的udp端口。唯一的问题是为你的gstreamer管道找到合适的解码器。

#2


1  

Can you write a client that just connects to your phone on that port and receives data?

你能编写一个只连接到该端口上的手机并接收数据的客户端吗?

Now, there are no mp4 java decoders, so you'll need to use another format. Take look at some sample apps using JavaLayer or JOgg. They both work with any InputStream, so as long as you can open a socket, you can play back your stream.

现在,没有mp4 java解码器,所以你需要使用另一种格式。使用JavaLayer或JOgg查看一些示例应用程序。它们都可以与任何InputStream一起使用,因此只要您可以打开套接字,就可以播放您的流。

Also, I'm not sure about Android, but don't you need to open a ServerSocket and wait for connections?

另外,我不确定Android,但是你不需要打开ServerSocket并等待连接吗?