我正在开发一个包含两个活动的应用程序,一个活动是媒体播放器,另一个活动是要播放的歌曲列表,mp3播放器工作正常,从带有该列表的活动中传递歌曲名称和播放器效果很好。我有两个问题,如果用户播放歌曲并离开应用程序(歌曲一直在后台播放,这是假定的工作方式),则用户返回到应用程序,seekbar栏设置为0,计时器设置为0,是否有一种方法可以“保存”活动“ ...也可以是,如果正在播放一首歌曲,而用户尝试播放另一首歌曲,则该歌曲会在前一首歌曲的上方播放,我尝试通过添加进入我的意图的“关键”,以识别是否是新音频,然后执行以下操作:但不起作用。
if (playerL != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop;
}
}
public class AudioPlayer extends Activity {
/////////////////////////////////////////////////////////////
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_player_activity);
// Header
Bundle extra = getIntent().getExtras();
if (extra != null) {
Intent intent = getIntent();
id_d = intent.getStringExtra("Id");
heading = intent.getStringExtra("Heading");
fileN = intent.getStringExtra("fileName");
audioN = intent.getStringExtra("audioName");
playerL = intent.getStringExtra("newAudio");
meet_instructor_round_image = findViewById(R.id.audio_player_img);
playerHeading = findViewById(R.id.audio_player_heading);
playerHeading.setText(heading);
Picasso.with(this).load(imgUrl).transform(new CropCircleTransformation()).into(round_image);
createNotificationChannel();
activateNotification(id_d, heading, imgUrl, bio);
} else {
Intent intent = new Intent(getApplicationContext(), com.starvizn.newstarvizn.COMMON.Activities.MainActivity.class);
startActivity(intent);
}
pause = findViewById(R.id.btnAudioSubpause);
play = findViewById(R.id.btnAudioSubPlay);
songName = findViewById(R.id.workoutName);
initialTime = findViewById(R.id.initialTime);
songName.setText(audioN);
Uri uri = Uri.parse(getApplicationContext().getFilesDir()+"/Downloads/"+fileN+".mp3");
mediaPlayer = MediaPlayer.create(this, uri);
seekBar = findViewById(R.id.seekBar);
seekBar.setClickable(false);
pause.setVisibility(View.INVISIBLE);
// Open lesson view
lessons_layout = findViewById(R.id.player_lessons);
lessons_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG, "Aqui");
Intent intent = new Intent(getApplicationContext(), com.myApp.MainActivity.class);
intent.putExtra("Id", id_d);
startActivity(intent);
}
});
if (playerL != null) {
if (mediaPlayer.isPlaying()) {
}
}
}
public void player_play(View view) {
play.setVisibility(View.INVISIBLE);
pause.setVisibility(View.VISIBLE);
mediaPlayer.start();
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
if (oneTimeOnly == 0) {
seekBar.setMax((int) finalTime);
oneTimeOnly = 1;
}
initialTime.setText(String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) startTime)))
);
seekBar.setProgress((int) startTime);
myHandler.postDelayed(UpdateSongTime, 100);
seekBar.setClickable(false);
pause.setVisibility(View.VISIBLE);
}
public void player_pause(View view) {
pause.setVisibility(View.INVISIBLE);
play.setVisibility(View.VISIBLE);
int temp = (int) startTime;
mediaPlayer.pause();
}
public void player_fwd(View view) {
int temp = (int) startTime;
if ((temp + fwdTime) <= finalTime) {
startTime = startTime + fwdTime;
mediaPlayer.seekTo((int) startTime);
} else {
Toast.makeText(getApplicationContext(), "Cannot jump forward 5 seconds!", Toast.LENGTH_LONG).show();
}
}
public void player_back(View view) {
int temp = (int) startTime;
if ((temp - backTime) > 0) {
startTime = startTime - backTime;
mediaPlayer.seekTo((int) startTime);
} else {
Toast.makeText(getApplicationContext(), "Cannot jump backward 5 seconds", Toast.LENGTH_LONG).show();
}
}
private Runnable UpdateSongTime = new Runnable() {
public void run() {
startTime = mediaPlayer.getCurrentPosition();
initialTime.setText(String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime)))
);
seekBar.setProgress((int) startTime);
myHandler.postDelayed(this, 100);
}
};
@Override
public void onBackPressed() {
builder = new android.app.AlertDialog.Builder(this);
builder.setTitle("End Player").setMessage("Exit").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancelAll();
Intent intent = new Intent(getApplicationContext(), com.myapp.MainActivity.class);
intent.putExtra("Id", id_d);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
}
答案 0 :(得分:0)
所以我想您只需要添加假定处理该过程的逻辑即可。正如文档所述,您有两种方法
getCurrentPosition()
和seekTo(long, int)
因此,我想您假设例如将当前位置保存在首选项中,然后在seekTo方法的帮助下使用之前保存的数据继续。这样一来,您就可以管理状态
当我在Player应用程序上工作时,我使用了ExoPlayer,它更加灵活且易于使用。您可以尝试下面的链接 https://codelabs.developers.google.com/codelabs/exoplayer-intro/#0