gpt4 book ai didi

java - 媒体播放器在 Activity 失去焦点时暂停音乐(当主页或屏幕关闭时)

转载 作者:行者123 更新时间:2023-11-29 10:10:27 26 4
gpt4 key购买 nike

当按下主页按钮或屏幕关闭时,媒体播放器停止播放音乐....即使 Activity 失去焦点我也希望音乐继续....但我希望音乐在来电和用户暂停时暂停音乐....当通过后退按钮关闭 Activity 时音乐也应该停止

这是我的代码示例:

package com.example.acer.aartisangrah;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class ekdanta extends AppCompatActivity implements Runnable, View.OnClickListener,SeekBar.OnSeekBarChangeListener {
TextView tv4;
Button b9, b10,but19;
int count = 0;
MediaPlayer play;
SeekBar seek_bar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ekdanta);
tv4 = (TextView) findViewById(R.id.textView9);
tv4.setTextSize(18);
tv4.setText(Html.fromHtml(getString(R.string.thirteen)));
b9 = (Button) findViewById(R.id.b9);
b10 = (Button) findViewById(R.id.b10);
seek_bar = (SeekBar) findViewById(R.id.seekBar);
seek_bar.setOnSeekBarChangeListener(this);
seek_bar.setEnabled(false);
but19 = (Button) findViewById(R.id.button19);
but19.setOnClickListener(this);
TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyManager.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private final PhoneStateListener mPhoneListener=new PhoneStateListener(){
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);

// Call receive state
if (state != TelephonyManager.CALL_STATE_IDLE) {
if ((play!= null) && (play.isPlaying()))
{
play.pause();
but19.setBackgroundResource(R.drawable.play);
}
}
}
};

public void run() {
int currentPosition = play.getCurrentPosition();
final int total = play.getDuration();
while (play != null && currentPosition < total) {
try {
Thread.sleep(1000);
currentPosition = play.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seek_bar.setProgress(currentPosition);
}
}
public void onClick(View v) {
if (v.equals(but19)) {
if (play == null) {
play = MediaPlayer.create(getApplicationContext(), R.raw.ekadanta);
seek_bar.setEnabled(true);
}
if (play.isPlaying()) {
play.pause();
but19.setBackgroundResource(R.drawable.play);
} else {
play.start();
but19.setBackgroundResource(R.drawable.pause);
seek_bar.setMax(play.getDuration());
new Thread(this).start();
}
}
play.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
play.seekTo(0);
but19.setBackgroundResource(R.drawable.play);
}
});
}

@Override
protected void onPause()
{
super.onPause();
}

@Override
protected void onDestroy()
{
super.onDestroy();
play.release();
}

@Override
public void onProgressChanged(SeekBar seek_bar, int progress, boolean fromUser) {
try{
if(play.isPlaying()||play!=null){
if (fromUser)
play.seekTo(progress);
}
else if(play==null){
Toast.makeText(getApplicationContext(),"First Play", Toast.LENGTH_SHORT).show();
seek_bar.setProgress(0);
}
}
catch(Exception e){
Log.e("seek bar",""+e);
seek_bar.setEnabled(false);
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}

public void increase(View inc) {
count++;
if (count == 1) {
tv4.setTextSize(22);
} else if (count == 2) {
tv4.setTextSize(30);
} else if (count >= 3) {
count = 3;
tv4.setTextSize(40);
}
}

public void decrease(View dec) {
count--;
if (count <= 0) {
tv4.setTextSize(18);
count = 0;
}
if (count == 1) {
tv4.setTextSize(22);
} else if (count == 2) {
tv4.setTextSize(30);
} else if (count == 3) {
tv4.setTextSize(40);
}
}
}

编辑

在将此代码用于 Activity 后...此 Activity 在某些设备上无法打开...该应用程序自行关闭...请帮助!!!没有此代码的其他 Activity 也能正常工作...

最佳答案

首先,你有如下代码

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

onPause 方法在每次 Activity 进入后台(失去焦点)或被用户关闭时执行。因此,您的第一步是从 onPause 方法中删除这段代码,您会在该方法中特意调用 play.pause()

您应该覆盖 onBackPressed() 方法并执行与您正在执行的操作类似的操作,但调用 play.stop()

检查 here ,特别是在 Performing cleanup 部分,其中显示以下内容

a MediaPlayer object can consume a significant amount of systemresources, so you should keep it only for as long as you need and callrelease() when you are done with it. It's important to call thiscleanup method explicitly rather than rely on system garbagecollection because it might take some time before the garbagecollector reclaims the MediaPlayer

请务必也查看该指南。

关于java - 媒体播放器在 Activity 失去焦点时暂停音乐(当主页或屏幕关闭时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38857168/

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