In my app i have created an audio player (only WAV files) using 
MediaPlayer<http://developer.android.com/reference/android/media/MediaPlayer.html>API.
 But the player doesn't give callback to onCompletion Listener 
everytime. Sometimes it gives callback but not everytime. I am doing some 
audio processing on wav file , like insertion and overwriting.

Is it because any missing in audio header? Why it doesn't give callback 
when the playback is completed?

here is the code:

mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            // realease media player
            // update player ui
        }
    });

 <http://stackoverflow.com/questions/tagged/android>

Here is the code how i create and update wav header:

public void createWAVHeader(int sRate,int byteRate,int nChannels,int 
BitsPerSample,long length){
     byte[] header = new byte[44];
     header[0]  = 'R';  // RIFF/WAVE header
     header[1]  = 'I';
     header[2]  = 'F';
     header[3]  = 'F'; 

     header[4]  = (byte) ((length+36) & 0xff);
     header[5]  = (byte) (((length+36) >> 8) & 0xff);
     header[6]  = (byte) (((length+36) >> 16) & 0xff);
     header[7]  = (byte) (((length+36) >> 24) & 0xff);

     header[8]  = 'W';
     header[9]  = 'A';
     header[10] = 'V';
     header[11] = 'E';
     header[12] = 'f';  // 'fmt ' chunk
     header[13] = 'm';
     header[14] = 't';
     header[15] = ' ';

     header[16] = 16;  // 4 bytes: size of 'fmt ' chunk
     header[17] = 0;
     header[18] = 0;
     header[19] = 0;
     header[20] = 1;  // format = 1
     header[21] = 0;
     header[22] = (byte) nChannels;
     header[23] = 0;
     header[24] = (byte) (sRate & 0xff);
     header[25] = (byte) ((sRate >> 8) & 0xff);
     header[26] = (byte) ((sRate >> 16) & 0xff);
     header[27] = (byte) ((sRate >> 24) & 0xff);
     header[28] = (byte) (byteRate & 0xff);
     header[29] = (byte) ((byteRate >> 8) & 0xff);
     header[30] = (byte) ((byteRate >> 16) & 0xff);
     header[31] = (byte) ((byteRate >> 24) & 0xff);
     header[32] = (byte) (2 * 16 / 8);  // block align
     header[33] = 0;
     header[34] = (byte) BitsPerSample;  // bits per sample
     header[35] = 0;
     header[36] = 'd';
     header[37] = 'a';
     header[38] = 't';
     header[39] = 'a';

     header[40] = (byte) (length & 0xff);
     header[41] = (byte) ((length >> 8) & 0xff);
     header[42] = (byte) ((length >> 16) & 0xff);
     header[43] = (byte) ((length >> 24) & 0xff);

     try {
         rafOut.write(header, 0, 44);
    } catch (IOException e) {
        e.printStackTrace();
    }}

Here is the code for updating the header when new audio data is been 
recorded

    public void updateHeader(String inFile){

    long totalAudioLen = 0;
    long totalDataLen =0;       // totalAudioLen + 36
    FileInputStream in = null;
    try
    {
    in = new FileInputStream(inFile);
    totalAudioLen = (in.getChannel().size()-44);
      totalDataLen = (totalAudioLen) + 36;

      RandomAccessFile invFile = new RandomAccessFile(inFile, "rw");
      invFile.seek(4);
      invFile.write((byte) (totalDataLen & 0xff));
      invFile.write((byte) ((totalDataLen >> 8) & 0xff));
      invFile.write((byte) ((totalDataLen >> 16) & 0xff));
      invFile.write((byte) ((totalDataLen >> 24) & 0xff));
      invFile.seek(40);
      invFile.write((byte) (totalAudioLen & 0xff));
      invFile.write((byte) ((totalAudioLen >> 8) & 0xff));
      invFile.write((byte) ((totalAudioLen >> 16) & 0xff));
      invFile.write((byte) ((totalAudioLen >> 24) & 0xff));
      invFile.close();
    }catch (Exception e) {
        // TODO: handle exception
    }
  }



-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to