简体   繁体   English

Android:在通话期间停止小部件的MediaPlayer活动

[英]Android: Stopping MediaPlayer activity of widget during Phone Calls

I have used the solutions provided in this answer Stopping & Starting music on incoming calls but can't figure out how to implement it in my code. 我已经使用了此答案中提供的解决方案在来电时停止和启动音乐,但无法弄清楚如何在我的代码中实现它。 This is what I did - 这就是我所做的-

public class KameWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
MediaPlayer mPlay;
Context mContext;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.main);

    Intent active = new Intent(context, KameWidget.class);
    active.setAction(ACTION_WIDGET_RECEIVER);
    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,
            0, active, 0);
    remoteViews.setOnClickPendingIntent(R.id.IBWidget, actionPendingIntent);
    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}

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

    final String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
        final int appWidgetId = intent.getExtras().getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            this.onDeleted(context, new int[] { appWidgetId });
        }
    } else {
        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {

            mPlay = MediaPlayer.create(context, R.raw.kamehameha);
            mPlay.start();
            PhoneStateListener phoneStateListener = new PhoneStateListener() {
                @Override
                public void onCallStateChanged(int state,
                        String incomingNumber) {
                    if (state == TelephonyManager.CALL_STATE_RINGING
                            || state == TelephonyManager.CALL_STATE_OFFHOOK) {
                        mPlay.stop();
                    }
                    super.onCallStateChanged(state, incomingNumber);
                }
            };
            TelephonyManager mgr = (TelephonyManager) mContext
                    .getSystemService(Context.TELEPHONY_SERVICE);
            if (mgr != null) {
                mgr.listen(phoneStateListener,
                        PhoneStateListener.LISTEN_CALL_STATE);
            }
        } else {
        }
        super.onReceive(context, intent);
    }
  }
}

Here's what I understood: 这是我的理解:

  • You write a media-player app widget 您编写一个媒体播放器应用程序小部件
  • user presses a button to make it start play (something) 用户按下一个按钮使其开始播放(某物)
  • Call comes in => the sound pauses 来电进来=>声音暂停

Here's how I would go on about it: 这是我要继续的方法:

  1. You bind a service ( android.app.Service ) that plays your audio track to a button on your widget. 您将播放音轨的服务( android.app.Service )绑定到小部件上的按钮。
  2. The service starts the actual MediaPlayer task. 该服务将启动实际的MediaPlayer任务。
  3. When a call comes in, the task pauses the MediaPlayer. 来电时,任务将暂停MediaPlayer。
  4. When the call ends, the task may (depends on what you want) resume the MediaPlayer. 通话结束时,任务可以(取决于您想要的)恢复MediaPlayer。

1) Inside KameWidget.onUpdate(..), something like: 1)在KameWidget.onUpdate(..)内部,类似:

Intent intentPlay = new Intent(context, YourPlaySoundService.class);
PendingIntent piPlay = PendingIntent.getService(context, 0, intentPlay, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(), layoutId);
views.setOnClickPendingIntent(R.id.play, piPlay);
...

2) The Service: 2)服务:

public class YourPlaySoundService extends Service {
  ...
  public int onStartCommand (Intent intent, int flags, int startId) {
    // Start the Audio Task
    // Instantiate AudioTask
    AudioTask task = new AudioTask();
    // E.g. get your resource ID from the widget:
    Bundle extras = intent.getExtras();
    int resId = extras.getInt(YOUR_RESOURCE_PARAM_KEY);
    task.execute(resId);

    return super.onStartCommand(intent, flags, startId);
  }
}

3) The Audio Task: 3)音频任务:

public class AudioTask extends AsyncTask<Integer, Void, Void> {
  private MediaPlayer mMediaPlayer;
  // Telephony-Stuff

  public void resume() {
    if(/* check on correct media player state */) {
      mMediaPlayer.start();
    }
  }
  public void pause() {
    if(mMediaPlayer.isPlaying()) {
      mMediaPlayer.pause();
    }
  }
  public void stop() {
    if(mMediaPlayer.isPlaying()) {
      mMediaPlayer.stop();
    }
    mMediaPlayer.release();
    mMediaPlayer = null;
  }

  protected Void doInBackground(Integer ... params) {
    int resId = params[0];
    // get the resources from a context (the service)
    AssetFileDescriptor afd = mResources.openRawResourceFd(resId);
    try {   
      mMediaPlayer.reset();
      mMediaPlayer.setDataSource(
          afd.getFileDescriptor(), 
          afd.getStartOffset(),
          afd.getDeclaredLength()
          );
      mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
          mp.seekTo(0);
          mp.start();
        }
      });
      mMediaPlayer.prepare();
      afd.close();
    }
    catch (Exception e) {
      Log.e("TAG", e.getMessage(), e);
    }
    return null;
  }

  // Copied from your link: http://stackoverflow.com/a/5610996/747906
  PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            mMediaPlayer.pause();
        } else if(state == TelephonyManager.CALL_STATE_IDLE) {
            //Not in call: Play music
        } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
            //A call is dialing, active or on hold
        }
        super.onCallStateChanged(state, incomingNumber);
    }
  };
}

I don't really know how the telephony stuff works. 我真的不知道电话的工作原理。 You'll have to figure it out yourself. 您必须自己弄清楚。

This is my idea. 这是我的主意。 I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM