FMUSER Wirless Transmit Video And Audio More Easier !

[email protected] WhatsApp +8618078869184
Language

    Android how to play H.264 Format Video

     

    "About the video file playback of the H.264 format, we can decode the implementation via Android.media.mediacodec, this is very familiar with the friend who is working in the current media, security real-time video transmission, this is just a demo, it needs Classmates download the source code research yourself! The effect is as follows: The key code is as follows: Package com.asir.h264demo; Import android.annotation.targetApi; Import Android.App.Activity; Import android.media.mediacodec; Import android.media.mediaformat; Import Android.OS.BUILD; Import android.os.bundle; Import android.util.log; Import android.view.surfaceview; Import android.view.view; Import android.widget.button; Import java.io.bufferedinputstream; Import java.io.file; Import java.io.fileinputstream; Import java.io.filenotfoundexception; Import java.io.ioException; Import Java.io.InputStream; Import java.nio.bytebuffer; Public class mainactivity extensity { Private SurfaceView MsurfaceView; Private button MREADBUTTON PRIVATE MediaCodec McODEC; Thread readfilethread; Boolean isinit = false; // Video constants Private final static string mime_type = "" VIDEO / AVC ""; // h.264 Advanced Video Private static int video_width = 1024; Private static int video_height = 600; PRIVATE FINAL = 30; PRIVATE FINAL Static Int Head_offset = 512; Private final static string log_tag = "asir" "; @Override Protected void oncreate (bundle savedinstancestate) { Super.onstance (Savedinstancestate); SetContentView (r.layout.Activity_main); MsurfaceView = (SurfaceView) FindViewbyid (r.id.surfaceView); MREADBUTTON = (Button) FindViewbyid (r.id.btn_readfile); MReadbutton.setonclickListener (New View.onClickListener () { @Override Public void onclick (view v) { IF (! isinit) { Initdecoder (); isinit = true; } ReadFileThread = New Thread (ReadFile); ReadFileThread.start (); } }); } @Override protected void onpause () { Super.onpause (); } @Override Protected void onResume () { Super.onResume (); } @Override protected void ondestroy () { Super.ondestroy (); ReadFileThread.Interrupt (); } @TargetApi (build.version_codes.jelly_bean) Public void initdecoder () { Try { McOdec = mediacodec.createdecoderbytype (mime_type); Mediaformat MediaFormat = MediaFormat.createvideoFormat (MIME_TYPE, Video_WIDTH, Video_HEIGHT); McOdec.configure (MediaFormat, MsurfaceView.getHolder (). getsurface (), null, 0); McOdec.start (); } catch (ioexception e) { E.PrintStackTrace (); } } INT mcount = 0; // final byte [] h264split = {0, 0, 0, 1}; @TargetApi (build.version_codes.jelly_bean) Public boolean onframe (byte [] buf, int length) { LOG.E (log_tag, "" onframe start "); // Get Input Buffer Index Long PTSUSEC = 0; IF (McOUNT == 0) { Bytebuffer [] InputBuffers = McODec.getinputBuffers (); INT INPUTBUFFERINDEX = McODec.dequeueInputBuffer (100000); Log.e (log_tag, "" Onframe Index: "+ InputBufferIndex); IF (InputBufferIndex> = 0) { Bytebuffer InputBuffer = INPUTBUFFERS [InputBufferIndex]; InputBuffer.clear (); InputBuffer.Put (buf, 0, length) .rewind (); McOdec.queueInputBuffer (InputBufferIndex, 0, Length, 0, Mediacodec.Buffer_Flag_codec_config); } else { Log.e (log_tag, "" InputBufferIndex <0 "+ InputBufferIndex); Return False; } } else { Bytebuffer [] InputBuffers = McODec.getinputBuffers (); INT INPUTBUFFERINDEX = McODec.dequeueInputBuffer (100000); Log.e (log_tag, "" Onframe Index: "+ InputBufferIndex); IF (InputBufferIndex> = 0) { InputBuffers [INPUTBUFFERINDEX] .clear (); InputBuffers [InputBufferIndex] .put (buf, 0, length) .rewind (); McODec.queueInputBuffer (InputBufferIndex, 0, Length, PTSUSEC, 0); } else { Log.e (log_tag, "" InputBufferIndex <0 "+ InputBufferIndex); Return False; } } McOUNT ++; // Get Output Buffer INDEX Mediacodec.bufferinfo bufferinfo = new mediacodec.bufferinfo (); Int outputBufferIndex = McOdec.dequeueoutputBuffer (BufferInfo, 10000); While (OutputBufferIndex> = 0) { McOdec.releaseoutputBuffer (OutputBufferIndex, true); OutputBufferIndex = mcodec.dequeueoutputbuffer (bufferinfo, 0); } LOG.E (log_tag, "" onframe end "); Return True; } / ** * Find H264 Frame HEAD * * @PARAM BUFFER * @Param Len * @Return the offset of frame head, return 0 if can not find one * / Static int foundhead (byte [] buffer, int LEN) { INT I; For (i = head_offset; i 300) { Try { Thread.sleep (20); H264count = 0; CONTINUE; } catch (exception e) { Log.e (log_tag, "" Exception for: "+ E.GetMessage ()); } } BufferedInputStream Bis = NULL; String sdfilename = "" / sdcard / "+ string.format (" ""% 03d ", h264count) +" ".h264" String assetsfilename = string.format (""% 03d ", h264count) +" ".h264" Log.e (log_tag, "" Read from FileName: "+ SDFileName); // fileinputstream fos = null; Try { File H264 = New File (SDFileName); IF (! h264.exists ()) { Log.e (log_tag, "" File isn't exist: "+ sdfilename); Return; } // read the SD card // fos = new fileinputstream (h264); // bis = new bufferedinputStream (FOS); // Read Assets Directory INPUTSTREAM IS = GetResources (). Getassets (). Open (assetsfilename); BIS = New BufferedInputStream (IS); INT filelen = (int) h264.length (); Byte [] buffer = new byte [filelen]; Try { Bis.Read (buffer); } catch (ioexception e) { Log.e (log_tag, "" Error: Read from file "+ E.getMessage ()); } Onframe (Buffer, Filelen); } catch (exception e) { IF (buildconfig.debug) { Log.e (log_tag, "" error: "+ E.getMessage ()); } } H264Count ++; } Log.d (log_tag, "end loop"); } } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 Code Logic Analysis: This is mainly to read the H264 file under the project directory assets directory through a thread thread, and then enters the read content and the SURFACEVIEW to be displayed to MEDIACODEC to make playback. XML interface file:

      Enter email  to get a surprise

      fmuser.org

      es.fmuser.org
      it.fmuser.org
      fr.fmuser.org
      de.fmuser.org
      af.fmuser.org ->Afrikaans
      sq.fmuser.org ->Albanian
      ar.fmuser.org ->Arabic
      hy.fmuser.org ->Armenian
      az.fmuser.org ->Azerbaijani
      eu.fmuser.org ->Basque
      be.fmuser.org ->Belarusian
      bg.fmuser.org ->Bulgarian
      ca.fmuser.org ->Catalan
      zh-CN.fmuser.org ->Chinese (Simplified)
      zh-TW.fmuser.org ->Chinese (Traditional)
      hr.fmuser.org ->Croatian
      cs.fmuser.org ->Czech
      da.fmuser.org ->Danish
      nl.fmuser.org ->Dutch
      et.fmuser.org ->Estonian
      tl.fmuser.org ->Filipino
      fi.fmuser.org ->Finnish
      fr.fmuser.org ->French
      gl.fmuser.org ->Galician
      ka.fmuser.org ->Georgian
      de.fmuser.org ->German
      el.fmuser.org ->Greek
      ht.fmuser.org ->Haitian Creole
      iw.fmuser.org ->Hebrew
      hi.fmuser.org ->Hindi
      hu.fmuser.org ->Hungarian
      is.fmuser.org ->Icelandic
      id.fmuser.org ->Indonesian
      ga.fmuser.org ->Irish
      it.fmuser.org ->Italian
      ja.fmuser.org ->Japanese
      ko.fmuser.org ->Korean
      lv.fmuser.org ->Latvian
      lt.fmuser.org ->Lithuanian
      mk.fmuser.org ->Macedonian
      ms.fmuser.org ->Malay
      mt.fmuser.org ->Maltese
      no.fmuser.org ->Norwegian
      fa.fmuser.org ->Persian
      pl.fmuser.org ->Polish
      pt.fmuser.org ->Portuguese
      ro.fmuser.org ->Romanian
      ru.fmuser.org ->Russian
      sr.fmuser.org ->Serbian
      sk.fmuser.org ->Slovak
      sl.fmuser.org ->Slovenian
      es.fmuser.org ->Spanish
      sw.fmuser.org ->Swahili
      sv.fmuser.org ->Swedish
      th.fmuser.org ->Thai
      tr.fmuser.org ->Turkish
      uk.fmuser.org ->Ukrainian
      ur.fmuser.org ->Urdu
      vi.fmuser.org ->Vietnamese
      cy.fmuser.org ->Welsh
      yi.fmuser.org ->Yiddish

       
  •  

    FMUSER Wirless Transmit Video And Audio More Easier !

  • Contact

    Address:
    No.305 Room HuiLan Building No.273 Huanpu Road Guangzhou China 510620

    E-mail:
    [email protected]

    Tel / WhatApps:
    +8618078869184

  • Categories

  • Newsletter

    FIRST OR FULL NAME

    E-mail

  • paypal solution  Western UnionBank OF China
    E-mail:[email protected]   WhatsApp:+8618078869184   Skype:sky198710021 Chat with me
    Copyright 2006-2020 Powered By www.fmuser.org

    Contact Us