Android three ways of playing video

Source: Internet
Author: User
Tags prepare

in the in Android, we have three ways to play Video:

1, with its own player. Specifies that the action is action_view,data to Uri,type for its MIME type. 2, use Videoview to play. Use Videoview in the layout file to control it with Mediacontroller.   3, using MediaPlayer class and Surfaceview to achieve, this way is very flexible. 1, call its own player:
Uri uri = Uri.parse (Environment.getexternalstoragedirectory (). GetPath () + "/test_movie.m4v");     
Call the system with its own player    
    Intent Intent = new Intent (intent.action_view);    
    LOG.V ("URI:::::;::", uri.tostring ());    
    Intent.setdataandtype (URI, "Video/mp4");    
    
   

2, using Videoview to achieve:

Uri uri = Uri.parse (Environment.getexternalstoragedirectory (). GetPath () + "/test_movie.m4v");    
Videoview Videoview = (videoview) This.findviewbyid (R.id.video_view);    
Videoview.setmediacontroller (This) (new Mediacontroller);    
Videoview.setvideouri (URI);    
Videoview.start ();    
Videoview.requestfocus ();    

3, the use of MediaPlayer:

Package Demo.camera;    
Import java.io.IOException;    
Import android.app.Activity;    
Import Android.media.MediaPlayer;    
Import Android.media.MediaPlayer.OnCompletionListener;    
Import Android.media.MediaPlayer.OnErrorListener;    
Import Android.media.MediaPlayer.OnInfoListener;    
Import Android.media.MediaPlayer.OnPreparedListener;    
Import Android.media.MediaPlayer.OnSeekCompleteListener;    
Import Android.media.MediaPlayer.OnVideoSizeChangedListener;    
Import Android.os.Bundle;    
Import android.os.Environment;    
Import Android.util.Log;    
Import Android.view.Display;    
Import Android.view.SurfaceHolder;    
Import Android.view.SurfaceView;    
Import Android.widget.LinearLayout; /** * This instance uses the MediaPlayer to complete the playback, simultaneously the interface uses the Surfaceview to realize * * * Here we implement many state changes in the MediaPlayer listener * * When using MediaPlayer, you can also  
 Use the Mediacontroller class, but implement the Mediacontroller.mediacontroller interface * To implement some control methods. * * Then, set Controller.setmediaplayer (), Setanchorview (), setenabled (), show () on it, this is no longer implemented * @author Administrator */public class Videosurfacedemo extends activity implements Oncompletionli Stener,onerrorlistener,oninfolistener, Onpreparedlistener, Onseekcompletelistener,onvideosizechangedlistener,    
    surfaceholder.callback{private Display Currdisplay;    
    Private Surfaceview Surfaceview;    
    Private Surfaceholder holder;    
    Private MediaPlayer player;    
    private int vwidth,vheight;    
            
    Private Boolean readytoplay = false;    
        public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    
                    
        This.setcontentview (R.layout.video_surface);    
        Surfaceview = (Surfaceview) This.findviewbyid (r.id.video_surface);    
        Add callback listening holder to Surfaceview = Surfaceview.getholder ();    
        Holder.addcallback (this); In order to be able to play the video or use camera preview, we need to specify its buffer type Holder.settype (surfaceholder.surface_type_Push_buffers);    
        The following begins instantiating the MediaPlayer object player = new MediaPlayer ();    
        Player.setoncompletionlistener (this);    
        Player.setonerrorlistener (this);    
        Player.setoninfolistener (this);    
        Player.setonpreparedlistener (this);    
        Player.setonseekcompletelistener (this);    
        Player.setonvideosizechangedlistener (this);    
        LOG.V ("Begin:::", "surfacedestroyed called"); Then specify the path where you want to play the file, initialize MediaPlayer String datapath = Environment.getexternalstoragedirectory (). GetPath () +/test_mo    
        VIE.M4V ";    
            try {player.setdatasource (datapath);    
        LOG.V ("Next::", "surfacedestroyed called");    
        catch (IllegalArgumentException e) {e.printstacktrace ();    
        catch (IllegalStateException e) {e.printstacktrace ();    
        catch (IOException e) {e.printstacktrace ();    
       } Then we get the current display object currdisplay = This.getwindowmanager (). Getdefaultdisplay ();    
        @Override public void surfacechanged (Surfaceholder arg0, int arg1, int arg2, int arg3) {    
    Triggering LOG.V ("Surface Change::", "surfacechanged called") when parameters such as Surface dimensions are changed; @Override public void surfacecreated (Surfaceholder holder) {//when surface in Surfaceview is created    
        Call//Here we specify MediaPlayer to play in the current surface player.setdisplay (holder);    
            
    After specifying the MediaPlayer to play the container, we can use prepare or prepareasync to prepare to play the Player.prepareasync (); @Override public void surfacedestroyed (Surfaceholder holder) {log.v ("Surface D    
    Estory:: "," surfacedestroyed called "); @Override public void onvideosizechanged (MediaPlayer arg0, int arg1, int arg2) {//when video size Trigger//When changing this method at least touches the player's source after it has been setSend a LOG.V ("Video Size Change", "onvideosizechanged called"); @Override public void Onseekcomplete (MediaPlayer arg0) {//Seek operation completes when LOG.V ("S    
            
    Eek completion "," Onseekcomplete called ");    
            
        @Override public void onprepared (MediaPlayer player) {//When prepare is complete, this method triggers, where we play the video    
        First get the width and height of the video vwidth = Player.getvideowidth ();    
            
        Vheight = Player.getvideoheight (); if (Vwidth > currdisplay.getwidth () | | | vheight > Currdisplay.getheight ()) {//If the width or height of the video exceeds the current screen size, the    
            To scale float Wratio = (float) vwidth/(float) currdisplay.getwidth ();    
                
            float hratio = (float) vheight/(float) currdisplay.getheight ();    
                
            Select the large one to zoom float ratio = Math.max (Wratio, hratio); Vwidth = (int) Math.ceil (Float) vwidth/ratio);    
                
            Vheight = (int) Math.ceil ((float) vheight/ratio);    
                
            Sets the layout parameters of the Surfaceview surfaceview.setlayoutparams (new Linearlayout.layoutparams (Vwidth, vheight));    
        Then start playing video Player.start (); @Override public boolean oninfo (MediaPlayer player, int whatinfo, int extra) {// Triggers switch (whatinfo) {case MEDIAPLAYER.MEDIA_INFO_BAD_INTERLEAVING:B when certain information appears or warns    
        Reak;    
        Case MediaPlayer.MEDIA_INFO_METADATA_UPDATE:break;    
        Case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:break;    
        Case MediaPlayer.MEDIA_INFO_NOT_SEEKABLE:break;    
    return false;  @Override public boolean onError (MediaPlayer player, int whaterror, int extra) {LOG.V ("Play ErROR:: "," onError called "); Switch (whaterror) {case MEDIAPLAYER.MEDIA_ERROR_SERVER_DIED:LOG.V ("Play ERROR:::", "media_e    
            Rror_server_died ");    
        Break    
            Case MEDIAPLAYER.MEDIA_ERROR_UNKNOWN:LOG.V ("Play ERROR:::", "Media_error_unknown");    
        Break    
        Default:break;    
    return false;    
        @Override public void Oncompletion (MediaPlayer player) {//triggers when MediaPlayer playback completes    
        LOG.V ("Play Over:::", "oncomletion called");    
    This.finish ();     }    
}
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.