如何在安卓设备上接收RTP音频流?

4

我正在使用Java将麦克风的rtp音频流传输到我的安卓手机应用程序,但是我听不到任何声音。在设置AudioGroup和AudioStream时没有错误,因此我认为一切都进行得很正常。这是应用程序端的代码:

    AudioStream audioStream;
    AudioGroup audioGroup;
    AudioCodec codec = AudioCodec.PCMU;
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
    StrictMode.setThreadPolicy(policy);
    AudioManager audio = (AudioManager)getSystemService(AUDIO_SERVICE);
    audio.setMode(AudioManager.MODE_IN_COMMUNICATION);
    audioGroup = new AudioGroup();
    audioGroup.setMode(AudioGroup.MODE_NORMAL);
    InetAddress inetAddress;
    try {
        inetAddress = InetAddress.getByName("163.11.62.208");
        audioStream = new AudioStream(inetAddress);
        audioStream.setMode(RtpStream.MODE_RECEIVE_ONLY);
        audioStream.setCodec(codec);


        InetAddress inetAddressRemote = InetAddress.getByName("163.11.169.206");
        audioStream.associate(inetAddressRemote, 5004);
        audioStream.join(audioGroup);
    }

我使用的ffmpeg流测试(在vlc Android播放器上有效)是:

ffmpeg -f lavfi -i aevalsrc="sin(400*2*PI*t)" -ar 8000 -vcodec pcm_u8 -f rtp rtp://163.11.62.208:5004

再试一次.. 这在VLC安卓播放器中可以,但在我的应用程序中不行。没有错误.. 只是没有音频。

好的,我刚刚注意到在LogCat中,我收到了告诉我它正在工作的消息。以下是一些日志。

stream[59] is configured as RAW 8kHz 32ms mode 0
D/AudioGroup﹕ stream[59] joins group[56]
group[56] switches from mode 0 to 1
stream[54] joins group[56]
getOutputSamplingRate() reading from output desc
V/AudioSystem﹕ getSamplingRate() streamType 0, output 2, sampling rate 48000
V/AudioSystem﹕ getFrameCount() streamType 0, output 2, frameCount 960
V/AudioSystem﹕ getLatency() streamType 0, output 2, latency 160
V/AudioTrack﹕ getMinFrameCount=1280: afFrameCount=960, minBufCount=8, afSampleRate=48000, afLatency=160
D/AudioGroup﹕ reported frame count: output 1280, input 320
D/AudioGroup﹕ adjusted frame count: output 1280, input 512
V/AudioTrack﹕ sampleRate 8000, channelMask 0x1, format 1
V/AudioTrack﹕ streamType 0
V/AudioTrack﹕ set() streamType 0, sampleRate 8000, format 1, frameCount 1280, flags 0000
V/AudioSystem﹕ getLatency() streamType 0, output 2, latency 160
V/AudioSystem﹕ getFrameCount() streamType 0, output 2, frameCount 960
V/AudioSystem﹕ getOutputSamplingRate() reading from output desc
V/AudioSystem﹕ getSamplingRate() streamType 0, output 2, sampling rate 48000
V/AudioTrack﹕ createTrack_l() output 2 afLatency 160
V/AudioTrack﹕ afFrameCount=960, minBufCount=8, afSampleRate=48000, afLatency=160
V/AudioTrack﹕ minFrameCount: 1280, afFrameCount=960, minBufCount=8, sampleRate=8000, afSampleRate=48000, afLatency=160
V/AudioRecord﹕ sampleRate 8000, channelMask 0x10, format 1
V/AudioRecord﹕ inputSource 7
V/AudioRecord﹕ set(): sampleRate 8000, channelMask 0x10, frameCount 512
D/AudioRecord﹕ set(): voiceActivationState 0
D/AudioRecord﹕ Keep input Source type.
V/AudioRecord﹕ AudioRecord::set() minFrameCount = 320
V/AudioRecord﹕ set(): mSessionId 1827
V/AudioSystem﹕ ioConfigChanged() event 3, ioHandle 1828

但是:我也注意到当我运行我的应用程序时,系统音量完全没有响声。当我改变系统音量时,通常会发出指示器噪声以显示有多响/安静,但我什么也听不到。我不确定为什么会这样。


不要请求关闭网络的严格模式。在现代设备上,它根本不起作用,并且很可能是问题的原因。如果需要进行网络操作,请使用线程或AsyncTask。 - Gabe Sechan
好的,我已经更改了,但似乎仍然没有声音。我还将代码移到了一个单独的线程中。 - Tyler Helmuth
顶一下。我也遇到了同样的问题...如果我尝试从MODE_RECEIVE_ONLY设置模式为MODE_SEND_ONLY,我会收到“IllegalStateException: 忙”:( - Andrew Dunai
好的,IllegalStateException 已经解决了,但是服务器仍然没有回送声音。 - Andrew Dunai
@TylerHelmuth 有解决这个问题的运气吗? - Hussaan S
1个回答

1
请确保您的清单中有<uses-permission android:name="android.permission.RECORD_AUDIO" />。我敢打赌您可能已经这样做了,所以请转到设置 -> 应用程序 -> (点击齿轮) -> 应用程序权限 -> 麦克风,确保它对您的应用程序开启。我花了很长时间才发现这一点,因为我在清单中也遇到了同样的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接