Java-made imitation micro-letter video playback control _java

Source: Internet
Author: User
Tags stub

This control inherits from Surfaceview and plays video using MediaPlayer.

Small video Playback interface

Movieplayerview.java

Import java.io.IOException;
Import Android.content.Context;
Import Android.media.AudioManager;
Import Android.media.MediaPlayer;
Import Android.media.MediaPlayer.OnCompletionListener;
Import Android.util.AttributeSet;
Import Android.view.SurfaceHolder;
Import Android.view.SurfaceHolder.Callback;
 
Import Android.view.SurfaceView; /** * Video Playback control * @author Liuyinjun * @date 2015-2-8/public class Movieplayerview extends Surfaceview {private Sur
  Faceholder Msurfaceholder;
   
  Private MediaPlayer MPlayer;
  Public Movieplayerview {This (context, NULL);
  Public Movieplayerview (context, AttributeSet attrs) {This (context, attrs, 0);
     
    Public Movieplayerview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
    Msurfaceholder = This.getholder (); Msurfaceholder.addcallback (Msurfaceholdercallback); Holder join the callback interface Msurfaceholder.settype (surfaceholder.surface_type_push_buffers);//SETtype must be set, or there will be an error. /** * Play video * @param filename File path * * public void Plays (String fileName, final Onplaycompletionlistener C
    Ompletionlistener) {MPlayer = new MediaPlayer ();
    Mplayer.setaudiostreamtype (Audiomanager.stream_music); Mplayer.setdisplay (Msurfaceholder); Define a Surfaceview Play it Mplayer.setoncompletionlistener (new Oncompletionlistener () {@Override public vo
        ID oncompletion (MediaPlayer arg0) {stop ();
        if (Completionlistener!= null) completionlistener.onplaycompletion ();
      Canvas.drawcolor (color.transparent,//PorterDuff.Mode.CLEAR);
 
    }
    });
      try {mplayer.setdatasource (fileName);
    Mplayer.prepare ();
    catch (IllegalStateException e) {//TODO auto-generated catch block E.printstacktrace ();
    catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
  } mplayer.start (); private void Stop () {if (MPlayer!= null)
      {mplayer.release ();
    MPlayer = null;
      }/** * Free resources/public void release () {if (MPlayer!= null) {mplayer.release ();
    MPlayer = null; } private Callback Msurfaceholdercallback = new Callback () {@Override public void surfacedestroyed (Su
    Rfaceholder arg0) {msurfaceholder = null; @Override public void surfacecreated (Surfaceholder arg0) {//TODO auto-generated a stub Msur
    Faceholder = arg0;  @Override public void surfacechanged (Surfaceholder arg0, int arg1, int arg2, int arg3) {//TODO
    auto-generated method Stub msurfaceholder = arg0;
   
  }
  }; /** * Playback Successful CALLBACK * @author Liuyinjun * @date 2015-2-9/public interface onplaycompletionlistener{Public
  void Onplaycompletion (); }
 
}

The above is the entire contents of this article, I hope you can enjoy.

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.