我创建了一个音乐流应用程序,从firebase获取数据,但我在我的应用程序中有一个问题,当我播放一首歌曲然后再回来然后搜索栏没有显示当前持续时间。所以我认为我应该使用共享首选项,但我不知道如何使用可以任何人建议我的音乐播放器的代码
public class ViewUploadsActivity extends AppCompatActivity implements
MediaPlayer.OnBufferingUpdateListener,View.OnTouchListener
,MediaPlayer.OnCompletionListener{
//the listview
ListView listView;
private SeekBar seekBarProgress;
private int mediaFileLengthInMilliseconds;
private final Handler handler = new Handler();
CustomArrayAdapter adapter;
//database reference to get uploads data
DatabaseReference mDatabaseReference;
//list to store uploads data
List<Upload> uploadList = new ArrayList<>();
private TextView mSelectedTrackTitle,selected_track_ar;
private ImageView mSelectedTrackImage;
private MediaPlayer mMediaPlayer =new MediaPlayer();
private ImageView mPlayerControl;
Toolbar toolbar2;
TextView length;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_upload);
listView = (ListView) findViewById(R.id.listView);
//status = (TextView) findViewById(R.id.status);
seekBarProgress = (SeekBar)findViewById(R.id.length);
listView.setStackFromBottom(true);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
togglePlayPause();
}
});
mMediaPlayer.setOnCompletionListener(new
MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mPlayerControl.setImageResource(R.drawable.ic_play);
}
});
mSelectedTrackTitle = (TextView)findViewById(R.id.selected_track_title);
mSelectedTrackImage =
(ImageView)findViewById(R.id.selected_track_image);
mSelectedTrackTitle.setSelected(true);
mPlayerControl = (ImageView)findViewById(R.id.player_control);
selected_track_ar=(TextView)findViewById(R.id.selected_track_ar);
mPlayerControl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
togglePlayPause();
}
});
get();
//getting the database reference
mDatabaseReference = FirebaseDatabase.getInstance().getReference
(Constants.DATABASE_PATH_UPLOADS);
//retrieving upload data from firebase database
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
uploadList.add(upload);
}
adapter = new CustomArrayAdapter(getApplicationContext(),
(ArrayList<Upload>) uploadList);
listView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
Intent svc=new Intent(this, MySongServices.class);
startService(svc);
}
private void togglePlayPause() {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.pause();
mPlayerControl.setImageResource(R.drawable.ic_play);
} else if(!mMediaPlayer.isPlaying()){
mMediaPlayer.start();
mPlayerControl.setImageResource(R.drawable.ic_pause);
mediaFileLengthInMilliseconds = mMediaPlayer.getDuration();
primarySeekBarProgressUpdater();
}
}
public void get()
{
seekBarProgress.setMax(99); // It means 100% .0-99
seekBarProgress.setOnTouchListener(this);
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int
i, long l) {
//getting the upload
Upload upload = uploadList.get(i);
mSelectedTrackTitle.setText(upload.getName());
selected_track_ar.setText(upload.getAr());
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
mMediaPlayer.reset();
seekBarProgress.setProgress(0);
}
try {
mMediaPlayer.setDataSource(upload.getUrl());
mMediaPlayer.prepare();
show();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void primarySeekBarProgressUpdater() {
try {
seekBarProgress.setProgress((int) (((float)
mMediaPlayer.getCurrentPosition()
/ mediaFileLengthInMilliseconds) * 100));
if (mMediaPlayer.isPlaying()) {
Runnable notification = new Runnable() {
public void run() {
primarySeekBarProgressUpdater();
}
};
handler.postDelayed(notification, 1000);
}
}
catch (NullPointerException e)
{
e.printStackTrace();
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(v.getId() == R.id.length){
/** Seekbar onTouch event handler. Method which seeks MediaPlayer
to
seekBar primary progress position*/
if(mMediaPlayer.isPlaying()){
SeekBar sb = (SeekBar)v;
int playPositionInMillisecconds = (mediaFileLengthInMilliseconds
/ 100) * sb.getProgress();
mMediaPlayer.seekTo(playPositionInMillisecconds);
}
}
return false;
}
}
答案 0 :(得分:0)
我认为你需要使用android生命周期 onPause(), onResume()函数来检测应用程序何时在后台和前台运行并且取决于你需要更新的条件所有必需的值。
有关详细信息,请查看 - https://developer.android.com/guide/components/activities/activity-lifecycle.html#onresume