Recording and playback using Opus Codec in Android

I am working on a project that should record and play back using Opus Codec, I have searched a lot, but I can not find a single example / example using this solution. I find a demo with an encoder but cannot find a decoder. I only find the source code of this codec using C, can you help me?

+5
source share
2 answers

Hi, the demonstration is a good place to start, he was very close to solving it. However, each packet must be sent separately from the encoder to the decoder. Instead of saving everything to a file and then reading them without any relation to running the package.
I changed the code to also record the number of bytes encoded, and when I decode, I first read 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" ); } 
+1
source

Try the GitHub demo . I compiled it, but it does not play the recorded sound.

0
source

Source: https://habr.com/ru/post/1207308/


All Articles