直接上代码
import androidx.appcompat.app.AppCompatActivity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback,
MediaPlayer.OnCompletionListener, MediaPlayer.OnVideoSizeChangedListener,
MediaPlayer.OnInfoListener, MediaPlayer.OnErrorListener,
MediaPlayer.OnPreparedListener, MediaPlayer.OnSeekCompleteListener {
SurfaceView surfaceView;
MediaPlayer mediaPlayer;
// String path=“http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8”; //中央一台
// String path=“http://ivi.bupt.edu.cn/hls/cctv5phd.m3u8”; //中央五台
String path=“http://ivi.bupt.edu.cn/hls/cctv6hd.m3u8”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView=findViewById(R.id.sfView);
surfaceView.setZOrderOnTop(false);
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().addCallback(this);
// startPlay();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startPlay();
}
},5000);
}
private void startPlay(){
if (mediaPlayer==null){
mediaPlayer=new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(MainActivity.this);
mediaPlayer.setOnVideoSizeChangedListener(MainActivity.this);
mediaPlayer.setOnInfoListener(MainActivity.this);
mediaPlayer.setOnErrorListener(MainActivity.this);
mediaPlayer.setOnPreparedListener(MainActivity.this);
mediaPlayer.setOnSeekCompleteListener(MainActivity.this);
}
try {
mediaPlayer.setDataSource(path);
mediaPlayer.setDisplay(surfaceView.getHolder());
mediaPlayer.prepare();
Log.e("tiwolf", "初始化: 设备准备好了" );
// mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.e("tiwolf", "surfaceCreated: 设备准备好了" );
// startPlay();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void onCompletion(MediaPlayer mp) {
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
return false;
}
@Override
public void onPrepared(MediaPlayer mp) {
int width=mp.getVideoWidth();
int height=mp.getVideoHeight();
if (width!=0 && height!=0){
int playWidth=surfaceView.getWidth();
int playHeight=surfaceView.getHeight();
Log.e("tiwolf", "onPrepared: 设备准备好了"+playWidth+";"+playHeight );
surfaceView.getHolder().setFixedSize(playWidth,playHeight);
mp.start();
}else{
mediaPlayer.start();
Log.e("tiwolf", "onPrepared: 设备准备好了,有宽高"+width+";"+height );
}
}
@Override
public void onSeekComplete(MediaPlayer mp) {
}
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
}
}