Android: Use the audiotrack class to play wav files

Source: Internet
Author: User

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 ();
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.