安卓应用中用VideoView来播放视频 详解Android App中使用VideoView来实现视频播放的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

安卓应用中用VideoView来播放视频 详解Android App中使用VideoView来实现视频播放的方法

chenzheng_java   2021-03-22 我要评论
想了解详解Android App中使用VideoView来实现视频播放的方法的相关内容吗,chenzheng_java在本文为您仔细讲解安卓应用中用VideoView来播放视频的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,VideoView,视频,播放,下面大家一起来学习吧。

通过VideoView播放视频的步骤:
1、在界面布局文件中定义VideoView组件,或在程序中创建VideoView组件
2、调用VideoView的如下两个方法来加载指定的视频
(1)setVidePath(String path):加载path文件代表的视频
(2)setVideoURI(Uri uri):加载uri所对应的视频
3、调用VideoView的start()、stop()、psuse()方法来控制视频的播放

VideoView通过与MediaController类结合使用,开发者可以不用自己控制播放与暂停

package cn.com.chenzheng_java; 
 
import android.app.Activity; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.MediaController; 
import android.widget.VideoView; 
import android.widget.MediaController.MediaPlayerControl; 
 
public class VideoActivity extends Activity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.video); 
     
    VideoView videoView = (VideoView)findViewById(R.id.videoView1); 
    /*** 
     * 将播放器关联上一个音频或者视频文件 
     * videoView.setVideoURI(Uri uri) 
     * videoView.setVideoPath(String path) 
     * 以上两个方法都可以。 
     */ 
    videoView.setVideoPath("data/yueding.mp3"); 
     
    /** 
     * w为其提供一个控制器,控制其暂停、播放……等功能 
     */ 
    videoView.setMediaController(new MediaController(this)); 
     
    /** 
     * 视频或者音频到结尾时触发的方法 
     */ 
    videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
      @Override 
      public void onCompletion(MediaPlayer mp) { 
        Log.i("通知", "完成"); 
      } 
    }); 
     
    videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { 
       
      @Override 
      public boolean onError(MediaPlayer mp, int what, int extra) { 
        Log.i("通知", "播放中出现错误"); 
        return false; 
      } 
    }); 
     
  } 
} 

video.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" android:layout_height="match_parent" 
  android:orientation="horizontal"> 
  <VideoView android:layout_height="match_parent" android:id="@+id/videoView1" 
    android:layout_width="wrap_content"></VideoView> 
</LinearLayout> 

当然,我们也可以播放网络上的多媒体。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们