package com.xunao.effectdemo.activity;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.Nullable;

import com.xunao.effectdemo.R;

import java.io.IOException;

import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/**
 * author : 程中强
 * e-mail : 740479946@qq.com
 * date : 2022/9/1314:34
 * desc :
 * version: 1.0
 */
public class IJKPlayerActivity extends Activity {

	private SurfaceView showView;
	private IjkMediaPlayer ijkMediaPlayer;
	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ijkplay);
		showView = findViewById(R.id.surfaceView);

		//创建IjkPlayer对象
		ijkMediaPlayer = new IjkMediaPlayer();
		//设置Surface创建以及发生变化的回调
		showView.getHolder().addCallback(callback);
	}

	private SurfaceHolder.Callback callback = new SurfaceHolder.Callback() {
		@Override
		public void surfaceCreated(SurfaceHolder holder) {
		}
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
			//设置显示界面的Holder
			ijkMediaPlayer.setDisplay(holder);
			try {
				//设置播放地址
				ijkMediaPlayer.setDataSource(IJKPlayerActivity.this, Uri.parse("http://v3.cztv.com/cztv/vod/2018/06/28/7c45987529ea410dad7c088ba3b53dac/h264_1500k_mp4.mp4"),null);
			} catch (IOException e) {
				e.printStackTrace();
			}
			//播放前准备
			ijkMediaPlayer.prepareAsync();
			//开始播放
			ijkMediaPlayer.start();
		}
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (ijkMediaPlayer!=null){
			ijkMediaPlayer.stop();
		}
	}
}
