我创建了媒体播放器运行时我有更改网址但是表面视图是黑色的,预期网址没有播放我在我的logcat中发现了一些错误我发布了我的错误和媒体播放器代码。为什么会这样?
代码:
public class MediaPlayerActivity extends Activity implements
SurfaceHolder.Callback, OnCompletionListener, OnClickListener {
/**
* MediaPlayer Field
*/
private MediaPlayer mediaPlayer;
/**
* surfaceHolder Field
*/
private SurfaceHolder surfaceHolder;
/**
* playerSurfaceView Field
*/
private SurfaceView playerSurfaceView;
/**
* wifiUSerName Field
*/
private static String wifiUSerName = "RMS";
/**
* List of Url Field
*/
private static List<Playfile> urls;
/**
* Button previous
*/
private Button prevButton;
/**
* Button play And Pause
*/
private Button playAndPauseButton;
/**
* Button Next
*/
private Button nextButton;
/**
* seekbar Field
*/
private SeekBar seekbar;
/**
* currentIndex Field
*/
private int currentIndex = 0;
/**
* PlayMode boolean
*/
private boolean isPlayMode = false;
/**
* FooterLayoutShow boolean
*/
private boolean isFooterLayoutShow = false;
/**
* progressDialog Field
*/
private ProgressDialog progressDialog;
/**
* Runnable Field to run on Every Second
*/
private Runnable onEverySecond = null;
/**
* FrameLayout Field
*/
private FrameLayout rootFrameLayout;
/**
* RelativeLayout footerButton
*/
private RelativeLayout footerButtonLayout;
// private ProgressBar progBar;
/**
* Method is called at very first time. set ui.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_player);
initViewControls();
getDataFromBundle();
/*
* if (MainActivity.progress.isShowing()) {
* MainActivity.progress.dismiss(); }
*/
mediaPlayer = new MediaPlayer();
}
/**
* Method is used to initialize media player object and flag, set default
* text on button.
*/
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
mediaPlayer = new MediaPlayer();
isPlayMode = true;
playAndPauseButton.setBackgroundResource(R.drawable.pause);
// Reset Urls
urls = MasterData.getValueBasedOnKey(wifiUSerName);
}
/**
* Method is used to get data from bundle.
*/
private void getDataFromBundle() {
if (getIntent().getExtras() != null) {
Bundle bundle = getIntent().getExtras();
wifiUSerName = bundle.getString("wifiusername");
urls = MasterData.getValueBasedOnKey(wifiUSerName);
} else {
showToast("No data available");
// Take the user to home
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);
}
}
/**
* Method is used to initialise view controls.
*/
private void initViewControls() {
playerSurfaceView = (SurfaceView) findViewById(R.id.playersurface);
prevButton = (Button) findViewById(R.id.activity_mediaplayer_prev_btn);
playAndPauseButton = (Button) findViewById(R.id.activity_mediaplayer_play_and_pause_btn);
nextButton = (Button) findViewById(R.id.activity_mediaplayer_next_btn);
seekbar = (SeekBar) findViewById(R.id.activity_mediaplayer_seekbar);
rootFrameLayout = (FrameLayout) findViewById(R.id.activity_media_player_root_layout);
footerButtonLayout = (RelativeLayout) findViewById(R.id.activity_mediaplayer_footer_button_layout);
footerButtonLayout.setVisibility(View.GONE);
prevButton.setOnClickListener(this);
playAndPauseButton.setOnClickListener(this);
nextButton.setOnClickListener(this);
surfaceHolder = playerSurfaceView.getHolder();
surfaceHolder.addCallback(this);
seekbar.setVisibility(View.INVISIBLE);
rootFrameLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
seekbar.setVisibility(View.INVISIBLE);
if (!isFooterLayoutShow) {
isFooterLayoutShow = true;
footerButtonLayout.setVisibility(View.VISIBLE);
} else {
isFooterLayoutShow = false;
footerButtonLayout.setVisibility(View.INVISIBLE);
}
} else {
seekbar.setVisibility(View.VISIBLE);
}
return true;
}
});
//
// showProgressDialog();
}
/**
* Surface Holder Callback Methods
*/
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
try {
playMediaPlayer();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
Utils.writeToSDCard("Exception -" + e.getMessage());
} catch (SecurityException e) {
// TODO Auto-generated catch block
Utils.writeToSDCard("Exception -" + e.getMessage());
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
Utils.writeToSDCard("Exception -" + e.getMessage());
}
}
/**
* Method is used to play media file.
*/
private void playMediaPlayer() {
if (urls != null) {
try {
if (currentIndex < urls.size()) {
//
// showProgressDialog();
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
}
mediaPlayer.reset();
mediaPlayer.setDisplay(surfaceHolder);
/*
* System.out.println(" url :: " +
* urls.get(currentIndex).getUrl());
*/
boolean a=MasterData.songtest;
/*if(MasterData.songtest){
mediaPlayer.setDataSource("http://122.165.94.246/rms/trans.mp4");
}else{*/
Log.e("print","for url :"+MasterData.songtest+urls.get(currentIndex).getUrl());
mediaPlayer.setDataSource(urls.get(currentIndex).getUrl());
/*}*/
showToast("Preparing media player");
//mediaPlayer.prepareAsync();
mediaPlayer.prepare();
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer
.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp,
int percent) {
// TODO Auto-generated method stub
//
// if(percent == 100){
// stopProgressDialog();
// }else {
// showProgressDialog();
// }
}
});
// ****************************************
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
/*
* if (progressDialog != null &&
* progressDialog.isShowing()) {
* progressDialog.dismiss(); }
*/
seekbar.setMax(mediaPlayer.getDuration());
seekbar.postDelayed(onEverySecond, 1000);
mediaPlayer.start();
// stopProgressDialog();
}
});
onEverySecond = new Runnable() {
@Override
public void run() {
if (seekbar != null && mediaPlayer != null) {
seekbar.setProgress(mediaPlayer
.getCurrentPosition());
}
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
seekbar.postDelayed(onEverySecond, 1000);
}
}
};
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
if (fromUser) {
// this is when actually seekbar has been seeked
// to a new position
mediaPlayer.seekTo(progress);
}
}
});
// ***************************************
} /*
* else { mediaPlayer.stop(); mediaPlayer.reset();
* playAndPauseButton.setBackgroundResource(R.drawable.stop); }
*/
} catch (IllegalStateException e) {
} catch (Exception e) {
Utils.writeToSDCard("Exception -" + e.getMessage());
// showToast("Some error occured!");
// stopProgressDialog();
}
} else {
showToast("No urls list present.Download xml file");
// stopProgressDialog();
// finish();
startActivity(new Intent(this, MainActivity.class));
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
mediaPlayer.reset();
}
}
/**
* Listner for Media File on Completion
*/
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
currentIndex++;
progressDialog = null;
playMediaPlayer();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
}
/*@Override
protected void onPause() {
super.onPause();
}*/
/**
* Method is used to controls all click event on view portion.
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.activity_mediaplayer_next_btn:
progressDialog = null;
if (mediaPlayer != null) {
playNextVideo();
}
break;
case R.id.activity_mediaplayer_prev_btn:
progressDialog = null;
if (mediaPlayer != null) {
playPrevVideo();
}
break;
case R.id.activity_mediaplayer_play_and_pause_btn:
playAndPauseVideo();
break;
default:
break;
}
}
/**
* Method is used to play and pause media player based on click event.
*/
private void playAndPauseVideo() {
if (isPlayMode) {
isPlayMode = false;
playAndPauseButton.setBackgroundResource(R.drawable.play);
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
} else {
isPlayMode = true;
playAndPauseButton.setBackgroundResource(R.drawable.pause);
if (mediaPlayer != null) {
mediaPlayer.start();
}
}
}
/**
* Method is used to play previous video.
*/
private void playPrevVideo() {
if (currentIndex > 0) {
//
// showProgressDialog();
currentIndex--;
playMediaPlayer();
} else {
showToast("No Prev Video Present.");
}
}
/**
* Method is used to play next video.
*/
private void playNextVideo() {
if (currentIndex < urls.size() - 1) {
//
// showProgressDialog();
currentIndex++;
playMediaPlayer();
} else {
showToast("No Next Video Present.");
}
}
/**
* Method is used to show toast based on message param.
*
* @param message
*/
private void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
/**
* Method is used to show progress dialog.
*/
private void showProgressDialog() {
if (progressDialog == null) {
progressDialog = ProgressDialog.show(this, "", "Buffering...");
}
}
/**
* Method is used to stop progress dialog.
*/
private void stopProgressDialog() {
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer = null;
}
}
}