Record and playback with Opus Codec in Android

6.8k views Asked by At

I'm working on a project that need to record and playback with Opus Codec, I search a lot but I can't find any demo/example using that solution. I find a demo having encoder but can't find the decoder. I only find the source code of this codec using C, can you help me?

2

There are 2 answers

1
Kenumir On

Try this GitHub demo. I compiled it but, it doesn't play the recorded sound.

2
Cristian On

Hello that demo is a good place to start, he was realy close to solving it. However each package must be sent separetly from the encoder to the decoder. Instead of saving all to a file and then read them back without any regard of package start.
I modified the code to also write the number of encoded bytes and when I decode, I read first the number of bytes in each packet and then the payload.

Here is the modified code in OpusEncoder.java

public void write( short[] buffer ) throws IOException
    {
        byte[] encodedBuffer = new byte[buffer.length];
        int lenEncodedBytes = this.nativeEncodeBytes( buffer , encodedBuffer);
        Log.i(TAG,"encoded "+lenEncodedBytes+" bytes");
        if (lenEncodedBytes > 0)
        {
            this.out.write(lenEncodedBytes);
            this.out.write( encodedBuffer, 0, lenEncodedBytes );
        }
        else
        {
            Log.e( TAG, "Error during Encoding. Error Code: " +  lenEncodedBytes);

            throw new IOException( "Error during Encoding. Error Code: " +  lenEncodedBytes );
        }
    }


Here is the modified code in OpusDecoder.java

        byte[] encodedBuffer;
        int bytesEncoded=this.in.read();
        int bytesDecoded=0;

        Log.d( TAG, bytesEncoded + " bytes read from input stream" );
        if ( bytesEncoded >= 0 )
        {
            encodedBuffer=new byte[bytesEncoded];
            int bytesRead = this.in.read( encodedBuffer );
            bytesDecoded = nativeDecodeBytes( encodedBuffer , buffer);
            Log.d( TAG, bytesEncoded + " bytes decoded" );
        }