package org.test
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import java.io.IOException;
public class VideoPlayerActivity extends Activity implements MediaPlayer.OnPreparedListener,
MediaPlayer.OnVideoSizeChangedListener, MediaPlayer.OnCompletionListener {
private SurfaceView surfaceView;
private MediaPlayer mediaPlayer;
private SurfaceHolder surfaceHolder;
private String resourcePathStr;
private Uri resourceUri;
private TVSeekBar seekBar;//自定义的seekbar
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
init();
playVideo();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (null != mediaPlayer) {
mediaPlayer.release();
mediaPlayer = null;
}
}
private void init() {
mediaPlayer = new MediaPlayer();
surfaceView = (SurfaceView) findViewById(R.id.video_sv);
seekBar = (TVSeekBar) findViewById(R.id.video_seekBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int process = seekBar.getProgress();
mediaPlayer.seekTo(process);
}
});
surfaceHolder = surfaceView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (mediaPlayer != null) {
mediaPlayer.setDisplay(holder);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (null != mediaPlayer) {
mediaPlayer.release();
mediaPlayer = null;
}
}
});
}
private void playVideo() {
mediaPlayer.reset();
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnVideoSizeChangedListener(this);
try {
mediaPlayer.setDataSource(this, resourceUri);
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.setScreenOnWhilePlaying(true);
surfaceHolder.setKeepScreenOn(true);
seekBar.setProgress(0);
seekBar.setMax(mediaPlayer.getDuration());
new Thread(new Runnable() {
@Override
public void run() {
while (seekBar.getProgress() <= seekBar.getMax()) {
int currentPosition = 0;
try {
if (mediaPlayer.isPlaying()) {
currentPosition = mediaPlayer.getCurrentPosition();
seekBar.setProgress(currentPosition);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
changeVideoSize();
}
@Override
public void onCompletion(MediaPlayer mp) {
if (null != mp) {
mp.release();
}
seekBar.setVisibility(View.INVISIBLE);
finish();
}
public void changeVideoSize() {
int videoWidth = mediaPlayer.getVideoWidth();
int videoHeight = mediaPlayer.getVideoHeight();
int surfaceHeight = surfaceView.getHeight();
int surfaceWidth = surfaceView.getWidth();
float max;
if (getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
max = Math.max((float) videoWidth / (float) surfaceWidth, (float) videoHeight / (float) surfaceHeight);
} else {
max = Math.max(((float) videoWidth / (float) surfaceHeight), (float) videoHeight / (float) surfaceWidth);
}
videoWidth = (int) Math.ceil((float) videoWidth / max);
videoHeight = (int) Math.ceil((float) videoHeight / max);
surfaceView.setLayoutParams(new RelativeLayout.LayoutParams(videoWidth, videoHeight));
}
}
Android_UI开发总结(九):MediaPlayer+SurfaceView+seekbar实现视频播放
最新推荐文章于 2022-04-07 15:16:01 发布