|
发表于 2011-10-22 17:35:26
|
显示全部楼层
Re:求助,音乐播放器如何实现快进
Duzpp 的帖子
将视频播放器添加新功能<快进,快退,时间显示等> package org.hqx.android.surfaceiewvedio;
import java.io.IOException;
import java.sql.Time;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class SurfaceViewVedio extends Activity {
private SurfaceHolder hold = null;
private SurfaceView mPreview = null;
private MediaPlayer mp = null ;
private Surface su = null;
private ProgressBar progressbar = null;
private Button btnplay = null;
private Button btnstoo = null;
private TextView text1 = null;
private TextView text2 = null;
private Button btnPAUSED = null;
private Button btnaddgo = null;
private Button btnaddback = null;
private MediaPlayerState mediaPlayerState = MediaPlayerState.STOPPED;
private enum MediaPlayerState
{
STOPPED, PLAYING, PAUSED
}
private HandlerInvocation handlerInvocation = new HandlerInvocation();
private Handler timerHandler = new Handler();
private final int updateInterval = 500;
final private String currentFilePath = "/sdcard/android/kongfu.mp4";
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mPreview = (SurfaceView)this.findViewById(R.id.surface);
btnplay = (Button)this.findViewById(R.id.butcnplay);
btnstoo =(Button)this.findViewById(R.id.butcnstoo);
btnPAUSED = (Button)this.findViewById(R.id.butcnpause);
text1 =(TextView)this.findViewById(R.id.textv1);
text2 =(TextView)this.findViewById(R.id.textv2);
btnaddgo = (Button)this.findViewById(R.id.addgo);
btnaddback = (Button)this.findViewById(R.id.addback);
progressbar = (ProgressBar)this.findViewById(R.id.progress_horizontal);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
mPreview.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
hold.setFixedSize(getWindowManager().getDefaultDisplay().getWidth(),
getWindowManager().getDefaultDisplay().getHeight());
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NO_STATUS_BAR,
WindowManager.LayoutParams.FLAG_NO_STATUS_BAR);
}
});
btnstoo.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
mediaPlayerStop();
}
});
btnPAUSED.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
mediaPlayerPause();
}
});
btnplay.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
try {
hold = mPreview.getHolder();
su = hold.getSurface();
mediaPlayerPlay();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnaddgo.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
addgo();
}
});
btnaddback.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
addback();
}
});
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_SPACE ){
hold.setFixedSize(480,240);
return true;
}
return false;
}
private class HandlerInvocation implements Runnable
{
public void run()
{
displayProgress();
timerHandler.postDelayed(handlerInvocation, updateInterval);
}
};
private void displayProgress() {
Time progress = new Time(mp.getCurrentPosition());
Time remaining = new Time(mp.getDuration()-mp.getCurrentPosition());
text1.setText(progress.toString());
text2.setText("-"+remaining.toString());
int progressValue = 0;
if(mp.getDuration()>0){
progressValue = progressbar.getMax()*mp.getCurrentPosition()/mp.getDuration();
}
progressbar.setProgress(progressValue);
}
private void mediaPlayerPause()
{
mp.pause();
mediaPlayerState = MediaPlayerState.PAUSED;
timerHandler.removeCallbacks(handlerInvocation);
displayProgress();
}
private void addgo()
{
if(mediaPlayerState != MediaPlayerState.STOPPED){
mp.seekTo(mp.getCurrentPosition()+20);
mp.start();
timerHandler.postDelayed(handlerInvocation, updateInterval);
}
else{
timerHandler.removeCallbacks(handlerInvocation);
displayProgress();
}
}
private void addback()
{
if(mediaPlayerState != MediaPlayerState.STOPPED){
mp.seekTo(mp.getCurrentPosition()-20);
mp.start();
timerHandler.postDelayed(handlerInvocation, updateInterval);
}
else{
timerHandler.removeCallbacks(handlerInvocation);
displayProgress();
}
}
private void mediaPlayerStop()
{
mp.stop();
mediaPlayerState = MediaPlayerState.STOPPED;
timerHandler.removeCallbacks(handlerInvocation);
displayProgress();
}
private void mediaPlayerPlay()
{
if (mediaPlayerState == MediaPlayerState.STOPPED)
{
mediaPlayerInitialise(currentFilePath);
mediaPlayerState = MediaPlayerState.PLAYING;
timerHandler.postDelayed(handlerInvocation, updateInterval);
}
else{
mp.seekTo(mp.getCurrentPosition());
mp.start();
mediaPlayerState = MediaPlayerState.PLAYING;
timerHandler.postDelayed(handlerInvocation, updateInterval);
}
}
private void mediaPlayerInitialise(String filePath)
{
mediaPlayerState = MediaPlayerState.STOPPED;
mp = new MediaPlayer();
mp.setOnCompletionListener(new MediaPlayerCompletionListener());
try
{
mp.setDisplay(su);
mp.setDataSource(filePath);
mp.prepare();
mp.start();
displayProgress();
}
catch (IOException e)
{
// TODO: Handle this exception better
e.printStackTrace();
assert (false);
}
}
private class MediaPlayerCompletionListener implements MediaPlayer.OnCompletionListener
{
public void onCompletion(MediaPlayer mediaPlayer)
{
mediaPlayerStop();
}
}
}
[/code] |
|