Android之MediaPlayer简单应用

  Android系统中为了能够播放视频,我们需要使用到MediaPlayer相关的类和SurfaceView类,前者是视频控制,后者是承载视频的内容,类似于画板的作用。整个实现思路先自定义一个SurfaceView类,用于后期扩展需要,在自定义一个简单的视频播放器皮肤,然后将皮肤绑定到Java视图View上,在View里面完成控件加载和视频播放控制,整个实现代码如下。

1.播放器SurfaceView自定义类

import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceView;


public class VideoSurface extends SurfaceView {
    public VideoSurface(Context context) {
        super(context);
    }

    public VideoSurface(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public VideoSurface(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


}


2.自定义的一个简单皮肤


</pre><pre name="code" class="java">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.matrix.mediaplayer.VideoSurface
        android:id="@+id/video_surface"
        android:layout_width="350dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:visibility="visible" />

    <Button
        android:id="@+id/btn_play_media"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:inputType="text"
        android:text="Play"
        android:visibility="visible" />


</RelativeLayout>

 3. 自定义播放器 

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


/**
 * 自定义播放器
 */
public class CustomMediaPlayer extends LinearLayout {
    private static final String TAG = "CustomMediaPlayer";
    private VideoSurface videoSurface;
    private SurfaceHolder holder;
    private Button btnStartMedia;
    private View videoSkinView;
    private Context mContext;
    private MediaPlayer mediaPlayer;


    public CustomMediaPlayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initMediaView();
    }

    public CustomMediaPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initMediaView();
    }

    public CustomMediaPlayer(Context context) {
        super(context);
        this.mContext = context;
        initMediaView();
    }

    /*
     * 初始化视图控件以及相应的事件
     */
    private void initMediaView() {
        videoSkinView = LayoutInflater.from(mContext).inflate(R.layout.mediaplayer_skin, null);
        videoSurface = (VideoSurface) videoSkinView.findViewById(R.id.video_surface);
        btnStartMedia = (Button) videoSkinView.findViewById(R.id.btn_play_media);

        //添加播放事件
        btnStartMedia.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                play();
            }
        });
        //设置回调
        surfaceViewCallback();
        // 将控件添加到视图上绑定
        addView(videoSkinView);
    }

    /**
     * SurfaceView对应的Callback回调函数,当SurfaceView创建好,修改或者销毁时候回调
     */
    private void surfaceViewCallback() {
        holder = videoSurface.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                play();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });
    }

    /**
     * 播放视频
     */
    private void play() {
        //如果mediaplayer不是第一次创建,清除mediaplayer播放设置
        if (mediaPlayer != null) {
            mediaPlayer.reset();

        }

        mediaPlayer = MediaPlayer.create(mContext, R.raw.demo);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDisplay(holder);
        //设置播放前准备事件的监听,视频准备好播放后回调
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();
            }
        });
    }


}


4.Activity对应的XML文件中引用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.matrix.mediaplayer.CustomMediaPlayer
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:visibility="visible"></com.matrix.mediaplayer.CustomMediaPlayer>


</RelativeLayout>


5.Activity设置绑定视图

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值