gpt4 book ai didi

java - 屏幕关闭时继续播放音乐

转载 作者:行者123 更新时间:2023-11-30 01:03:54 26 4
gpt4 key购买 nike

即使屏幕关闭(即使用户锁定了他的手机)我也想继续播放音乐,当用户按下主页按钮并再次返回应用程序时暂停应该允许他从他暂停的地方继续......类似于用户通过文件浏览器播放音乐....

这是我的代码:

public class prathmeshvara extends AppCompatActivity implements Runnable,  View.OnClickListener, SeekBar.OnSeekBarChangeListener{
TextView tv25;
Button b47, b48, but32;
int count = 0;
MediaPlayer play3;
SeekBar seek_bar3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prathmeshvara);
ActionBar actionBar=getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.mipmap.icon);
tv25 = (TextView) findViewById(R.id.textView25);
tv25.setText(Html.fromHtml(getString(R.string.twentyone)));
b47 = (Button) findViewById(R.id.b47);
b48 = (Button) findViewById(R.id.b48);
seek_bar3 = (SeekBar) findViewById(R.id.seekBar3);
seek_bar3.setOnSeekBarChangeListener(this);
seek_bar3.setEnabled(false);
but32 = (Button) findViewById(R.id.button32);
but32.setOnClickListener(this);
}

public void run() {
int currentPosition = play3.getCurrentPosition();
final int total = play3.getDuration();
while (play3 != null && currentPosition < total) {
try {
Thread.sleep(1000);
currentPosition = play3.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seek_bar3.setProgress(currentPosition);
}
}

public void onClick(View v) {
if (v.equals(but32)) {
if (play3 == null) {
play3 = MediaPlayer.create(getApplicationContext(), R.raw.prathameshwara);
seek_bar3.setEnabled(true);
}
if (play3.isPlaying()) {
play3.pause();
but32.setBackgroundResource(R.drawable.play);
} else {
play3.start();
but32.setBackgroundResource(R.drawable.pause);
seek_bar3.setMax(play3.getDuration());
new Thread(this).start();
}
}
play3.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
play3.seekTo(0);
but32.setBackgroundResource(R.drawable.play);
}
});
}

@Override
protected void onPause()
{
super.onPause();
if (play3!= null)
{
play3.pause();
}
}

@Override
protected void onResume()
{
super.onResume();
if ((play3 != null) && (!play3.isPlaying())) {
but32.setBackgroundResource(R.drawable.play);
but32.setOnClickListener(this);
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
try {
if (play3.isPlaying() || play3 != null) {
if (fromUser)
play3.seekTo(progress);
} else if (play3 == null) {
Toast.makeText(getApplicationContext(), "First Play", Toast.LENGTH_SHORT).show();
seek_bar3.setProgress(0);
}
} catch (Exception e) {
Log.e("seek bar", "" + e);
seek_bar3.setEnabled(false);
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
}

最佳答案

基本上,为了达到您的目标,您不应该在 onPause() 语句中执行 play3.pause:

@Override
protected void onPause()
{
super.onPause();
if (play3!= null)
{
play3.pause();
}
}

现在,当您的应用进入 onPause 状态时,您的播放会暂停。顺便说一句,不要忘记花一些时间来实现 AudioFocus 处理。

关于java - 屏幕关闭时继续播放音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39080815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com