gpt4 book ai didi

android - 如何与自定义媒体播放器一起显示搜索栏

转载 作者:行者123 更新时间:2023-11-29 16:24:29 25 4
gpt4 key购买 nike

我已经构建了一个自定义媒体播放器,我还想显示搜索栏。搜索栏目前正在显示但不起作用,即播放歌曲时它没有进行。让我知道我哪里错了。以下是代码

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

}

}

最佳答案

在 mediplayer 启动后调用 updatePosition() 方法..它适用于我。

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

}

关于android - 如何与自定义媒体播放器一起显示搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5259707/

25 4 0