Android筆記:視訊播放器播放本地視訊和網路視訊
阿新 • • 發佈:2019-01-28
這篇博文主要是記錄一下VideoView的使用,這個demo使用VideoView來播放本地視訊和網路視訊。
先來看一下效果圖:
接下來說程式碼:
1。佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height ="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="app.com.myapplication.LocalVideoActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout >
</RelativeLayout>
2。Activity
package app.com.myapplication;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoActivity extends AppCompatActivity {
private VideoView videoView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_video);
//本地的視訊 需要在手機SD卡根目錄新增一個 fl1234.mp4 視訊
String videoUrl1 = Environment.getExternalStorageDirectory().getPath()+"/fl1234.mp4" ;
//網路視訊
String videoUrl2 = Utils.videoUrl ;
Uri uri = Uri.parse( videoUrl2 );
videoView = (VideoView)this.findViewById(R.id.videoView );
//設定視訊控制器
videoView.setMediaController(new MediaController(this));
//播放完成回撥
videoView.setOnCompletionListener( new MyPlayerOnCompletionListener());
//設定視訊路徑
videoView.setVideoURI(uri);
//開始播放視訊
videoView.start();
}
class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {
@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText( LocalVideoActivity.this, "播放完成了", Toast.LENGTH_SHORT).show();
}
}
}
程式碼展示完畢。程式碼比較簡單就不放demo原始碼了,有需要可以直接評論留言。