安卓MediaPlayer和VideoView简单使用
2016-09-22 18:27
218 查看
开发过程当中,难免会遇到需要用播放器的地方,下面就来说下安卓系统自带的MediaPlayer和VideoView。
思路:
MediaPlayer
首先创建一个MediaPlayer对象,然后调用setDataSource()方法来设置音频文件的路径,再调用prepare()方法让MediaPlayer进入准备状态,接下来调用start()开始播放,pause()暂停,reset()停止播放。release()释放掉相关资源,seekTo()从指定位置开始播放等等。
用法和举例:
声明变量 private MediaPlayer mMediaPlayer=new MediaPlayer();
在Activity的初始化方法里面写上初始化方法
public void initVideoPath() {
try{
File file = new File(Environment.getExternalStorageDirectory(), "文件名字带后缀的");
mMediaPlayer.setDataSource(file.getPath());
mMediaPlayer.prepare();
}catch (Exception e){
e.printStackTrace();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_paly:
if (!mMediaPlayer.isPlaying()) {
mMediaPlayer.start();
}
break;
case R.id.tv_pause:
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}
break;
case R.id.tv_replay:
if (mMediaPlayer.isPlaying()) {
initVideoPath();
}
break;
}
@Override
protected void onPause() {
super.onPause();
if (null!=mMediaPlayer){
mMediaPlayer.stop();
mMediaPlayer.release();
}
}
VideoView
方法差不多,setVideoPath()设置播放的视频文件位置,start()开始或者继续播放,pause()暂停播放,reseum()将视频从头开始播放,seekto()从指定位置开始播放视频,isPlaying()判断当前是否正在播放视频
用法和举例:
用之前要在布局文件里面写上该控件。
<VideoView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="100"/>
在Activity的初始化方法里面写上初始化方法
public void initVideoPath(){
File file=new File(Environment.getExternalStorageDirectory(),"文件名字带后缀的");
videoView.setVideoPath(file.getPath());
}
在写个点击事件
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_paly:
if (!videoView.isPlaying()) {
videoView.start();
}
break;
case R.id.tv_pause:
if (videoView.isPlaying()) {
videoView.pause();
}
break;
case R.id.tv_replay:
if (videoView.isPlaying()) {
videoView.resume();
}
break;
}
}
记得要在页面销毁的时候把资源释放掉
protected void onDestory() {
super.onPause();
if (null!=videoView){
videoView.suspend();
}
思路:
MediaPlayer
首先创建一个MediaPlayer对象,然后调用setDataSource()方法来设置音频文件的路径,再调用prepare()方法让MediaPlayer进入准备状态,接下来调用start()开始播放,pause()暂停,reset()停止播放。release()释放掉相关资源,seekTo()从指定位置开始播放等等。
用法和举例:
声明变量 private MediaPlayer mMediaPlayer=new MediaPlayer();
在Activity的初始化方法里面写上初始化方法
public void initVideoPath() {
try{
File file = new File(Environment.getExternalStorageDirectory(), "文件名字带后缀的");
mMediaPlayer.setDataSource(file.getPath());
mMediaPlayer.prepare();
}catch (Exception e){
e.printStackTrace();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_paly:
if (!mMediaPlayer.isPlaying()) {
mMediaPlayer.start();
}
break;
case R.id.tv_pause:
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
}
break;
case R.id.tv_replay:
if (mMediaPlayer.isPlaying()) {
initVideoPath();
}
break;
}
@Override
protected void onPause() {
super.onPause();
if (null!=mMediaPlayer){
mMediaPlayer.stop();
mMediaPlayer.release();
}
}
VideoView
方法差不多,setVideoPath()设置播放的视频文件位置,start()开始或者继续播放,pause()暂停播放,reseum()将视频从头开始播放,seekto()从指定位置开始播放视频,isPlaying()判断当前是否正在播放视频
用法和举例:
用之前要在布局文件里面写上该控件。
<VideoView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="100"/>
在Activity的初始化方法里面写上初始化方法
public void initVideoPath(){
File file=new File(Environment.getExternalStorageDirectory(),"文件名字带后缀的");
videoView.setVideoPath(file.getPath());
}
在写个点击事件
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_paly:
if (!videoView.isPlaying()) {
videoView.start();
}
break;
case R.id.tv_pause:
if (videoView.isPlaying()) {
videoView.pause();
}
break;
case R.id.tv_replay:
if (videoView.isPlaying()) {
videoView.resume();
}
break;
}
}
记得要在页面销毁的时候把资源释放掉
protected void onDestory() {
super.onPause();
if (null!=videoView){
videoView.suspend();
}
相关文章推荐
- Android之使用VideoView组件播放一个简单的视频
- 安卓VideoView使用
- 在安卓中使用VideoView来播放视频
- 安卓使用videoview进行音频、视频播放,及播放控制
- 视频02.MediaPlayer和VideoView,TextureView的使用
- Android之使用MediaPlayer和SurfaceView组件播放一个简单的视频
- [简单易懂]VideoCacheView使用解析,强大的视频缓存类
- Android VideoView的简单使用
- 安卓:WebView简单使用
- MediaPlayer简单使用,绑定surfaceView实现播放视频的功能
- 安卓自定义View之贝塞尔曲线简单使用
- 安卓_图片轮播效果,RollViewPager的简单使用
- 安卓开发:弹出自定义对话框自动播放视频(仿视频广告效果,使用mediaplayer和surfaceview做播放器)
- 安卓学习之路之ViewPager的简单使用
- android MediaPlayer和VideoView的使用
- Android中VideoView的简单使用
- Android 安卓使用VideoView播放本地视频
- 安卓中RecyclerView的简单使用和自定义RecyclerView的接口点击监听事件
- 安卓使用MediaPlayer和SurfaceView播放视频
- 安卓播放器VideoView的简单使用