1

我已经建立了一个自定义媒体播放器,我也想显示搜索栏。搜索栏现在正在显示,但没有功能,即在播放歌曲时它没有进行。让我知道我哪里错了。以下是代码

public class myPlayer extends Activity implements Runnable,SeekBar.OnSeekBarChangeListener {

    MediaPlayer mPlayer;
    Button play,stop;
    SeekBar seekbar;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        seekbar = (SeekBar)findViewById(R.id.seekbar);
        mPlayer = MediaPlayer.create(this,R.raw.sajnave);
        play=(Button)findViewById(R.id.play);
        stop=(Button)findViewById(R.id.stop);
        Toast.makeText(myPlayer.this, ""+mPlayer.getDuration(), Toast.LENGTH_SHORT).show();

        play.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mPlayer.start();

            }

        });

        stop.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(myPlayer.this, ""+mPlayer.getCurrentPosition(), Toast.LENGTH_SHORT).show();
                mPlayer.stop();
            }

        });
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

        int currentPosition = 0;

                int total = mPlayer.getDuration();

                seekbar.setMax(total);

                while (mPlayer != null && currentPosition < total) {

                    try {

                        Thread.sleep(1000);

                        currentPosition = mPlayer.getCurrentPosition();

                    } catch (InterruptedException e) {

                        return;

                    } catch (Exception e) {

                        return;

                    }

                    seekbar.setProgress(currentPosition);

                }

    }

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        // TODO Auto-generated method stub
        mPlayer.seekTo(arg1);
    }

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

    }

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

    }

}
4

2 回答 2

3

updatePosition()mediplayer 启动后调用方法..它对我有用。

private final Handler handler = new Handler();

private final Runnable updatePositionRunnable = new Runnable() {
    public void run() {
        updatePosition();
    }
};

private void updatePosition() {
    handler.removeCallbacks(updatePositionRunnable);
    if (player != null) seekbar.setProgress(player.getCurrentPosition());

    handler.postDelaye(updatePositionRunnable, 500);

}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
    // TODO Auto-generated method stub
    player.seekTo(arg1);
}

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

}

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

}
于 2011-10-04T05:26:43.070 回答
1
SurfaceView mPreview=(SurfaceView)findViewById(R.id.surface);    
mPreview.postDelayed(runnable,200);    
Runnable runnable = new Runnable() {

    @Override
    public void run() {
        if (mMediaPlayer != null) {
            if (isPlaying) {
                // converting current position into seconds so dividing by
                // 1000
                try {
                    currentPosition = mMediaPlayer.getCurrentPosition();
                    mVideoProgress
                            .setProgress((int) ((currentPosition / (float) mMediaPlayer
                                    .getDuration()) * 100f));

                } catch (Exception e) {
                    e.printStackTrace();
                }

                mPreview.postDelayed(runnable, 150);
            }
        }

    }
};
于 2011-03-10T13:25:08.957 回答