• SurfaceViewVideoList 列表视频(SurfaceView+Mediaplayer实现播放功能 本地)


    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    package com.example.asus.zhokao03;
    
    import android.os.Bundle;
    import android.os.Environment;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private Button play_btn;
        private MyVideoSurfaceView surface_view;
        private Button onplay_btn;
        private Button prepareplay_btn;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            initView();
        }
        //运行、可见
    
        @Override
        protected void onStart() {
            super.onStart();
        }
    
        //可交互
        @Override
        protected void onResume() {
            super.onResume();
        }
    
        private void play() {
            String videoPath = Environment.getExternalStorageDirectory().getPath() +
                    "/DCIM/Camera/VID_20171117_204539.mp4";//外部存储路径
            surface_view.playVideo(videoPath);
        }
    
        private void initView() {
            play_btn = (Button) findViewById(R.id.play_btn);
            surface_view = (MyVideoSurfaceView) findViewById(R.id.surface_view);
    
            play_btn.setOnClickListener(this);
            onplay_btn = (Button) findViewById(R.id.onplay_btn);
            onplay_btn.setOnClickListener(this);
            prepareplay_btn = (Button) findViewById(R.id.prepareplay_btn);
            prepareplay_btn.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                //播放视频
                case R.id.play_btn:
                    play();
                    break;
                //停止视频
                case R.id.onplay_btn:
                    surface_view.onplayVideo();
                    break;
                //重新播放视频
                case R.id.prepareplay_btn:
                    surface_view.prepareplayVideo();
                    play();
                    break;
            }
        }
    }
    package com.example.asus.zhokao03;
    
    import android.content.Context;
    import android.media.MediaPlayer;
    import android.util.AttributeSet;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    import java.io.IOException;
    
    /**
     * Created by asus on 2017/11/20.
     */
    
    public class MyVideoSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
       private SurfaceHolder mHolder;
        private MediaPlayer mMediaPlayer;
    
        public MyVideoSurfaceView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        private void init() {
            //获取Surface换掉那个区的持有者
            mHolder = getHolder();
            mHolder.addCallback(this);
            mHolder.setFixedSize(320,320);//分辨率
            //不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
    
        //设置播放
        public void playVideo(String path){
            if (mMediaPlayer == null){
                mMediaPlayer = new MediaPlayer();
            }
            try {
                //设置播放源
                mMediaPlayer.setDataSource(path);
                //设置多媒体的显示部分
                mMediaPlayer.setDisplay(mHolder);
                mMediaPlayer.prepare();//缓冲
                mMediaPlayer.start();//播放
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //设置停止
        public void onplayVideo(){
            if (mMediaPlayer !=null){
                //停止
                mMediaPlayer.stop();
                //播放
                mMediaPlayer.start();
            }
        }
        //设置重新播放
        public void prepareplayVideo(){
               //重置
                mMediaPlayer.reset();
    
        }
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
    
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
         mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }
    <?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.asus.zhokao03.MainActivity">
    
        <Button
            android:id="@+id/play_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放"/>
    <Button
        android:id="@+id/onplay_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止播放"/>
        <Button
            android:id="@+id/prepareplay_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="重新播放"/>
    <com.example.asus.zhokao03.MyVideoSurfaceView
        android:id="@+id/surface_view"
        android:layout_width="320dp"
        android:layout_height="320dp" />
    </LinearLayout>
  • 相关阅读:
    百度音乐搜索API
    浅谈对设计模式的理解
    AS3 Embed
    AS3.0中遍历删除容器内子对象的误区。
    如何获取GridView的总记录数?
    分享几个国外的Ajax&Jquery网站
    SQL2000: MMC 不能打开文件
    结合.net开发 谈谈 access 中 模糊查询语句 like的用法
    [转]C#里巧用DateTime预设一些可选的日期范围(如本年度、本季度、本月等)
    根据条件动态改变GridView某行或某个单元格的背景色
  • 原文地址:https://www.cnblogs.com/yudada/p/7880839.html
Copyright © 2020-2023  润新知