您的位置:首页 > 其它

使用SurfaceView播放视频

2016-04-05 16:40 369 查看

说明:

看百思上面第一次启动时有个视频短片介绍,本来打算用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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: