Android Mediecodec Hard Decode mp3,wma

Source: Internet
Author: User

1MedieCodec supports more than 4.1 system use, is a class of Google SDK.

2 ability to decode MP3,WMA format audio files

3 before decoding, need mediaexactor extract file information, including file type, sample rate,


Package Com.dawin.mediacodec;import Java.io.ioexception;import Java.nio.bytebuffer;import Android.media.MediaCodec ; import Android.media.mediaextractor;import Android.media.mediaformat;import android.media.MediaCodec.BufferInfo; public class mediacodectest{byte[] Decodedata = new byte[1024 * 1024x768 * 20];//20mMediaCodec Mmediacodec; Mediaextractor Mediaextractor; Mediaformat mmediaformat;final int timeout_us = 1000; Bufferinfo Info;boolean Sawoutputeos = False;boolean Sawinputeos = false; Bytebuffer[] codecinputbuffers; Bytebuffer[] codecoutputbuffers;/** * Decode the audio file, return the last decoded data * * @param URL * @return */public byte[] decode (String url) {url = ""; Try{mediaextractor.setdatasource (URL);} catch (IOException e) {}mmediaformat = Mediaextractor.gettrackformat (0); String mime = mmediaformat.getstring (mediaformat.key_mime); try{mmediacodec = Mediacodec.createdecoderbytype (MIME);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Mmediacodec.configure (Mmediaformat, NULL, NULL, 0); MMEdiacodec.start (); codecinputbuffers = Mmediacodec.getinputbuffers (); codecoutputbuffers = Mmediacodec.getoutputbuffers (); info = new Bufferinfo (); mediaextractor.selecttrack (0); input (); output (); return Decodedata;} private void Output () {Final int res = Mmediacodec.dequeueoutputbuffer (info, timeout_us); if (res >= 0) {int Outputbufind ex = res; Bytebuffer buf = codecoutputbuffers[outputbufindex];final byte[] Chunk = new Byte[info.size];buf.get (chunk); Read the buffer all at oncebuf.clear (); * * MUST do!!! OTHERWISE the NEXT time you GET this//same BUFFER bad things would happenif (Chunk.length > 0) {system.arraycopy (chunk, 0, Decodedata, 0, chunk.length);//Maudiotrack.write (chunk, 0, chunk.length);} Mmediacodec.releaseoutputbuffer (Outputbufindex, FALSE/* render */); if (Info.flags & Mediacodec.buffer_flag_end_ Of_stream)! = 0) {Sawoutputeos = true;}} else if (res = = mediacodec.info_output_buffers_changed) {codecoutputbuffers = Mmediacodec.getoutputbuffers ();} else if ( res = = Mediacodec.info_output_format_changed) {final Mediaformat Oformat = Mmediacodec.getoutputformat ();//LOG.D (Log_tag, "OUTPUT Format has changed to "+ Oformat);//Maudiotrack.setplaybackrate (Oformat.getinteger (Mediaformat.key_sample_rate));}} private void input () {int inputbufindex = Mmediacodec.dequeueinputbuffer (Timeout_us); if (inputbufindex >= 0) { Bytebuffer Dstbuf = Codecinputbuffers[inputbufindex];int samplesize = mediaextractor.readsampledata (dstBuf, 0);// LOG.I (Log_tag, "samplesize:" +samplesize); Long Presentationtimeus = 0;if (SampleSize < 0) {//. LOG.I (Log_tag, "Saw input end of stream!"); Sawinputeos = true;samplesize = 0;} Else{presentationtimeus = Mediaextractor.getsampletime ();//LOG.I (Log_tag, "Presentationtimeus" +presentationTimeUs );} Mmediacodec.queueinputbuffer (inputbufindex,0,//Offsetsamplesize, Presentationtimeus,sawinputeos?) mediacodec.buffer_flag_end_of_stream:0), if (!sawinputeos) {//LOG.I (Log_tag, "extractor.advance ()"); Mediaextractor.advance ();}}}


Android Mediecodec Hard Decode mp3,wma

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.