扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章将为大家详细讲解有关Android如何实现播放视频,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
淮安区网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联2013年开创至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。一、通过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
布局文件
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
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
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如何实现播放视频”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流