Java 屏幕关闭时继续播放音乐
我想继续播放音乐,即使屏幕关闭(即使用户锁定了手机),当用户按下home(主页)按钮时暂停,再次返回应用程序应允许他从暂停的位置继续播放音乐…类似于用户通过文件浏览器播放音乐 这是我的密码:Java 屏幕关闭时继续播放音乐,java,android,media-player,android-mediaplayer,seekbar,Java,Android,Media Player,Android Mediaplayer,Seekbar,我想继续播放音乐,即使屏幕关闭(即使用户锁定了手机),当用户按下home(主页)按钮时暂停,再次返回应用程序应允许他从暂停的位置继续播放音乐…类似于用户通过文件浏览器播放音乐 这是我的密码: public class prathmeshvara extends AppCompatActivity implements Runnable, View.OnClickListener, SeekBar.OnSeekBarChangeListener{ TextView tv25; Button b4
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) {
}
}
公共类prathmeshvara扩展AppCompatActivity实现Runnable、View.OnClickListener、SeekBar.OnSeekbarChangListener{
文本视图tv25;
按钮b47、b48、but32;
整数计数=0;
MediaPlayer play3;
SeekBar seek_bar3;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Pratheshvara);
ActionBar ActionBar=getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(真);
actionBar.setIcon(R.mipmap.icon);
tv25=(TextView)findViewById(R.id.textView25);
setText(Html.fromHtml(getString(R.string.twentytone));
b47=(按钮)findViewById(R.id.b47);
b48=(按钮)findViewById(R.id.b48);
seek_bar3=(SeekBar)findViewById(R.id.seekBar3);
seek_bar3.setonseekbarchaneglistener(这个);
seek_bar3.setEnabled(false);
but32=(按钮)findViewById(R.id.button32);
但是32.setOnClickListener(这个);
}
公开募捐{
int currentPosition=play3.getCurrentPosition();
final int total=play3.getDuration();
while(play3!=null&¤tPosition
基本上,为了达到你的目标,你不应该做play3。在onPause()中暂停
@Override
protected void onPause()
{
super.onPause();
if (play3!= null)
{
play3.pause();
}
}
现在,当你的应用程序进入onPause状态时,你的播放会暂停。
顺便说一句,别忘了花点时间实施AudioFocus处理。我从以下来源了解了AudioFocus及其处理:
此外,在makeweight中,这里您有一些关于清理媒体资源的信息,以使您的应用程序不占用大量内存,并为其他应用程序发布媒体编解码器,以防它们也想使用它:
在您的玩家活动中,在onCreate
之前:
import android.media.AudioManager;
在onClick
方法中:
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener,
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED){
// we have audio focus now
// Create and setup the {@link MediaPlayer} for the audio resource associated with the current word
mMediaPlayer = MediaPlayer.create(YourActivity.this, word.getAudioResourceId());
// Start the audio file
mMediaPlayer.start();
// Setup a listener on the media player, so that we can stop and release the
// media player once the sound has finished playing.
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
此外,我建议您将此项添加到播放器活动中(但不在onCreate
中),以便正确释放媒体资源,并在按下停止按钮时使用此项,在其他情况下应停止播放:
/**
* Clean up the media player by releasing its resources.
*/
private void releaseMediaPlayer() {
// If the media player is not null, then it may be currently playing a sound.
if (mMediaPlayer != null) {
// Regardless of the current state of the media player, release its resources
// because we no longer need it.
mMediaPlayer.release();
// Set the media player back to null. For our code, we've decided that
// setting the media player to null is an easy way to tell that the media player
// is not configured to play an audio file at the moment.
mMediaPlayer = null;
// Regardless of whether or not we were granted audio focus, abandon it. This also
// unregisters the AudioFocusChangeListener so we don't get anymore callbacks.
mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);
}
}
你能帮我解决一下AudioFocusant吗?只是对我现有的代码做一些修改,然后解决问题……我无法得到,因为我最近开始编码。。。。
/**
* Clean up the media player by releasing its resources.
*/
private void releaseMediaPlayer() {
// If the media player is not null, then it may be currently playing a sound.
if (mMediaPlayer != null) {
// Regardless of the current state of the media player, release its resources
// because we no longer need it.
mMediaPlayer.release();
// Set the media player back to null. For our code, we've decided that
// setting the media player to null is an easy way to tell that the media player
// is not configured to play an audio file at the moment.
mMediaPlayer = null;
// Regardless of whether or not we were granted audio focus, abandon it. This also
// unregisters the AudioFocusChangeListener so we don't get anymore callbacks.
mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);
}
}