Android AudioRecord通过UDP传输到服务器的播放问题

21

我正在尝试制作一个简单的Android应用程序,将麦克风音频流传输到服务器以进行播放。播放结果听起来很奇怪,声音中有大的间隙。是否有人知道我在做错了什么?

编辑:问题已解决。事实证明,我假设每个传入的缓冲区都完全填满是错误的。

以下是我的活动:

public class MainActivity extends Activity {
    private static String TAG = "AudioClient";

    // the server information
    private static final String SERVER = "xx.xx.xx.xx";
    private static final int PORT = 50005;

    // the audio recording options
    private static final int RECORDING_RATE = 44100;
    private static final int CHANNEL = AudioFormat.CHANNEL_IN_MONO;
    private static final int FORMAT = AudioFormat.ENCODING_PCM_16BIT;

    // the button the user presses to send the audio stream to the server
    private Button sendAudioButton;

    // the audio recorder
    private AudioRecord recorder;

    // the minimum buffer size needed for audio recording
    private static int BUFFER_SIZE = AudioRecord.getMinBufferSize(
            RECORDING_RATE, CHANNEL, FORMAT);

    // are we currently sending audio data
    private boolean currentlySendingAudio = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i(TAG, "Creating the Audio Client with minimum buffer of "
                + BUFFER_SIZE + " bytes");

        // set up the button
        sendAudioButton = (Button) findViewById(R.id.start_button);
        sendAudioButton.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:
                    startStreamingAudio();
                    break;

                case MotionEvent.ACTION_UP:
                    stopStreamingAudio();
                    break;
                }

                return false;
            }
        });
    }

    private void startStreamingAudio() {

        Log.i(TAG, "Starting the audio stream");
        currentlySendingAudio = true;
        startStreaming();
    }

    private void stopStreamingAudio() {

        Log.i(TAG, "Stopping the audio stream");
        currentlySendingAudio = false;
        recorder.release();
    }

    private void startStreaming() {

        Log.i(TAG, "Starting the background thread to stream the audio data");

        Thread streamThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {

                    Log.d(TAG, "Creating the datagram socket");
                    DatagramSocket socket = new DatagramSocket();

                    Log.d(TAG, "Creating the buffer of size " + BUFFER_SIZE);
                    byte[] buffer = new byte[BUFFER_SIZE];

                    Log.d(TAG, "Connecting to " + SERVER + ":" + PORT);
                    final InetAddress serverAddress = InetAddress
                            .getByName(SERVER);
                    Log.d(TAG, "Connected to " + SERVER + ":" + PORT);

                    Log.d(TAG, "Creating the reuseable DatagramPacket");
                    DatagramPacket packet;

                    Log.d(TAG, "Creating the AudioRecord");
                    recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
                            RECORDING_RATE, CHANNEL, FORMAT, BUFFER_SIZE * 10);

                    Log.d(TAG, "AudioRecord recording...");
                    recorder.startRecording();

                    while (currentlySendingAudio == true) {

                        // read the data into the buffer
                        int read = recorder.read(buffer, 0, buffer.length);

                        // place contents of buffer into the packet
                        packet = new DatagramPacket(buffer, read,
                            serverAddress, PORT);

                        // send the packet
                        socket.send(packet);
                    }

                    Log.d(TAG, "AudioRecord finished recording");

                } catch (Exception e) {
                    Log.e(TAG, "Exception: " + e);
                }
            }
        });

        // start the thread
        streamThread.start();
    }
}

这是我的服务器端代码:

class Server {

    AudioInputStream audioInputStream;
    static AudioInputStream ais;
    static AudioFormat format;
    static boolean status = true;
    static int port = 50005;
    static int sampleRate = 11025;
    static int bufferSize = 9728;

    static Long lastTime;
    static long totalBytesReceived = 0L;

    private static final int audioStreamBufferSize = bufferSize * 20;
    static byte[] audioStreamBuffer = new byte[audioStreamBufferSize];
    private static int audioStreamBufferIndex = 0;

    public static void main(String args[]) throws Exception {

        Log("Starting the AudioServer...");

        Log("Creating the datagram socket on port " + port + "...");
        DatagramSocket serverSocket = new DatagramSocket(null);
        serverSocket.setReuseAddress(true);
        serverSocket.bind(new InetSocketAddress(port));

        Log("Creating the buffer to hold the received data of size "
                + bufferSize + "...");
        byte[] receiveData = new byte[bufferSize];

        Log("Setting the audio rate to " + sampleRate + "hz...");
        format = new AudioFormat(sampleRate, 16, 1, true, false);

        Log("Ready to receive audio data");
        while (status == true) {

            DatagramPacket receivePacket = new DatagramPacket(receiveData,
                    receiveData.length);
            serverSocket.receive(receivePacket);
            bufferAudioForPlayback(receivePacket.getData(),
                    receivePacket.getOffset(), receivePacket.getLength());
        }

        serverSocket.close();
    }

