我已经为我的应用程序实现了一个搜索栏,这是一个简单的音乐播放器。但是当我播放音乐(从uri收到)时,它就不会在运行时自动更新。但当我将它移动到某个位置时,它会将歌曲移动到那里。任何想法如何解决这个问题?
我的代码:
public class MusicPlayer extends ThemableActivity implements SeekBar.OnSeekBarChangeListener {
MediaPlayer musicPlayer;
@Override
protected void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
setContentView(R.layout.media_player_layout);
Intent intent = getIntent();
String track = intent.getDataString();
TextView txt = (TextView)findViewById(R.id.txt);
txt.setGravity(Gravity.CENTER);
txt.setText(track);
ImageView img = (ImageView)findViewById(R.id.img);
img.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
final Uri uri = getIntent() != null ? getIntent().getData() : null;
final SharedPreferences premiumSettings = getSharedPreferences("PREMIUM", Context.MODE_PRIVATE);
final boolean isPremium = premiumSettings.getBoolean("isPremium", false);
final SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(this);
if (isPremium) {
musicPlayer = MediaPlayer.create(MusicPlayer.this, uri);
seekBar.setMax(musicPlayer.getDuration()/1000);
musicPlayer.start();
final ImageButton play = (ImageButton) findViewById(R.id.play);
if(!musicPlayer.isPlaying()){
play.setImageResource(android.R.drawable.ic_media_play);
}
else{
play.setImageResource(android.R.drawable.ic_media_pause);
}
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = musicPlayer.getCurrentPosition();
if(musicPlayer.isPlaying()){
musicPlayer.pause();
play.setImageResource(android.R.drawable.ic_media_play);
}
else {
musicPlayer.seekTo(position);
musicPlayer.start();
play.setImageResource(android.R.drawable.ic_media_pause);
}}
});
final Handler mHandler = new Handler();
Runnable mRunnable = new Runnable() {
@Override
public void run() {
if(musicPlayer!=null){
int mCurrentPosition = musicPlayer.getCurrentPosition() / 1000;
seekBar.setProgress(mCurrentPosition);
}
mHandler.postDelayed(this, 1000);
}
};
ImageButton exit = (ImageButton) findViewById(R.id.exit);
exit.setImageResource(android.R.drawable.btn_dialog);
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (musicPlayer.isPlaying()) {
musicPlayer.stop();
}
musicPlayer.release();
musicPlayer = null;
finish();
}
});
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(MusicPlayer.this);
builder.setTitle("Error");
builder.setMessage("You are not premium user. Please enter the promocode or buy the full version");
builder.setIcon(R.drawable.holo_dark_action_info);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}}
public void onBackPressed () {
if(musicPlayer.isPlaying()) {
musicPlayer.stop();
}
musicPlayer = null;
finish();
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (musicPlayer != null && b){
musicPlayer.seekTo(i * 1000);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
答案 0 :(得分:1)
在您发布的代码段中,runnable永远不会运行。我本来期望在Runnable本身之外的mHandler.post(mRunnable);
或mRunnable.run();
答案 1 :(得分:0)
创建一个简单的线程说" progressSeekBarOnEverySecond"
progressSeekBarOnEverySecond=new Runnable() {
@Override
public void run() {
if(seekBar != null)
{
seekBar.setProgress(musicPlayer.getCurrentPosition());
textviewCurrentVideoTime.setText((musicPlayer.getCurrentPosition()/1000)/60 +":"+(musicPlayer.getCurrentPosition()/1000)%60);
}
if(musicPlayer.isPlaying())
{
seekBar.postDelayed(onEverySecond, 1000);
}
}
};
并在你点击播放时运行线程..或者你想要使用的逻辑
textviewCurrentVideoTime是textview ..at,它显示了视频的当前时间。另请注意,一旦线程被调用,它将在1秒内自动调用自身,并且搜索栏将进入视频的当前位置