从转换的Andr​​oid播放PCM字节数组从Base64的字符串慢声音 [英] Android Play PCM byte array from Converted from Base64 String Slow Sounds

查看:265
本文介绍了从转换的Andr​​oid播放PCM字节数组从Base64的字符串慢声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于很长的标题所暗示的,我有麻烦打从我通过网络发送至一个音频的音频PubNunb 。我做的是我送的声音​​从AudioRecord使用这种code,而录音:

As the very long title suggests, I'm having trouble playing the audio from a audio that I send over the network through PubNunb. What I do is I send the audio while recording from AudioRecord using this code:

 AudioConfig audioConfig = getValidSampleRates(AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
    buffer = new byte[audioConfig.getBufferSize()];

    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, audioConfig.getSampleSize(), AudioFormat.CHANNEL_IN_MONO, AUDIO_FORMAT, audioConfig.getBufferSize());

当用户持有该按钮,记录的数据将被发送出去

Recorded data is sent when the user holds the button down:

   private class RecorderRunnable implements Runnable {
    @Override
    public void run() {
        while(mRecording) {
            Log.d("RECORDER_STATE", "Recording LOOP");
            recorder.read(buffer, 0, buffer.length);

            String base64EncodedBuffer = Base64.encodeToString(buffer, Base64.NO_WRAP);

            pubnub.publish(MainActivity.CHANNEL_ID, base64EncodedBuffer, new Callback() {
                @Override
                public void successCallback(String channel, Object message) {
                    super.successCallback(channel, message);
                }
            });
        }
    }
}

收到code:

Receive code:

       @Override
                    public void successCallback(String channel, final Object message) {


                        byte[] decodedBase64 = Base64.decode(message.toString(), Base64.NO_WRAP);

                        speaker.write(decodedBase64, 0, decodedBase64.length);
                    }

问题: 我得到的声音,但我得到的是很慢的声音。 你好的声音听起来象: Hee- *静态* -ll- *静态* -oo

Issue: I get the audio, but I get sounds that are really slow. "Hello" would sound like: "Hee-*static*-ll-*static*-oo"

要排除可能的原因,我试图立刻打这样的声音(没有网络):

To rule out possible causes, I tried immediately playing the audio like this (without the network):

 while(mRecording) {
            Log.d("RECORDER_STATE", "Recording LOOP");
            recorder.read(buffer, 0, buffer.length);

            String base64EncodedBuffer = Base64.encodeToString(buffer, Base64.NO_WRAP);

            byte[] decodedBase64 = Base64.decode(base64EncodedBuffer, Base64.NO_WRAP);

            speaker.write(decodedBase64, 0, decodedBase64.length);
        }

注意:我做了转换为Base64并返回到目的字节数组

结果为code以上(记录后直接打它)是pretty的好。所以,我想知道我在做什么错在网络上操作时。

The result for the code above (directly playing it after recording) is pretty good. So I'm wondering what I'm doing wrong when handling it over the network.

任何建议是AP preciated。谢谢。

Any suggestion is appreciated. Thank you.

修改:2015年8月28日 找到了一个很好的解释这个<一href="http://stackoverflow.com/questions/30948346/voip-rtp-streaming-from-to-server-in-java-to-from-android">here.但现在的问题是,什么是处理网络抖动/缓冲和packetloss使用我目前的执行情况的最好办法。

08/28/2015 Found a good explanation for this here. But now the question is, what's the best way of handling network jitter/buffering and packetloss using my current implementation.

推荐答案

pubnub提供通过TCP数据(的不是UDP )。 的http://www.pubnub.com/knowledge-base/discussion/263/does-pubnub-support-the-udp-protocol

pubnub delivers data through tcp (not udp). http://www.pubnub.com/knowledge-base/discussion/263/does-pubnub-support-the-udp-protocol

不应该有任何需要处理的数据包丢失在应用程序中。

There should be no need to handle packet loss in your application.

您可能需要通过建立某种形式的缓冲区来处理抖动。因为就没有严格的实时约束,我将讨论的做法,而不是贴code。

You may need to handle jitter by creating a buffer of some sort. Since there would be no rigid realtime constraint, I will discuss an approach rather than pasting code.

您可以使用队列缓冲区。我建议有两个线程。一个为你的读者(你的播放器),一个用于写入器(网络流)。让笔者排队让读者阅读之前的一些数据(数据也许几秒钟)。在纸面上,有一个很简单的概念验证,你不应该有问题,同时读取和写入,因为作家是写队列的末尾,读者可以阅读的队列的开始。

You can make a buffer using a queue. I suggest having two threads. One for your reader (your player) and one for writer (the network stream). Let the writer queue up some data (maybe a few seconds of data) before letting the reader read. On paper, with a very simply proof of concept, you should not have issues with simultaneous reads and writes since the writer is writing to the end of the queue and the reader is reading at the beginning of the queue.

把它作为一个水桶是一半满。你在倒水,让水渗漏出来以同样的速度。

Think of it as a bucket that is halfway full. You pour water in and let water leak out at the same rate.

这篇关于从转换的Andr​​oid播放PCM字节数组从Base64的字符串慢声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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