    private static void bufferAudioForPlayback(byte[] buffer, int offset,
            int length) {

        byte[] actualBytes = new byte[length];

        for (int i = 0; i < length; i++) {
            actualBytes[i] = buffer[i];
        }

        for (byte sample : actualBytes) {

            int percentage = (int) (((double) audioStreamBufferIndex / (double) audioStreamBuffer.length) * 100.0);
            Log("buffer is " + percentage + "% full");

            audioStreamBuffer[audioStreamBufferIndex] = sample;
            audioStreamBufferIndex++;
            Log("Buffer " + audioStreamBufferIndex + " / "
                    + audioStreamBuffer.length + "    " + percentage);

            if (audioStreamBufferIndex == audioStreamBuffer.length - 1) {
                toSpeaker(audioStreamBuffer);
                audioStreamBufferIndex = 0;
                System.exit(0);
            }
        }
    }

    private static void Log(String log) {
        System.out.println(log);
    }

    public static void toSpeaker(byte soundbytes[]) {
        try {

            DataLine.Info dataLineInfo = new DataLine.Info(
                    SourceDataLine.class, format);
            SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem
                    .getLine(dataLineInfo);

            sourceDataLine.open(format);

            FloatControl volumeControl = (FloatControl) sourceDataLine
                    .getControl(FloatControl.Type.MASTER_GAIN);
            volumeControl.setValue(100.0f);

            sourceDataLine.start();
            sourceDataLine.open(format);
            sourceDataLine.start();
            sourceDataLine.write(soundbytes, 0, soundbytes.length);
            sourceDataLine.drain();
            sourceDataLine.close();
        } catch (Exception e) {
            System.out.println("Error with audio playback: " + e);
            e.printStackTrace();
        }
    }
}

最后,这是主活动的资源XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="20dip">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:scaleType="fitCenter"/>

        <TextView  
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_info"
        android:layout_weight="1.0"
        android:textSize="20dip"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnStart"
                android:text="@string/start_recording"
                android:layout_weight="1.0"/>

                <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnStop"
                android:text="@string/stop_recording"
                android:layout_weight="1.0"/>
    </LinearLayout>
</LinearLayout>

编辑:回放的音频嗡嗡作响,像这样 suh-suh-suh-suh-o-ou-ou-ou-nds-nds-ds。


使用这种方法,我可以让多个设备流到同一个端口吗?如果可以,那么在服务器端如何区分每个流是哪个设备的? - Will R.
接收数据包时,为什么需要使用receivePacket.getOffset()?这似乎是多余的。另外,为什么要使用System.exit(0)来终止进程? - Udi Reshef
2个回答

10

以下是你可以尝试的替代方法,而不是:

// read the data into the buffer
recorder.read(buffer, 0, buffer.length);

// place contents of buffer into the packet
packet = new DatagramPacket(buffer, buffer.length, serverAddress, PORT);

不要期望从recorder收到完整的读取缓冲区,而是使用实际读取的值

不要预期您将从recorder收到完全读取的缓冲区,而是使用实际读取的值。
// read the data into the buffer
int read = recorder.read(buffer, 0, buffer.length);

// place contents of buffer into the packet
packet = new DatagramPacket(buffer, read, serverAddress, PORT);

或者类似的东西。


我按照你建议的修改了,但音频输出仍然听起来像机枪声。你可以听出这是正确的音频,只是有间隔。不过还是谢谢你的反馈! - Joshua W
2
此外,您可能还想更改读取数据包的方式。可以尝试使用类似以下的代码:bufferAudioForPlayback(receivePacket.getData(), receivePacket.getOffset(), receivePacket.getLength())。 - harism
1
谢谢!这个加上把采样率改为11025,让我得到了无间隙、清晰的播放。你太棒了,谢谢! - Joshua W
@Joshua Witter:我正在使用你的代码在两个安卓设备之间通过WiFi传输语音消息,但是它并没有像预期那样工作。我能够接收到所有的数据包,但就是无法听到任何声音。你有什么想法吗?我真的需要解决这个问题。 - G droid
我的问题不同,但这个问题和答案解决了它。+1 - Mahdi Moqadasi

0
感谢 Joshua 的帖子.... 对新手很有帮助 :)
volumeControl.setValue(volumeControl.getMaximum());

removes illegalStateException at Server

在 Android 客户端中的权限

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

2
这些代码片段与播放卡顿的问题有何关联? - MarsAtomic

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