简体   繁体   English

从Android到桌面应用程序的音频流

[英]Streaming audio from Android to desktop application

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. 使用UDP或TCP对我来说无关紧要,无论什么效果最好。 Phone and computer will be on same NAT anyway so transmission will work fine. 无论如何,手机和电脑将在同一个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? 问题是,如何在PC上的Java应用程序中播放此流? Or is there a better way to stream the sound? 或者有更好的方式来传输声音吗?

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! 在此先感谢您的任何帮助!

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. 我在服务器上使用gstreamer监听udp端口。 There is also a relay server written in java which is basically nothing more than a loopback socket. 还有一个用java编写的中继服务器,它基本上只不过是一个环回套接字。 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. 有一个服务器端口等待移动客户端连接,在接收数据时,它使用DatagramPackets(udp数据包的java类)将它们全部转储到gstreamer的udp端口。 The only catch is to find the proper decoder for your gstreamer pipeline. 唯一的问题是为你的gstreamer管道找到合适的解码器。

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. 现在,没有mp4 java解码器,所以你需要使用另一种格式。 Take look at some sample apps using JavaLayer or JOgg. 使用JavaLayer或JOgg查看一些示例应用程序。 They both work with any InputStream, so as long as you can open a socket, you can play back your stream. 它们都可以与任何InputStream一起使用,因此只要您可以打开套接字,就可以播放您的流。

Also, I'm not sure about Android, but don't you need to open a ServerSocket and wait for connections? 另外,我不确定Android,但是你不需要打开ServerSocket并等待连接吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM