Example usage for android.content Context AUDIO_SERVICE

List of usage examples for android.content Context AUDIO_SERVICE

Introduction

In this page you can find the example usage for android.content Context AUDIO_SERVICE.

Prototype

String AUDIO_SERVICE

To view the source code for android.content Context AUDIO_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.media.AudioManager for handling management of volume, ringer modes and audio routing.

Usage

From source file:com.googlecode.mindbell.accessors.ContextAccessor.java

public int getAlarmVolume() {
    AudioManager audioMan = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    return audioMan.getStreamVolume(AudioManager.STREAM_ALARM);
}

From source file:com.jungle.base.utils.MiscUtils.java

public static void playSound(Context context, int soundResId) {
    final MediaPlayer player = MediaPlayer.create(context, soundResId);
    if (player == null) {
        return;//from ww  w .j  a  v  a2s.  c  om
    }

    AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int currVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);

    float volume = 1.0f;
    if (maxVolume > 0) {
        volume = (float) currVolume / (float) maxVolume;
    }

    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            player.release();
        }
    });

    player.setVolume(volume, volume);
    player.start();
}

From source file:com.andrew.apollo.MusicPlaybackService.java

private void initService() {
    // Initialize the favorites and recents databases
    mFavoritesCache = FavoritesStore.getInstance(this);
    mRecentsCache = RecentStore.getInstance(this);

    // Initialize the notification helper
    mNotificationHelper = new NotificationHelper(this);

    // Initialize the image fetcher
    mImageFetcher = ImageFetcher.getInstance(this);
    // Initialize the image cache
    mImageFetcher.setImageCache(ImageCache.getInstance(this));

    // Start up the thread running the service. Note that we create a
    // separate thread because the service normally runs in the process's
    // main thread, which we don't want to block. We also make it
    // background priority so CPU-intensive work will not disrupt the UI.
    final HandlerThread thread = new HandlerThread("MusicPlayerHandler",
            android.os.Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();// w  w w.j  a va2 s .c  om

    // Initialize the handler
    mPlayerHandler = new MusicPlayerHandler(this, thread.getLooper());

    // Initialize the audio manager and register any headset controls for
    // playback
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mMediaButtonReceiverComponent = new ComponentName(getPackageName(),
            MediaButtonIntentReceiver.class.getName());
    try {
        if (mAudioManager != null) {
            mAudioManager.registerMediaButtonEventReceiver(mMediaButtonReceiverComponent);
        }
    } catch (SecurityException e) {
        e.printStackTrace();
        // ignore
        // some times the phone does not grant the MODIFY_PHONE_STATE permission
        // this permission is for OMEs and we can't do anything about it
    }

    // Use the remote control APIs to set the playback state
    setUpRemoteControlClient();

    // Initialize the preferences
    mPreferences = getSharedPreferences("Service", 0);
    mCardId = getCardId();

    registerExternalStorageListener();

    // Initialize the media player
    mPlayer = new MultiPlayer(this);
    mPlayer.setHandler(mPlayerHandler);

    ConfigurationManager CM = ConfigurationManager.instance();
    // Load Repeat Mode
    setRepeatMode(CM.getInt(Constants.PREF_KEY_GUI_PLAYER_REPEAT_MODE));
    // Load Shuffle Mode On/Off
    enableShuffle(CM.getBoolean(Constants.PREF_KEY_GUI_PLAYER_SHUFFLE_ENABLED));
    MusicUtils.isShuffleEnabled();

    // Initialize the intent filter and each action
    final IntentFilter filter = new IntentFilter();
    filter.addAction(SERVICECMD);
    filter.addAction(TOGGLEPAUSE_ACTION);
    filter.addAction(PAUSE_ACTION);
    filter.addAction(STOP_ACTION);
    filter.addAction(NEXT_ACTION);
    filter.addAction(PREVIOUS_ACTION);
    filter.addAction(REPEAT_ACTION);
    filter.addAction(SHUFFLE_ACTION);
    // Attach the broadcast listener
    registerReceiver(mIntentReceiver, filter);

    // Initialize the wake lock
    final PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    if (powerManager != null) {
        mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
        mWakeLock.setReferenceCounted(false);
    }
    // Initialize the delayed shutdown intent
    final Intent shutdownIntent = new Intent(this, MusicPlaybackService.class);
    shutdownIntent.setAction(SHUTDOWN_ACTION);

    mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    mShutdownIntent = PendingIntent.getService(this, 0, shutdownIntent, 0);

    // Listen for the idle state
    scheduleDelayedShutdown();

    // Bring the queue back
    reloadQueue();
    notifyChange(QUEUE_CHANGED);
    notifyChange(META_CHANGED);
    updateNotification();
}

From source file:com.googlecode.mindbell.accessors.ContextAccessor.java

public void setAlarmVolume(int volume) {
    AudioManager audioMan = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    audioMan.setStreamVolume(AudioManager.STREAM_ALARM, volume, 0);

}

From source file:com.hotstar.player.adplayer.player.PlayerFragment.java

private void abandonAudioFocus() {
    AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
    am.abandonAudioFocus(null);//from  w ww .  j a v  a2  s  .  com

    AdVideoApplication.logger.i(LOG_TAG + "#abandonAudioFocus()", "Abandoned audio focus.");
}

From source file:edu.missouri.bas.service.SensorService.java

public void playSound(int sound, float fSpeed) {
    AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
    float volume = streamVolumeCurrent;
    mSoundPool.play(soundsMap.get(sound), volume, volume, 1, 0, fSpeed);

}

From source file:singh.amandeep.musicplayer.MusicService.java

/**
 * {@inheritDoc}/*from  w w w .ja v a 2  s . com*/
 */
@Override
public void onCreate() {
    if (D)
        Log.d(TAG, "Creating service");
    super.onCreate();

    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Initialize the favorites and recents databases
    mRecentsCache = RecentStore.getInstance(this);

    // gets the song play count cache
    mSongPlayCountCache = SongPlayCount.getInstance(this);

    // gets a pointer to the playback state store
    mPlaybackStateStore = MusicPlaybackState.getInstance(this);

    /*// Initialize the image fetcher
    mImageFetcher = ImageFetcher.getInstance(this);
    // Initialize the image cache
    mImageFetcher.setImageCache(ImageCache.getInstance(this));*/

    // Start up the thread running the service. Note that we create a
    // separate thread because the service normally runs in the process's
    // main thread, which we don't want to block. We also make it
    // background priority so CPU-intensive work will not disrupt the UI.
    mHandlerThread = new HandlerThread("MusicPlayerHandler", android.os.Process.THREAD_PRIORITY_BACKGROUND);
    mHandlerThread.start();

    // Initialize the handler
    mPlayerHandler = new MusicPlayerHandler(this, mHandlerThread.getLooper());

    // Initialize the audio manager and register any headset controls for
    // playback
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mMediaButtonReceiverComponent = new ComponentName(getPackageName(),
            MediaButtonIntentReceiver.class.getName());
    mAudioManager.registerMediaButtonEventReceiver(mMediaButtonReceiverComponent);

    // Use the remote control APIs to set the playback state
    setUpMediaSession();

    // Initialize the preferences
    mPreferences = getSharedPreferences("Service", 0);
    mCardId = getCardId();

    registerExternalStorageListener();

    // Initialize the media player
    mPlayer = new MultiPlayer(this);
    mPlayer.setHandler(mPlayerHandler);

    // Initialize the intent filter and each action
    final IntentFilter filter = new IntentFilter();
    filter.addAction(SERVICECMD);
    filter.addAction(TOGGLEPAUSE_ACTION);
    filter.addAction(PAUSE_ACTION);
    filter.addAction(STOP_ACTION);
    filter.addAction(NEXT_ACTION);
    filter.addAction(PREVIOUS_ACTION);
    filter.addAction(PREVIOUS_FORCE_ACTION);
    filter.addAction(REPEAT_ACTION);
    filter.addAction(SHUFFLE_ACTION);
    // Attach the broadcast listener
    registerReceiver(mIntentReceiver, filter);

    // Get events when MediaStore content changes
    mMediaStoreObserver = new MediaStoreObserver(mPlayerHandler);
    getContentResolver().registerContentObserver(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, true,
            mMediaStoreObserver);
    getContentResolver().registerContentObserver(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, true,
            mMediaStoreObserver);

    // Initialize the wake lock
    final PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    mWakeLock.setReferenceCounted(false);

    // Initialize the delayed shutdown intent
    final Intent shutdownIntent = new Intent(this, MusicService.class);
    shutdownIntent.setAction(SHUTDOWN);

    mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    mShutdownIntent = PendingIntent.getService(this, 0, shutdownIntent, 0);

    // Listen for the idle state
    scheduleDelayedShutdown();

    // Bring the queue back
    reloadQueue();
    notifyChange(QUEUE_CHANGED);
    notifyChange(META_CHANGED);
}

From source file:com.evandroid.musica.MainLyricActivity.java

@SuppressLint("InlinedApi")
public void resync(MenuItem item) {
    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    am.requestAudioFocus(null,//  w  ww . j  ava2 s. c om
            // Use the music stream.
            AudioManager.STREAM_SYSTEM,
            // Request permanent focus.
            AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
    am.abandonAudioFocus(null);
}

From source file:net.sourceforge.kalimbaradio.androidapp.util.Util.java

public static void registerMediaButtonEventReceiver(Context context) {

    // Only do it if enabled in the settings.
    SharedPreferences prefs = getPreferences(context);
    boolean enabled = prefs.getBoolean(Constants.PREFERENCES_KEY_MEDIA_BUTTONS, true);

    if (enabled) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        ComponentName mediaButtonIntentReceiver = new ComponentName(context.getPackageName(),
                MediaButtonIntentReceiver.class.getName());
        audioManager.registerMediaButtonEventReceiver(mediaButtonIntentReceiver);
    }//  w ww .ja v a 2  s  .com
}

From source file:com.kaku.weac.fragment.AlarmClockOntimeFragment.java

/**
 * /*from w  w w .  ja v a  2  s  .c o m*/
 */
private void playRing() {
    mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);

    mCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    // ?
    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAlarmClock.getVolume(), AudioManager.ADJUST_SAME);

    // 
    if (mAlarmClock.getRingUrl().equals(WeacConstants.DEFAULT_RING_URL)
            || TextUtils.isEmpty(mAlarmClock.getRingUrl())) {
        // ?
        if (mAlarmClock.isVibrate()) {
            // 
            AudioPlayer.getInstance(getActivity()).playRaw(R.raw.ring_weac_alarm_clock_default, true, true);
        } else {
            AudioPlayer.getInstance(getActivity()).playRaw(R.raw.ring_weac_alarm_clock_default, true, false);
        }

        // 
    } else if (mAlarmClock.getRingUrl().equals(WeacConstants.NO_RING_URL)) {
        // ?
        if (mAlarmClock.isVibrate()) {
            AudioPlayer.getInstance(getActivity()).stop();
            AudioPlayer.getInstance(getActivity()).vibrate();
        } else {
            AudioPlayer.getInstance(getActivity()).stop();
        }
    } else {
        // ?
        if (mAlarmClock.isVibrate()) {
            AudioPlayer.getInstance(getActivity()).play(mAlarmClock.getRingUrl(), true, true);
        } else {
            AudioPlayer.getInstance(getActivity()).play(mAlarmClock.getRingUrl(), true, false);
        }
    }
}