surfaceview的生命周期
public class MainActivity extends Activity implements OnClickListener {
private EditText et_path;
private Button bt_play, bt_replay, bt_stop;
private SurfaceView sv_video;
private SeekBar sb;
private static MediaPlayer mediaPlayer;
private int currentPosition;
private boolean flag = false;
public static MediaPlayer getMediaPlayer() {
if (mediaPlayer == null) {
synchronized (MainActivity.class) {
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
}
}
}
return mediaPlayer;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.et_path = (EditText) this.findViewById(R.id.et_path);
this.bt_play = (Button) this.findViewById(R.id.bt_play);
this.bt_replay = (Button) this.findViewById(R.id.bt_replay);
this.bt_stop = (Button) this.findViewById(R.id.bt_stop);
this.sv_video = (SurfaceView) this.findViewById(R.id.sv_video);
this.sb = (SeekBar) this.findViewById(R.id.sb);
this.sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
if (mediaPlayer != null) {
int progress = arg0.getProgress();
mediaPlayer.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
}
});
this.bt_play.setOnClickListener(this);
this.bt_replay.setOnClickListener(this);
this.bt_stop.setOnClickListener(this);
/**
* 4.0以下的版本需要设置一下一个参数<br>
* 设置surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户界面
*/
this.sv_video.getHolder().setType(
SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.sv_video.getHolder().addCallback(new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
System.out.println("surfaceDestroyed........");
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
currentPosition = mediaPlayer.getCurrentPosition();
}
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
System.out.println("surfaceCreated........");
if (currentPosition > 0) {
play(currentPosition);
}
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
System.out.println("surfaceChanged........");
}
});
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
this.bt_play.setText("暂停");
} else if (mediaPlayer != null) {
this.bt_play.setText("播放");
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_play:
play(0);
break;
case R.id.bt_replay:
replay();
break;
case R.id.bt_stop:
stop();
break;
}
}
/**
* 播放或暂停音乐
*/
private void play(int currentPosition) {
// 播放状态
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
this.bt_play.setText("播放");
} else if (mediaPlayer != null) {
mediaPlayer.start();
this.bt_play.setText("暂停");
} else {
String path = this.et_path.getText().toString().trim();
File file = new File(path);
if (file.exists()) {
try {
mediaPlayer = getMediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(sv_video.getHolder());
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
mediaPlayer.start();
int duration = mediaPlayer.getDuration();
sb.setMax(duration);
flag = true;
new Thread() {
@Override
public void run() {
while (flag) {
int position = mediaPlayer.getCurrentPosition();
sb.setProgress(position);
try {
sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
mediaPlayer.seekTo(currentPosition);
mediaPlayer
.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(getApplication(), "播放完成", 0)
.show();
stop();
}
});
this.bt_play.setText("暂停");
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "播放错误", 0).show();
}
} else {
Toast.makeText(this, "音频文件不存在", 0).show();
}
}
}
private void replay() {
if (mediaPlayer != null) {
sb.setProgress(0);
mediaPlayer.start();
mediaPlayer.seekTo(0);
this.bt_play.setText("暂停");
return;
}
play(0);
}
private void stop() {
if (mediaPlayer != null) {
flag = false;
sb.setProgress(0);
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
this.bt_play.setText("播放");
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<!-- android:text="/storage/sdcard0/DCIM/Camera/VID_20130901_084921.mp4" -->
<EditText
android:id="@+id/et_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="/storage/sdcard0/DCIM/Camera/VID_20130901_084921.mp4" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/bt_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="播放" />
<Button
android:id="@+id/bt_replay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="重播" />
<Button
android:id="@+id/bt_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止" />
</LinearLayout>
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<SurfaceView
android:id="@+id/sv_video"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
在线视频
public class MainActivity extends Activity implements OnClickListener {
private EditText et_path;
private Button bt_play, bt_replay, bt_stop;
private SurfaceView sv_video;
private static MediaPlayer mediaPlayer;
private int currentPosition;
public static MediaPlayer getMediaPlayer() {
if (mediaPlayer == null) {
synchronized (MainActivity.class) {
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
}
}
}
return mediaPlayer;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.et_path = (EditText) this.findViewById(R.id.et_path);
this.bt_play = (Button) this.findViewById(R.id.bt_play);
this.bt_replay = (Button) this.findViewById(R.id.bt_replay);
this.bt_stop = (Button) this.findViewById(R.id.bt_stop);
this.sv_video = (SurfaceView) this.findViewById(R.id.sv_video);
this.bt_play.setOnClickListener(this);
this.bt_replay.setOnClickListener(this);
this.bt_stop.setOnClickListener(this);
/**
* 4.0以下的版本需要设置一下一个参数<br>
* 设置surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户界面
*/
this.sv_video.getHolder().setType(
SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.sv_video.getHolder().addCallback(new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
System.out.println("surfaceDestroyed........");
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
currentPosition = mediaPlayer.getCurrentPosition();
}
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
System.out.println("surfaceCreated........");
if (currentPosition > 0) {
play(currentPosition);
}
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
System.out.println("surfaceChanged........");
}
});
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
this.bt_play.setText("暂停");
} else if (mediaPlayer != null) {
this.bt_play.setText("播放");
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_play:
play(0);
break;
case R.id.bt_replay:
replay();
break;
case R.id.bt_stop:
stop();
break;
}
}
/**
* 播放或暂停音乐
*/
private void play(int currentPosition) {
// 播放状态
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.pause();
this.bt_play.setText("播放");
} else if (mediaPlayer != null) {
mediaPlayer.start();
this.bt_play.setText("暂停");
} else {
String path = et_path.getText().toString().trim();
try {
mediaPlayer = getMediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(sv_video.getHolder());
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.seekTo(currentPosition);
mediaPlayer
.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
Toast.makeText(getApplication(), "播放完成", 0)
.show();
stop();
}
});
this.bt_play.setText("暂停");
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "播放错误", 0).show();
}
}
}
private void replay() {
if (mediaPlayer != null) {
mediaPlayer.seekTo(0);
this.bt_play.setText("暂停");
return;
}
play(0);
}
private void stop() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
this.bt_play.setText("播放");
}
}
}
posted on 2013-06-02 19:00 Freedom000 阅读(187) 评论(0) 编辑 收藏 举报