Android播放视频的三种方式
程序员文章站
2024-03-04 21:10:00
在android中,我们有三种方式来实现视频的播放:
1)、使用其自带的播放器。指定action为action_view,data为uri,type为其mime类型。...
在android中,我们有三种方式来实现视频的播放:
1)、使用其自带的播放器。指定action为action_view,data为uri,type为其mime类型。
2)、使用videoview来播放。在布局文件中使用videoview结合mediacontroller来实现对其控制。
3)、使用mediaplayer类和surfaceview来实现,这种方式很灵活。
1、调用其自带的播放器:
uriuri = uri.parse(environment.getexternalstoragedirectory().getpath()+"/test_movie.m4v"); //调用系统自带的播放器 intentintent = new intent(intent.action_view); log.v("uri:::::::::", uri.tostring()); intent.setdataandtype(uri, "video/mp4"); startactivity(intent);
2、使用videoview来实现:
uriuri = uri.parse(environment.getexternalstoragedirectory().getpath()+"/test_movie.m4v"); videoviewvideoview = (videoview)this.findviewbyid(r.id.video_view); videoview.setmediacontroller(new mediacontroller(this)); videoview.setvideouri(uri); videoview.start(); videoview.requestfocus();
3、使用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; /** * 该实例中使用mediaplayer完成播放,同时界面使用surfaceview来实现 * * 这里我们实现mediaplayer中很多状态变化时的监听器 * * 使用mediaplayer时,也可以使用mediacontroller类,但是需要实现mediacontroller.mediacontroller接口 * 实现一些控制方法。 * * 然后,设置controller.setmediaplayer(),setanchorview(),setenabled(),show()就可以了,这里不再实现 * @author administrator * */ public 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); //给surfaceview添加callback监听 holder = surfaceview.getholder(); holder.addcallback(this); //为了可以播放视频或者使用camera预览,我们需要指定其buffer类型 holder.settype(surfaceholder.surface_type_push_buffers); //下面开始实例化mediaplayer对象 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"); //然后指定需要播放文件的路径,初始化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(); } //然后,我们取得当前display对象 currdisplay = this.getwindowmanager().getdefaultdisplay(); } @override public void surfacechanged(surfaceholderarg0, int arg1, int arg2, int arg3) { // 当surface尺寸等参数改变时触发 log.v("surface change:::", "surfacechanged called"); } @override public void surfacecreated(surfaceholderholder) { // 当surfaceview中的surface被创建的时候被调用 //在这里我们指定mediaplayer在当前的surface中进行播放 player.setdisplay(holder); //在指定了mediaplayer播放的容器后,我们就可以使用prepare或者prepareasync来准备播放了 player.prepareasync(); } @override public void surfacedestroyed(surfaceholderholder) { log.v("surface destory:::", "surfacedestroyed called"); } @override public void onvideosizechanged(mediaplayerarg0, int arg1, int arg2) { // 当video大小改变时触发 //这个方法在设置player的source后至少触发一次 log.v("video size change", "onvideosizechanged called"); } @override public void onseekcomplete(mediaplayerarg0) { // seek操作完成时触发 log.v("seek completion", "onseekcomplete called"); } @override public void onprepared(mediaplayerplayer) { // 当prepare完成后,该方法触发,在这里我们播放视频 //首先取得video的宽和高 vwidth = player.getvideowidth(); vheight = player.getvideoheight(); if(vwidth > currdisplay.getwidth() || vheight > currdisplay.getheight()){ //如果video的宽或者高超出了当前屏幕的大小,则要进行缩放 float wratio = (float)vwidth/(float)currdisplay.getwidth(); float hratio = (float)vheight/(float)currdisplay.getheight(); //选择大的一个进行缩放 float ratio = math.max(wratio, hratio); vwidth = (int)math.ceil((float)vwidth/ratio); vheight = (int)math.ceil((float)vheight/ratio); //设置surfaceview的布局参数 surfaceview.setlayoutparams(new linearlayout.layoutparams(vwidth, vheight)); //然后开始播放视频 player.start(); } } @override public boolean oninfo(mediaplayerplayer, int whatinfo, int extra) { // 当一些特定信息出现或者警告时触发 switch(whatinfo){ 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 called"); 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) { // 当mediaplayer播放完成后触发 log.v("play over:::", "oncomletion called"); this.finish(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。