Home | History | Annotate | Download | only in musicplayer
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.musicplayer;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.util.Log;
     23 import android.view.KeyEvent;
     24 import android.widget.Toast;
     25 
     26 /**
     27  * Receives broadcasted intents. In particular, we are interested in the
     28  * android.media.AUDIO_BECOMING_NOISY and android.intent.action.MEDIA_BUTTON intents, which is
     29  * broadcast, for example, when the user disconnects the headphones. This class works because we are
     30  * declaring it in a <receiver> tag in AndroidManifest.xml.
     31  */
     32 public class MusicIntentReceiver extends BroadcastReceiver {
     33     @Override
     34     public void onReceive(Context context, Intent intent) {
     35         if (intent.getAction().equals(android.media.AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
     36             Toast.makeText(context, "Headphones disconnected.", Toast.LENGTH_SHORT).show();
     37 
     38             // send an intent to our MusicService to telling it to pause the audio
     39             context.startService(new Intent(MusicService.ACTION_PAUSE));
     40 
     41         } else if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) {
     42             KeyEvent keyEvent = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT);
     43             if (keyEvent.getAction() != KeyEvent.ACTION_DOWN)
     44                 return;
     45 
     46             switch (keyEvent.getKeyCode()) {
     47                 case KeyEvent.KEYCODE_HEADSETHOOK:
     48                 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
     49                     context.startService(new Intent(MusicService.ACTION_TOGGLE_PLAYBACK));
     50                     break;
     51                 case KeyEvent.KEYCODE_MEDIA_PLAY:
     52                     context.startService(new Intent(MusicService.ACTION_PLAY));
     53                     break;
     54                 case KeyEvent.KEYCODE_MEDIA_PAUSE:
     55                     context.startService(new Intent(MusicService.ACTION_PAUSE));
     56                     break;
     57                 case KeyEvent.KEYCODE_MEDIA_STOP:
     58                     context.startService(new Intent(MusicService.ACTION_STOP));
     59                     break;
     60                 case KeyEvent.KEYCODE_MEDIA_NEXT:
     61                     context.startService(new Intent(MusicService.ACTION_SKIP));
     62                     break;
     63                 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
     64                     // TODO: ensure that doing this in rapid succession actually plays the
     65                     // previous song
     66                     context.startService(new Intent(MusicService.ACTION_REWIND));
     67                     break;
     68             }
     69         }
     70     }
     71 }
     72