android中使用VideoView播放视频

来源:互联网 发布:阿里域名备案 编辑:程序博客网 时间:2024/05/21 09:55

activity_main.xml

<?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:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.yh.myapplication.MainActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:id="@+id/play"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="play" />        <Button            android:id="@+id/pause"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="pause" />        <Button            android:id="@+id/replay"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="replay" />    </LinearLayout>    <VideoView        android:id="@+id/video_view"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <FrameLayout        android:id="@+id/placeholder"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:background="@drawable/apple" /></LinearLayout>

MainActivity.java

import android.media.MediaPlayer;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.MediaController;import android.widget.VideoView;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private VideoView videoView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        videoView = (VideoView) findViewById(R.id.video_view);        Button play = (Button) findViewById(R.id.play);        Button pause = (Button) findViewById(R.id.pause);        Button replay = (Button) findViewById(R.id.replay);        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {            @Override            public void onPrepared(MediaPlayer mediaPlayer) {                //Called when the video is ready to play                View placeholder = findViewById(R.id.placeholder);                placeholder.setVisibility(View.GONE);            }        });        play.setOnClickListener(this);        pause.setOnClickListener(this);        replay.setOnClickListener(this);        huanchong();        initVideoPath();    }    private void huanchong() {        MediaController mc = new MediaController(MainActivity.this);//Video是我类名,是你当前的类        videoView.setMediaController(mc);//设置VedioView与MediaController相关联    }    private void initVideoPath() {       // File file = new File("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");        videoView.setVideoPath("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");    }    @Override    public void onClick(View view) {        switch (view.getId()) {            case R.id.play:                if (!videoView.isPlaying()) {                    videoView.start();                }                break;            case R.id.pause:                if (videoView.isPlaying()) {                    videoView.pause();                }                break;            case R.id.replay:                if (videoView.isPlaying()) {                    videoView.resume();                }                break;        }    }    @Override    protected void onDestroy() {        super.onDestroy();        if (videoView != null) {            videoView.suspend();        }    }}
代码很简单,一看就可以明白的哦,希望大家有好的框架或者好的意见,一起相互学习哦大笑

0 0
原创粉丝点击