SeekBar与android 2.3.3 Mediaplayer!如何在我的项目中添加搜索栏?

时间:2012-12-24 06:02:27

标签: android seekbar

我想添加一个SeekBar来控制我的mediaplayer进程,但我不知道如何添加代码。请帮助我,并在我的项目代码中给出一些示例! package lamTo.OSDC_Project;

public class OSDC_Mini_ProjectActivity extends Activity implements MediaPlayer.OnCompletionListener {

    private ImageButton play,pause,stop;
    private MediaPlayer mp;
    private SeekBar seekbar;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        play = (ImageButton)findViewById(R.id.play);
        pause = (ImageButton)findViewById(R.id.pause);
        stop = (ImageButton)findViewById(R.id.stop);


        play.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                play();
            }
        });

        pause.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                pause();
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                stop();
            }
        });

        setup();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (stop.isEnabled()) {
            mp.stop();
        }
    }

    public void onCompletion(MediaPlayer mp) {
        stop();
    }

    private void play() {
        mp.start();
        play.setEnabled(false);
        pause.setEnabled(true);
        stop.setEnabled(true);
    }

    private void stop() {
        mp.stop();
        pause.setEnabled(false);
        stop.setEnabled(false);

        try {
            mp.prepare();
            mp.seekTo(0);
            play.setEnabled(true);
        }
        catch (Throwable t) {
            error(t);
        }
    }

    private void pause() {
        mp.pause();
        play.setEnabled(true);
        pause.setEnabled(false);
        stop.setEnabled(true);
    }

    private void loadClip() {
        try {       
            mp=MediaPlayer.create(this, R.raw.bigbang);
            mp.setOnCompletionListener(this);
        }
        catch (Throwable t) {
            error(t);
        }
    }

    private void setup() {
        loadClip();
        play.setEnabled(true);
        pause.setEnabled(false);
        stop.setEnabled(false);
    }

    private void error(Throwable t) {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder
            .setTitle("Error!")
            .setMessage(t.toString())
            .setPositiveButton("Yes", null)
            .show();
    }
}

1 个答案:

答案 0 :(得分:0)

在您的设计版面中,添加此

<SeekBar
 android:id="@+id/SeekBar01"
 android:layout_height="wrap_content"
 android:layout_width="fill_parent"
 />

在你的onCreate()中添加这个

seekBar = (SeekBar) findViewById(R.id.SeekBar01);
seekBar.setMax(mPlayer.getDuration());
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}

public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}

public void onProgressChanged(SeekBar seekBar, int progress,
 boolean fromUser) {
seekChange(seekBar, progress, fromUser); 

}
});
 }

 // This makes the seekbar follow the music
public void startPlayProgressUpdater() {
seekBar.setProgress(mPlayer.getCurrentPosition());

if (mPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
startPlayProgressUpdater();
}
};
handler.postDelayed(notification,1000);
}else{
mPlayer.pause();
buttonPlayStop.setText(getString(R.string.play_str));
seekBar.setProgress(0);
}
 } 

// This is event handler thumb moving event
private void seekChange(SeekBar seekBar, int progress,
boolean fromUser){
mPlayer.seekTo(progress);
 }

完整教程here包含源代码。