Search code examples
androidaudioyoutube-livestreaming-api

Getting Audio issue in Youtube Live streaming in android application


I am Developing an app in android live streaming. I am able to stream the live videos to the YouTube channel. But the problem was, getting no audio to that live streaming video.

My code will like below

 private static final int frequency= 44100;
     public void recordThread() {
            Log.d(MainActivity.APP_NAME, "recordThread");

            int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
            int channelConfiguration = AudioFormat.CHANNEL_IN_STEREO;
            int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
            Log.i(MainActivity.APP_NAME, "AudioRecord buffer size: " + bufferSize);

            // 16 bit PCM stereo recording was chosen as example.
            AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration,
                    audioEncoding, bufferSize);
            recorder.startRecording();

            // Make bufferSize be in samples instead of bytes.
            bufferSize /= 2;
            short[] buffer = new short[bufferSize];
            while (!cancel) {
                int bufferReadResult = recorder.read(buffer, 0, bufferSize);
                // Utils.Debug("bufferReadResult: " + bufferReadResult);
                if (bufferReadResult > 0) {
                    frameCallback.handleFrame(buffer, bufferReadResult);
                } else if (bufferReadResult < 0) {
                    Log.w(MainActivity.APP_NAME, "Error calling recorder.read: " + bufferReadResult);
                }
            }
            recorder.stop();

            Log.d(MainActivity.APP_NAME, "exit recordThread");
        }

please some suggest me the solution to get out of this issue.


Solution

  • Search in github. There you will find a sample project(yasea) for audio encoding. Intermix those two projects, you will get the solution.