2
我正在製作一個自定義視頻播放器,並將其與視頻播放器一起使用,但是當我運行該應用程序時,它只顯示黑屏並播放音頻。SurfaceView不顯示來自媒體播放器的視頻
我的代碼:
public class MainActivity extends Activity implements SurfaceHolder.Callback {
private SeekBar seekbar;
private ImageView ivPlay;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private MediaPlayer mediaPlayer;
private Handler mHandler;
private Utilities utils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initObjects();
playVideo();
}
private void initObjects() {
seekbar = (SeekBar) findViewById(R.id.sbVideo);
seekbar.setOnSeekBarChangeListener(seekbarChangeListener);
mHandler = new Handler();
utils = new Utilities();
ivPlay = (ImageView) findViewById(R.id.ivPlay);
ivPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer.isPlaying()) {
if (mediaPlayer != null) {
mediaPlayer.pause();
// Changing button image to play button
ivPlay.setImageResource(R.drawable.ic_media_play);
}
} else {
// Resume song
if (mediaPlayer != null) {
mediaPlayer.start();
// Changing button image to pause button
ivPlay.setImageResource(R.drawable.ic_media_pause);
}
}
}
});
surfaceView = (SurfaceView) findViewById(R.id.svVideoSurface);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(surfaceHolder);
}
public void playVideo() {
// Play song
Uri uri = Uri
.parse("android.resource://com.videoplayer.demo/raw/title_video1");
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(MainActivity.this, uri);
mediaPlayer.prepare();
mediaPlayer.start();
// Changing Button Image to pause image
ivPlay.setImageResource(R.drawable.ic_media_pause);
// set Progress bar values
seekbar.setProgress(0);
seekbar.setMax(100);
// Updating progress bar
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private OnSeekBarChangeListener seekbarChangeListener = new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekbar) {
mHandler.removeCallbacks(mUpdateTimeTask);
int totalDuration = mediaPlayer.getDuration();
int currentPosition = utils.progressToTimer(seekbar.getProgress(),
totalDuration);
// forward or backward to certain seconds
mediaPlayer.seekTo(currentPosition);
// update timer progress again
updateProgressBar();
}
@Override
public void onStartTrackingTouch(SeekBar seekbar) {
// TODO Auto-generated method stub
mHandler.removeCallbacks(mUpdateTimeTask);
}
@Override
public void onProgressChanged(SeekBar seekbar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
};
@Override
public void surfaceChanged(SurfaceHolder holder, int arg1, int arg2, int arg3) {
mediaPlayer.setDisplay(holder);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
public void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask, 100);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long totalDuration = mediaPlayer.getDuration();
long currentDuration = mediaPlayer.getCurrentPosition();
// Updating progress bar
int progress = (int) (utils.getProgressPercentage(currentDuration,
totalDuration));
// Log.d("Progress", ""+progress);
seekbar.setProgress(progress);
// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer.release();
};
}
我不知道我在做什麼錯了,任何幫助將是巨大的。
你能格式化你的文章嗎? –
我不明白你是誰? cose已經格式化好了! –