Adds the Random Music Player sample.

This sample illustrates how to write an application that plays media
in the background, handles streaming media from the network and
reacts appropriately to system events like headphone disconnection and
audio focus changes, and also uses the notification system to
keep the user informed about what is happening.

This CL is a cherrypick of the old CL 104495 (originally in
honeycomb-mr1) into honeycomb-mr2. Original CL:
https://android-git.corp.google.com/g/#change,104495

Change-Id: Ib8b15ee920300adc093a8b52c17bd9c251ac74ca
This commit is contained in:
Bruno Oliveira
2011-06-07 01:09:21 -04:00
parent 193d98ec18
commit b30a3010f5
58 changed files with 1461 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.musicplayer;
import android.content.Context;
import android.media.AudioManager;
/**
* Convenience class to deal with audio focus. This class deals with everything related to audio
* focus: it can request and abandon focus, and will intercept focus change events and deliver
* them to a MusicFocusable interface (which, in our case, is implemented by {@link MusicService}).
*
* This class can only be used on SDK level 8 and above, since it uses API features that are not
* available on previous SDK's.
*/
public class AudioFocusHelper implements AudioManager.OnAudioFocusChangeListener {
AudioManager mAM;
MusicFocusable mFocusable;
public AudioFocusHelper(Context ctx, MusicFocusable focusable) {
mAM = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
mFocusable = focusable;
}
/** Requests audio focus. Returns whether request was successful or not. */
public boolean requestFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
mAM.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
/** Abandons audio focus. Returns whether request was successful or not. */
public boolean abandonFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAM.abandonAudioFocus(this);
}
/**
* Called by AudioManager on audio focus changes. We implement this by calling our
* MusicFocusable appropriately to relay the message.
*/
@Override
public void onAudioFocusChange(int focusChange) {
if (mFocusable == null) return;
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
mFocusable.onGainedAudioFocus();
break;
case AudioManager.AUDIOFOCUS_LOSS:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
mFocusable.onLostAudioFocus(false);
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
mFocusable.onLostAudioFocus(true);
break;
default:
}
}
}