Search code examples
android-mediaplayeramr

Convert PCM encoded wav file to amr file using AmrInputStream, but not able to play it using MediaPlayer


I am converting a PCM encoded wav file to amr file in Android using AmrInputStream. I used the code from below post:

converting PCM-16 to AMR using AmrInputStream

Everything works fine, and the amr file is also generated. However, when I tried to play it with MeidaPlayer, it thrown following exception:

   java.io.IOException: Prepare failed.: status=0xFFFFFFFC  
      at android.media.MediaPlayer.prepare(Native Method)

I noticed that in the above post, it mentioned: "requiring adding the #AMR!\n tag to the output file for playing.". but I don't know how to do it exactly. Please help!


Solution

  • Ok, I made it working. need to add the tag in the first 6 bytes of the output file:

        InputStream inStream;
        inStream = new FileInputStream(wavFilename);
        AmrInputStream aStream = new AmrInputStream(inStream);
    
        File file = new File(amrFilename);        
        file.createNewFile();
        OutputStream out = new FileOutputStream(file); 
    
        out.write(0x23);
        out.write(0x21);
        out.write(0x41);
        out.write(0x4D);
        out.write(0x52);
        out.write(0x0A);    
    
        byte[] x = new byte[1024];
        int len;
        while ((len=aStream.read(x)) > 0) {
            out.write(x,0,len);
        }
    
        out.close();