Android之实现视频播放

Android的视频播放有两种,如果你是想自己做个视频播放软件的话用MediaPlayer加SurfaceView实现,第二种调用Android自带VideoView实现,两种比较来说第二种简单容易,但是你如果想把播放软件做的用户体验跟爱奇艺一样的话,那就用第一种吧。

第一种实现方法
1.布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_two"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <SurfaceView
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:id="@+id/sv_main_surface"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <SeekBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar_main"
        android:layout_weight="1"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_media_play"
        android:onClick="mediapl"
        />
    </LinearLayout>
</LinearLayout>


2.java代码
public class TwoActivity extends AppCompatActivity {

    private SeekBar seekBar_main;
    private MediaPlayer mediaPlayer;
    private SurfaceView sv_main_surface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        seekBar_main = (SeekBar) findViewById(R.id.seekBar_main);
        sv_main_surface = (SurfaceView) findViewById(R.id.sv_main_surface);

        //给进度条设置拖动事件
        seekBar_main.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            //值发生改变时调用
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            //开始触摸时调用
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            //停止触摸时调用
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //获取停止拖动后的进度
                int progress = seekBar.getProgress();
                mediaPlayer.seekTo(progress);
            }
        });
    }

    public void mediapl(View view) throws IOException {
        ImageButton imageButton = (ImageButton) view;
//        if (mediaPlayer == null) {
        mediaPlayer = new MediaPlayer();
        //设置音频流的类型
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        //设置音频的来源
        mediaPlayer.setDataSource(this, Uri.parse("file://mnt/sdcard/Movies/sister.mp4"));
        //准备一下
        mediaPlayer.prepare();
        //将媒体播放器捕捉的画面展示
        mediaPlayer.setDisplay(sv_main_surface.getHolder());
        //播放音乐
        mediaPlayer.start();
        //当音乐播放时改变图片
        imageButton.setImageResource(android.R.drawable.ic_media_pause);
        //获取音乐的播放时间长度
        int duration = mediaPlayer.getDuration();
        //设置进度条的最大值
        seekBar_main.setMax(duration);
        //调用子线程
        new Mythread().start();
        //如果音频正在播放
    }

    class Mythread extends Thread {
        @Override
        public void run() {
            while (seekBar_main.getProgress() <= seekBar_main.getMax()) {
                //获取音乐的当前播放位置
                int current = mediaPlayer.getCurrentPosition();
                seekBar_main.setProgress(current);
            }
        }
    }
}


第二张实现方法
1.布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_play_video"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

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

</LinearLayout>

2.java代码
public class PlayVideoActivity extends AppCompatActivity {

    private VideoView vv_video;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video);
        vv_video = (VideoView) findViewById(R.id.vv_video);
        //设置视频路径
        vv_video.setVideoPath("file://mnt/sdcard/Movies/sister.mp4");
        //实例化媒体控制器
        MediaController mediaController=new MediaController(this);
        //互相映射一下
        mediaController.setMediaPlayer(vv_video);
        vv_video.setMediaController(mediaController);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值