·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> Android开发 >> 详解Android App中使用VideoView来实现视频播放的方法

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

作者:佚名      Android开发编辑:admin      更新时间:2022-07-23

通过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> 

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