Android 在安卓系统中,当电话到达时,如何停止音频播放

Android 在安卓系统中,当电话到达时,如何停止音频播放,android,service,android-mediaplayer,android-audiomanager,Android,Service,Android Mediaplayer,Android Audiomanager,你好,我是android新手,我正在为我的教堂做一个简单的在线广播应用。问题是,当手机接到电话并结束通话时,我需要播放服务停止并重新启动 到目前为止,我可以停止服务只是罚款,但当它再次启动只是崩溃,所以我不知道我做错了什么 import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; imp

你好,我是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());}

 }

问题是,从活动中,我可以很好地启动和停止服务,但在这里,我只能停止服务。因此,现在我使用通知返回活动并重新启动服务

您需要在当前活动中添加此片段

IntentFilter receiverFilter = new IntentFilter(
        Intent.ACTION_HEADSET_PLUG);
BootBroadcast receiver = new BootBroadcast();
registerReceiver(receiver, receiverFilter);
然后同样需要在receiver manifest.xml中注册

例如:

<receiver
            android:name="YOUR_ACTION_STRING.BootCompletedReceiver"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

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

我需要播放服务停止和重新启动时,手机得到一个声音 召唤

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

此行为可以通过使用

基本上,你必须:

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

请查看Android文档中的文章。

我认为您需要在TelephonyManager.CALL\u STATE\u IDLE case中添加启动服务的代码,谢谢!这正是我所需要的它能工作flawlessly@user3739754我很高兴!如果你认为答案有用,别忘了接受它:)
newIntent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);