使用SurfaceView播放视频

说明:

看百思上面第一次启动时有个视频短片介绍,本来打算用VideoView播放视频呢!可扯淡的是怎么也不能控制与父控件的大小…..索性用SurfaceView进行播放期间也有可多坑!!!!直接上代码吧!

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/advideo_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/VideoLayout"
        android:layout_width="fill_parent"
        android:layout_height="260.0dip"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10.0dip"
        android:layout_marginRight="10.0dip"
        android:layout_marginTop="110.0dip"
        android:background="@drawable/ad_video_bg">

        <SurfaceView
            android:id="@+id/surfaceView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="10.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginRight="10.0dip"
            android:layout_marginTop="60.0dip" />
    </RelativeLayout>

    <Button
        android:id="@+id/jumpBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/VideoLayout"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="88.0dip"
        android:background="@drawable/splash_jump_selected_bg" />
</RelativeLayout>

JAVA:

package b.s.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import b.s.R;
import b.s.utils.SharePrefUtil;

/**
 * Created by Administrator on 2016/4/5.
 */
public class VideoSplashActivity extends Activity implements View.OnClickListener {

    private SurfaceView mVideo;
    private MediaPlayer mMediaPlayer;
    private Button mJumpBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_splash);
        mVideo = (SurfaceView) findViewById(R.id.surfaceView);
        mJumpBtn = (Button) findViewById(R.id.jumpBtn);
        mJumpBtn.setOnClickListener(this);

        initSurfaceView();


    }

    private void initSurfaceView() {
        //保持屏幕常亮
        mVideo.getHolder().setKeepScreenOn(true);

        mVideo.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                initMediaPlayer();
            }

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


            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

                mMediaPlayer.release();
                Log.i(">>>>>>>>>>>>>>>>>>>>>", "销毁了");
            }
        });
    }

    private void initMediaPlayer() {

        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.reset();//初始化
            AssetFileDescriptor afd = getAssets().openFd("advideo.mp4");//获取视频资源
            mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            mMediaPlayer.setDisplay(mVideo.getHolder());
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mMediaPlayer.start();
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(VideoSplashActivity.this, SplashActivity.class);
        startActivity(intent);
        SharePrefUtil.saveBoolean(VideoSplashActivity.this, "firstLogin", true);
        finish();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值