Reference: http://mindtherobot.com/blog/624/android-audio-play-an-mp3-file-on-an-audiotrack/
Http://baike.baidu.com/view/14471.htm
We recommend that you download a ringtone file (smaller) and then convert it to WAV.
The source code is as follows:
Public class playmp3activity extends activity {
Private final string file = "// sdcard // testring.wav ";
Private Final Static string tag = "22 ";
Static byte [] buffer = NULL;
Audiotrack at = NULL;
Int pcmlen = 0;
@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Try {
Fileinputstream FCM = new fileinputstream (File );
Buffer = new byte [1024*1024*2]; // 2 m
Int Len = FCM. Read (buffer );
Log. I (TAG, "FCM Len =" + Len );
Log. I (TAG, "0:" + (char) buffer [0]);
Pcmlen = 0;
Pcmlen + = buffer [0x2b];
Pcmlen = pcmlen* 256 + buffer [0x2a];
Pcmlen = pcmlen * 256 + buffer [0x29];
Pcmlen = pcmlen * 256 + buffer [0x28];
Int channel = buffer [0x17];
Channel = channel * 256 + buffer [0x16];
Int bits = buffer [0x23];
Bits = bits * 256 + buffer [0x22];
Log. I (TAG, "pcmlen =" + pcmlen + ", channel =" + channel + ", BITs =" + bits );
At = new audiotrack (audiomanager. stream_music, 44100,
Channel,
Audioformat. encoding_pcm_16bit,
Pcmlen,
Audiotrack. mode_static );
At. Write (buffer, 0x2c, pcmlen );
Log. I (TAG, "Write 1 ...");
At. Play ();
Log. I (TAG, "Play 1 ...");
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
@ Override
Protected void ondestroy (){
// Todo auto-generated method stub
Super. ondestroy ();
At. Release ();
}
}