Я внедрил панель поиска в свое приложение, которое представляет собой простой музыкальный проигрыватель. Но когда я играю музыку (полученную от 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) {
}