时间:2021-05-20
本文主要给大家介绍的是关于Android播放视频的四种方法,分享出来供大家参考学习,下面来一起看看详细的介绍:
一、通过intent的方式,调用系统自带的播放器
Uri uri = Uri.parse("/storage/emulated/0/DCIM/Camera/20170521_200117.mp4"); //调用系统自带的播放器 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4"); startActivity(intent);二、使用VideoView
布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_video_play_by_vv" android:layout_width="match_parent" android:layout_height="match_parent"> <VideoView android:id="@+id/video_view" android:layout_width="wrap_content" android:layout_height="wrap_content"/></RelativeLayout>Activity
public class VideoPlayByVVActivity extends AppCompatActivity { private VideoView mVideoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);// requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉 title// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //设置全屏// getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); //设置屏幕常亮 setContentView(R.layout.activity_video_play_by_vv); mVideoView = (VideoView) findViewById(R.id.video_view); init(); } private void init() { String path = "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4"; Uri uri = Uri.parse(path); mVideoView.setVideoPath(path); mVideoView.start(); mVideoView.requestFocus(); }}三、MediaPlayer + SurfaceView
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_video_play_by_sur" android:layout_width="match_parent" android:layout_height="match_parent"> <SurfaceView android:id="@+id/surface_view" android:layout_width="180dp" android:layout_height="wrap_content"/> <LinearLayout android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="40dp"> <Button android:id="@+id/stop" android:text="stop" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:id="@+id/play" android:text="play" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:id="@+id/pasue" android:text="pasue" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> </LinearLayout></RelativeLayout>Activity
public class VideoPlayBySurActivity extends AppCompatActivity implements View.OnClickListener { private SurfaceView mSurfaceView; private MediaPlayer mMediaPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video_play_by_sur); mSurfaceView = (SurfaceView) findViewById(R.id.surface_view); findViewById(R.id.stop).setOnClickListener(this); findViewById(R.id.pasue).setOnClickListener(this); findViewById(R.id.play).setOnClickListener(this); init(); } private void init() { mMediaPlayer = new MediaPlayer(); mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { play(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { } }); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.stop: stop(); break; case R.id.play: if(!mMediaPlayer.isPlaying()){ play(); } break; case R.id.pasue: pasue(); break; } } public void stop(){ if(mMediaPlayer.isPlaying()){ mMediaPlayer.stop(); } } public void pasue(){ if(mMediaPlayer.isPlaying()){ mMediaPlayer.pause(); }else{ mMediaPlayer.start(); } } public void play(){ String path = "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4"; try { mMediaPlayer.reset(); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); //设置需要播放的视频 mMediaPlayer.setDataSource(this, Uri.parse(path)); //把视频画面输出到SurfaceView mMediaPlayer.setDisplay(mSurfaceView.getHolder()); mMediaPlayer.prepare(); mMediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); } }}四、 MediaPlayer + TextureView
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_video_play_by_textrue_view" android:layout_width="match_parent" android:layout_height="match_parent"> <TextureView android:id="@+id/texture_view" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/video_image" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/all_darkbackground"/></RelativeLayout>Activity
public class VideoPlayByTextrueViewActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnInfoListener, MediaPlayer.OnBufferingUpdateListener { private TextureView mTextureView; private ImageView mImageVideo; private Surface mSurface; private MediaPlayer mMediaPlayer; private static String path = "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video_play_by_textrue_view); mTextureView = (TextureView) findViewById(R.id.texture_view); mImageVideo = (ImageView) findViewById(R.id.video_image); init(); } private void init() { mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) { mSurface = new Surface(surfaceTexture); Log.e("tag", "---- onSurfaceTextureAvailable"); play(); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { Log.e("tag", "---- onSurfaceTextureSizeChanged"); } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { mTextureView=null; mSurface=null; mMediaPlayer.stop(); mMediaPlayer.release(); return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { } }); } public void play(){ mMediaPlayer = new MediaPlayer(); try { mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(path)); mMediaPlayer.setSurface(mSurface); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.setOnPreparedListener(this); mMediaPlayer.setOnInfoListener(this); mMediaPlayer.setOnBufferingUpdateListener(this); mMediaPlayer.prepareAsync(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onPrepared(MediaPlayer mp) { mImageVideo.setVisibility(View.GONE); mMediaPlayer.start(); } @Override public boolean onInfo(MediaPlayer mp, int what, int extra) { return false; } @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { }}参考文章
Android中SurfaceView的使用详解
Android 5.0(Lollipop)中的SurfaceTexture,TextureView, SurfaceView和GLSurfaceView
Android TextureView简易教程
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Android关闭多个Activity的实现方法总的来说有四种方法:1、使用Application来进行关闭publicclassAppextendsAppli
Android应用经常会和服务器端交互,这就需要手机客户端发送网络请求,下面介绍四种常用网络请求方式,我这边是通过Android单元测试来完成这四种方法的,还不
优酷的星光币怎么得到?今天我们就来介绍四种方法,很简单,请看下文详细介绍。软件名称:优酷视频(视频播放器)forandroidV9.3.0官方安卓版软件大小:1
Javascript数组去重的四种方法四种算法来实现这个目的:第一种方法:Array.prototype.unique1=function(){varn=[];
怎样制作视频NERO多媒体软件使用教程怎样制作视频NERO多媒体软件使用教程怎样制作视频NERO多媒体软件使用教程怎样制作视频NERO多媒体软件使用教程怎样制作