jeudi 22 janvier 2015

How to encode audio in Android wear?


I have a question about how to encode video using Android wear.


I have a project about replaying voice message using android wear. I try to recording audio using android wear, and encoding the audio, passed the file to android phone. But Android wear can only using AudioRecord(not MediaRecord) to record audio as PCM format, the raw audio. But it's too big to transfer. So I want to encode raw to other format.


There are two ways I can do:




  1. copy android source's AmrInputStream file(the path is frameworks/base/media/java/android/media/AmrInputStream.java) and us this class to load libmedia_jni.so. Unfortunately, adb log tells me the library has already loaded.




  2. use MediaCodec class, But Android wear cannot support amr encoder. I want to know which encode type is supported by android wear.


    MediaCodecList encodeList = new MediaCodecList(MediaCodecList.REGULAR_CODECS);



    MediaCodecInfo[] count = encodeList.getCodecInfos();

    Log.v(TAG, "count length: " + count.length);

    for(int i=0; i< count.length; i++) {

    String[] info = count[i].getSupportedTypes();
    for(int j=0; j< info.length; j++)
    Log.v(TAG, info[j].toString() + "");
    }



and the result is


01-22 18:08:01.087 13665-13665/com.codoon.voice V/voice﹕ audio/mpeg


01-22 18:08:01.087 13665-13665/com.codoon.voice V/voice﹕ audio/mp4a-latm


01-22 18:08:01.087 13665-13665/com.codoon.voice V/voice﹕ audio/g711-alaw


01-22 18:08:01.087 13665-13665/com.codoon.voice V/voice﹕ audio/g711-mlaw


01-22 18:08:01.087 13665-13665/com.codoon.voice V/voice﹕ audio/vorbis


01-22 18:08:01.087 13665-13665/com.codoon.voice V/voice﹕ audio/opus


01-22 18:08:01.087 13665-13665/com.codoon.voice V/voice﹕ audio/raw


No amr support? so I use audio/mpeg format to test.



private static String audioType = "audio/mpeg";

private void creatMediaCodec(){
try {
encoder = MediaCodec.createEncoderByType(audioType);
} catch (IOException e) {
e.printStackTrace();
}
MediaFormat format = new MediaFormat();
format.setString(MediaFormat.KEY_MIME, audioType);
format.setInteger(MediaFormat.KEY_CHANNEL_COUNT, channelConfig);
format.setInteger(MediaFormat.KEY_SAMPLE_RATE, sampleRateInHz);

format.setInteger(MediaFormat.KEY_BIT_RATE, 80);//AAC-HE 64kbps
encoder.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);

}


based on the source: encoder = MediaCodec.createEncoderByType(audioType);


reported this problem:


1-23 10:38:30.365 2427-2448/com.codoon.voice I/OMXClient﹕ Using client-side OMX mux.


01-23 10:38:30.368 2427-2448/com.codoon.voice E/ACodec﹕ Unable to instantiate a encoder for type 'audio/mpeg'.


01-23 10:38:30.368 2427-2448/com.codoon.voice E/ACodec﹕ signalError(omxError 0x80001003, internalError -2147483648)


01-23 10:38:30.369 2427-2448/com.codoon.voice E/MediaCodec﹕ Codec reported err 0xfffffffe, actionCode 0, while in state 1


01-23 10:38:30.371 2427-2427/com.codoon.voice W/MediaCodec-JNI﹕ try to release MediaCodec from JMediaCodec::~JMediaCodec()...


01-23 10:38:30.373 2427-2427/com.codoon.voice W/MediaCodec-JNI﹕ done releasing MediaCodec from JMediaCodec::~JMediaCodec().


01-23 10:38:30.373 2427-2427/com.codoon.voice D/AndroidRuntime﹕ Shutting down VM


01-23 10:38:30.375 2427-2427/com.codoon.voice E/AndroidRuntime﹕ FATAL EXCEPTION: main


And I test other format support as above, but no help. Reported similar problem.


Besides transfer PCM directly from android wear to android phone, is there any other way when we want to use voice message on Android Wear?





1 commentaire: