如何在Android中拨打电话时停止音频播放

时间:2014-06-14 05:44:55

标签: android service android-mediaplayer android-audiomanager

你好我刚接触到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());}

 }
堰的事情是,从我可以开始和停止服务的活动就好了,但在这里我只能阻止它。所以现在我使用通知回到活动并重新启动服务

2 个答案:

答案 0 :(得分:1)

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

  

我需要播放服务才能在手机获取时停止并重新启动   调用

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

使用OnAudioFocusChangeListener可以获得此行为。

基本上,你必须:

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

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

答案 1 :(得分:0)

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

IntentFilter receiverFilter = new IntentFilter(
        Intent.ACTION_HEADSET_PLUG);
BootBroadcast receiver = new BootBroadcast();
registerReceiver(receiver, receiverFilter);

然后同样需要在接收器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>

在BootBroadcast类

中添加此标志
newIntent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);