Android three ways to play video _android

Source: Internet
Author: User
Tags prepare

In Android, we have three ways to play Video:

1), using 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, use MediaPlayer class and Surfaceview to realize, this way is very flexible.

1, call its own player:

Uriuri = Uri.parse (Environment.getexternalstoragedirectory (). GetPath () + "/test_movie.m4v"); 
Call the system with its own player 
 intentintent = new Intent (intent.action_view); 
 LOG.V ("URI:::::;::", uri.tostring ()); 
 Intent.setdataandtype (URI, "Video/mp4"); 
 StartActivity (Intent);

2, using Videoview to achieve:

Uriuri = Uri.parse (Environment.getexternalstoragedirectory (). GetPath () + "/test_movie.m4v"); 
Videoviewvideoview = (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 MediaPlayer to complete playback, and the interface uses Surfaceview to achieve * * * Here we implement a lot of state changes in the MediaPlayer listener * * When using MediaPlayer, you can also use Mediacontr 
Oller class, but need to implement Mediacontroller.mediacontroller interface * implement some control methods. * * Then, set Controller.setmediaplayer (), Setanchorview (), setenabled (), show () on it, no longer implemented here * @author Administrator * * * * Publ IC Class VideosurfacedemoExtends Activityimplements Oncompletionlistener,onerrorlistener,oninfolistener, Onpreparedlistener, 
 onseekcompletelistener,onvideosizechangedlistener,surfaceholder.callback{private Displaycurrdisplay; 
 Private Surfaceviewsurfaceview; 
 Private Surfaceholderholder; 
 Private Mediaplayerplayer; 
 private int vwidth,vheight; 
   
 Private Boolean readytoplay = false; 
  public void OnCreate (bundlesavedinstancestate) {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 the 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_ 
  MOVIE.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 (SurfaceHolderarg0, int arg1, int arg2, int arg3) {//When parameter changes such as surface size are triggered 
 LOG.V ("Surface Change:::", "surfacechanged called"); @Override public void surfacecreated (Surfaceholderholder) {//when surface in Surfaceview is created//here we specify MEDIAPL 
  The Ayer is played in the current surface player.setdisplay (holder); In the designation of MediAfter the Aplayer is played, we can use prepare or prepareasync to prepare to play the Player.prepareasync (); @Override public void surfacedestroyed (Surfaceholderholder) {log.v ("Surface destory::", "surfacedestroyed C 
 Alled "); @Override public void onvideosizechanged (MediaPlayerarg0, int arg1, int arg2) {//When video size changes trigger//This method sets play 
   
 The ER source is triggered at least once LOG.V ("Video Size Change", "onvideosizechanged called"); @Override the public void Onseekcomplete (MEDIAPLAYERARG0) {//Seek operation completes when the LOG.V is triggered ("seek completion", "Onseekcomplet 
   
 E called "); @Override public void onprepared (Mediaplayerplayer) {//When prepare is complete, this method triggers, where we play the video//first get the width and height of the videos VW 
  Idth = 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, zoom FL 
   Oat 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 (mediaplayerplayer, int whatinfo, int extra) {//trigger switch when certain information appears or warns (W 
  Hatinfo) {case MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING:break; 
  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 (mediaplayerplayer, int whaterror, int extra) {log.v ("Play Error:::", "OnError C 
  Alled "); 
   Switch (whaterror) {case MEDIAPLAYER.MEDIA_ERROR_SERVER_DIED:LOG.V ("Play ERROR:::", "media_error_server_died"); 
  Break CASE MEDIAPLAYER.MEDIA_ERROR_UNKNOWN:LOG.V ("Play ERROR:::", "Media_error_unknown"); 
  Break 
  Default:break; 
 return false; @Override public void Oncompletion (Mediaplayerplayer) {//when MediaPlayer playback is complete triggers log.v ("Play Over:::", "Oncomlet 
  Ion called "); 
 This.finish (); } 
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.