码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-11-22 21:53:52      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:res   ddc   direct   pre   this   内容   mis   linear   external   

<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>

 

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

标签:res   ddc   direct   pre   this   内容   mis   linear   external   

原文地址:http://www.cnblogs.com/yudada/p/7880839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!