如何实现音频呼吁在Android的插座

编程入门 行业动态 更新时间:2024-10-15 12:34:04
本文介绍了如何实现音频呼吁在Android的插座的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经搜查,发现声音流或音频通话的code简单的插座两款Android手机之间。我已经实现这一点,但是这code不工作。我无法听到任何声音。

I have searched and found a code of voice streaming or audio calling between two android phones on simple sockets. I have implemented this , but this code is not working. I am unable to hear any voice.

接收器code:

private int sampleRate = 44100; private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO; private int audioFormat = AudioFormat.ENCODING_PCM_16BIT; int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat); public void startReceiving() { Thread receiveThread = new Thread (new Runnable() { @Override public void run() { try { DatagramSocket socket = new DatagramSocket(50005); Log.d("VR", "Socket Created"); byte[] buffer = new byte[256]; speaker = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRate,channelConfig,audioFormat,minBufSize,AudioTrack.MODE_STREAM); while(status == true) { try { DatagramPacket packet = new DatagramPacket(buffer,buffer.length); socket.receive(packet); Log.d("VR", "Packet Received"); //reading content from packet buffer=packet.getData(); Log.d("VR", "Packet data read into buffer"); //sending data to the Audiotrack obj i.e. speaker speaker.write(buffer, 0, minBufSize); Log.d("VR", String.valueOf(buffer)); speaker.play(); } catch(IOException e) { Log.e("VR","IOException"); } } } catch (SocketException e) { Log.e("VR", "SocketException"); } } }); receiveThread.start(); }

发件人code:

Sender Code:

public void startStreaming() { Thread streamThread = new Thread(new Runnable() { @Override public void run() { try { DatagramSocket socket = new DatagramSocket(); Log.d("VS", "Socket Created"); byte[] buffer = new byte[minBufSize]; Log.d("VS","Buffer created of size " + minBufSize); DatagramPacket packet; final InetAddress destination = InetAddress.getByName("192.168.0.216"); Log.d("VS", "Address retrieved"); recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize*10); Log.d("VS", "Recorder initialized"); recorder.startRecording(); while(status == true) { //reading data from MIC into buffer minBufSize = recorder.read(buffer, 0, buffer.length); //putting buffer in the packet packet = new DatagramPacket (buffer,buffer.length,destination,port); socket.send(packet); } } catch(UnknownHostException e) { Log.e("VS", "UnknownHostException"); } catch (IOException e) { e.printStackTrace(); Log.e("VS", "IOException"); } } }); streamThread.start(); }

我调试的code和包被成功地发送和 speaker.play()也被称为。但没有语音

我已经实现在一个单一的应用程序和活动这code。有两个按钮开始听和开始流式传输

I have implemented this code in a single application and activity. With two Buttons Start Listening and Start Streaming

推荐答案

getMinBufferSize()可能不是同一个 AudioRecord 和 AudioTrack (了解到硬盘的方式)。请确保您使用的是两个大。

getMinBufferSize() may not be the same for AudioRecord and AudioTrack (learned that the hard way). Make sure you are using the larger of the two.

更多推荐

如何实现音频呼吁在Android的插座

本文发布于:2023-11-25 23:05:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631708.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:插座   如何实现   音频   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!