视频播放器VideoView

【VideoView】
支持格式限制:
flv/3gp/mp4
==================================================
@设置资源和关联控制器
·videoView.setVideoPath(path);/videoView.setVideoURI(uri); 
·videoView.setMediaController(mediaController);
·mediaController.setMediaPlayer(videoView);
----------------------------------------
@播放控制
·videoView.start();
·videoView.seekTo(currentPosition);
·videoView.pause();
·videoView.stopPlayback();
·videoView.getDuration();
·videoView.getCurrentPosition();
----------------------------------------
@设置控制条位置
·mediaController.setPadding(0, 0, 0, 0);
----------------------------------------
@显示或隐藏控制条
·mediaController.hide();
·mediaController.show(0);
----------------------------------------
@获取视频缩略图
	MediaMetadataRetriever retriever = new MediaMetadataRetriever();
	retriever.setDataSource(filePath);
	bitmap = retriever.getFrameAtTime();
	bitmap =retriever.getFrameAtTime(10000);
	retriever.release();

代码:

public class MainActivity extends AppCompatActivity {
    private VideoView vv;
    Button b1,b2;
    private MediaController mediaController;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vv = (VideoView) findViewById(R.id.vv);
        b1= (Button) findViewById(R.id.btn_raw);
        b2= (Button) findViewById(R.id.btn_onLine);
        //设置播放路径
        vv.setVideoPath("android.resource://com.example.videoview/"+R.raw.abc);
        //设置系统控制按钮(暂停,播放,拖动条)
        mediaController = new MediaController(this);
        vv.setMediaController(mediaController);
    }

    public void onBClick(View view ){
        switch (view.getId()){
            case R.id.btn_raw:
                 if (vv.isPlaying())
                 {
                     vv.pause();
                 }else{
                     vv.start();
                 }
                break;
            case R.id.btn_onLine:
                 vv.setVideoURI(Uri.parse("http://i.snssdk.com/neihan/video/playback/?video_id=840aebabb21d4ed7a27dfd5f993f86e3&quality=480p&line=0&is_gif=0.mp4"));
//                vv.setVideoPath("http://i.snssdk.com/neihan/video/playback/?video_id=840aebabb21d4ed7a27dfd5f993f86e3&quality=480p&line=0&is_gif=0.mp4");
                vv.start();
                break;
            case R.id.btn_onFile:
                vv.setVideoURI(Uri.fromFile(new File(Environment.getExternalStorageDirectory()+ File.pathSeparator+"abc.mp4")));
                vv.start();
        }
    }
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.example.videoview.MainActivity">

   <VideoView
       android:id="@+id/vv"
       android:layout_width="match_parent"
       android:layout_margin="10dp"
       android:layout_height="300dp" />

   <Button
       android:layout_width="match_parent"
       android:layout_margin="10dp"
       android:onClick="onBClick"
       android:id="@+id/btn_raw"
       android:text="播放raw视频"
       android:layout_height="wrap_content" />

   <Button
       android:layout_width="match_parent"
       android:layout_margin="10dp"
       android:onClick="onBClick"
       android:id="@+id/btn_onLine"
       android:text="播放网络视频"
       android:layout_height="wrap_content" />

   <Button
       android:layout_width="match_parent"
       android:layout_margin="10dp"
       android:onClick="onBClick"
       android:id="@+id/btn_onFile"
       android:text="播放文件"
       android:layout_height="wrap_content" />
</LinearLayout>
运行结果:




猜你喜欢

转载自blog.csdn.net/yang__k/article/details/80216337