通過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>
當然,我們也可以播放網絡上的多媒體。
新聞熱點
疑難解答