gpt4 book ai didi

android - 如何在 Android 来电时停止音频播放

转载 作者:行者123 更新时间:2023-11-30 02:50:51 26 4
gpt4 key购买 nike

你好我是 android 的新手,我正在为我的教堂做一个简单的在线广播应用程序问题是我需要播放服务停止并在电话接到电话并结束时重新启动到目前为止,我可以很好地停止服务,但是当它再次启动时就崩溃了,所以我不知道我做错了什么

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.res.Resources;
import android.os.IBinder;
import android.os.PowerManager.WakeLock;
import android.support.v4.app.NotificationCompat;
import com.spoledge.aacdecoder.MultiPlayer;


public class RadioService extends Service{


WakeLock wakelock;
//TelephonyManager teleManager= null;
//PhoneStateListener listener;
NotificationManager mNotificationManager;
public static MultiPlayer aacPlayer;
public static String radio;
private static Boolean isTuning;

// private TeleListener myListener = null;



@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}


@Override
public void onCreate(){
super.onCreate();
setTuning(false);

setRadio(getString(R.string.rhema));
aacPlayer = new MultiPlayer();





}


@Override
public int onStartCommand(Intent intent, int flags, int startId){

setTuning(true);

setTuning(true);

aacPlayer.playAsync(radio);
//CallReceiver.setTuning(true);

Resources r= getResources();
String[] Notify = r.getStringArray(R.array.PlayNotify);
// prepare intent which is triggered if the
// notification is selected

Intent inten = new Intent(this, PlayerActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, inten, 0);

// build notification
// the addAction re-use the same intent to keep the example short
NotificationCompat.Builder n = new NotificationCompat.Builder(this)
.setContentTitle(Notify[0])
.setContentText(Notify[1])
.setSmallIcon(R.drawable.noti_icon)
.setContentIntent(pIntent)
.setAutoCancel(true)
.setOngoing(true)
.setContentIntent(pIntent);

Notification hola = n.build();
startForeground(100, hola);

//startForeground(startId, notification);
return START_STICKY;
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
pause();


//CallReceiver.setTuning(false);
super.onDestroy();

}
public static boolean getTuning() {
return isTuning;
}
public void setTuning(boolean Tuning) {
isTuning = Tuning;
}
public void setRadio(String r){
radio = r;
}

public void PlayNotify(){
Resources r= getResources();
String[] Notify = r.getStringArray(R.array.PlayNotify);
// prepare intent which is triggered if the
// notification is selected

Intent intent = new Intent(this, RadioService.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

// build notification
// the addAction re-use the same intent to keep the example short
NotificationCompat.Builder n = new NotificationCompat.Builder(this)
.setContentTitle(Notify[0])
.setContentText(Notify[1])
.setSmallIcon(R.drawable.noti_icon)
.setContentIntent(pIntent)
.setAutoCancel(true);

n.setOngoing(true);
n.setContentIntent(pIntent);
//NotificationManager mNotificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// mId allows you to update the notification later on.
mNotificationManager.notify(100, n.build());
}

public static void play(){
aacPlayer.playAsync(radio);

}
public static void pause(){
aacPlayer.stop();

}

}

这是广播

    import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class CallReceiver extends BroadcastReceiver{
TelephonyManager telManager;
Context context;
public static boolean Tuning = false;
PlayerActivity mainActivity;
NotificationManager mNotificationManager;

@Override
public void onReceive(Context context, Intent intent) {


mainActivity=new PlayerActivity();


this.context=context;

telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

}

private final PhoneStateListener phoneListener = new PhoneStateListener() {

@Override
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: {
//PAUSE
if(Tuning){

context.stopService(new Intent(context,RadioService.class));
}

break;
}
case TelephonyManager.CALL_STATE_OFFHOOK: {
if(Tuning){

context.stopService(new Intent(context,RadioService.class));
}

break;
}
case TelephonyManager.CALL_STATE_IDLE: {
//PLAY
if(Tuning){

showNotification(context);
context.stopService(new Intent(context,RadioService.class));}
break;
}
default: { }
}
} catch (Exception ex) {

}
}

};

public static void setTuning(boolean r){

Tuning = r;
}
private void showNotification(Context context) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, PlayerActivity.class), 0);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.noti_icon)
.setContentTitle("Rhema Stereo")
.setContentText("Vuelve a Sintonizarnos.");
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());}

}

奇怪的是,从 Activity 中我可以很好地启动和停止服务,但在这里我只能停止它。所以现在我使用通知来返回 Activity 并重新启动服务

最佳答案

如果可以的话,我会建议另一种方法来解决这个问题。您声明:

i need the playback service to stop and restart when the phone gets a call

然而,这个问题更复杂。您还应该停止播放,例如,当通知到达时,或者如果用户启动了另一个音乐播放器。 Android 不必独立监控所有这些可能性,而是为这个概念提供内置支持——它称为音频焦点。从广义上讲,这意味着在一个时间点只有一个应用程序可以拥有音频焦点,如果它要求,您应该放弃。

可以通过使用 OnAudioFocusChangeListener 获得此行为。 .

基本上,您必须:

  • 在开始播放之前请求音频焦点。
  • 仅在您有效获取后才开始播放。
  • 停止播放时放弃焦点。
  • 处理音频焦点丢失(通过降低音量或完全停止播放)。

请检查Managing Audio Focus Android 文档中的文章。

关于android - 如何在 Android 来电时停止音频播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24216966/

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