Example usage for android.media AudioManager STREAM_MUSIC

List of usage examples for android.media AudioManager STREAM_MUSIC

Introduction

In this page you can find the example usage for android.media AudioManager STREAM_MUSIC.

Prototype

int STREAM_MUSIC

To view the source code for android.media AudioManager STREAM_MUSIC.

Click Source Link

Document

Used to identify the volume of audio streams for music playback

Usage

From source file:org.botlibre.sdk.activity.ChatActivity.java

private void muteMicBeep(boolean mute) {
    debug("muteMicBeep:" + mute + ":" + MainActivity.volume);
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    if (mute) {/*from  w w w .  ja v  a2s.  com*/
        //if its true then the Volume will be zero.
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
    } else {
        //if its false, the Volume will put back on
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, MainActivity.volume, 0);
    }
}

From source file:com.kaichaohulian.baocms.ecdemo.ui.chatting.ChattingFragment.java

private void initToneGenerator() {
    AudioManager mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
    if (mToneGenerator == null) {
        try {/*from   www. j  a  v  a  2  s. c om*/
            int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            int streamMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            int volume = (int) (TONE_RELATIVE_VOLUME * (streamVolume / streamMaxVolume));
            mToneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, volume);

        } catch (RuntimeException e) {
            LogUtil.d("Exception caught while creating local tone generator: " + e);
            mToneGenerator = null;
        }
    }
}

From source file:org.botlibre.sdk.activity.ChatActivity.java

private void setStreamVolume() {
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    if (volume != 0) {
        debug("setStreamVolume:" + volume);
        Log.d("ChatActivity", "The volume changed and saved to : " + volume);
        MainActivity.volume = volume;//from  w ww. j av  a2  s  . c om
    }
}

From source file:com.firefly.sample.castcompanionlibrary.cast.VideoCastManager.java

@SuppressLint("InlinedApi")
private void setUpRemoteControl(final MediaInfo info) {
    if (!isFeatureEnabled(BaseCastManager.FEATURE_LOCKSCREEN)) {
        return;//from   w ww.ja  v  a2s  .c o  m
    }
    LOGD(TAG, "setupRemoteControl() was called");
    mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

    ComponentName eventReceiver = new ComponentName(mContext, VideoIntentReceiver.class.getName());
    mAudioManager.registerMediaButtonEventReceiver(eventReceiver);

    if (mRemoteControlClientCompat == null) {
        Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        intent.setComponent(mMediaButtonReceiverComponent);
        mRemoteControlClientCompat = new RemoteControlClientCompat(
                PendingIntent.getBroadcast(mContext, 0, intent, 0));
        RemoteControlHelper.registerRemoteControlClient(mAudioManager, mRemoteControlClientCompat);
    }
    mRemoteControlClientCompat.addToMediaRouter(mMediaRouter);
    mRemoteControlClientCompat.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE);
    if (null == info) {
        mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
        return;
    } else {
        mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    }

    // Update the remote control's image
    updateLockScreenImage(info);

    // update the remote control's metadata
    updateLockScreenMetadata();
}

From source file:com.naman14.timber.musicplayer.MusicService.java

public void play(boolean createNewNextTrack) {
    int status = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    if (D) {/*from  w w  w  . j  a v  a 2  s . c o m*/
        Log.d(TAG, "Starting playback: audio focus request status = " + status);
    }

    if (status != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        return;
    }

    final Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
    intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId());
    intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
    sendBroadcast(intent);

    mAudioManager.registerMediaButtonEventReceiver(
            new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName()));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        mSession.setActive(true);

    if (createNewNextTrack) {
        setNextTrack();
    } else {
        setNextTrack(mNextPlayPos);
    }

    if (mPlayer.isInitialized()) {
        final long duration = mPlayer.duration();
        if (mRepeatMode != REPEAT_CURRENT && duration > 2000 && mPlayer.position() >= duration - 2000) {
            gotoNext(true);
        }

        mPlayer.start();
        mPlayerHandler.removeMessages(FADEDOWN);
        mPlayerHandler.sendEmptyMessage(FADEUP);

        setIsSupposedToBePlaying(true, true);

        cancelShutdown();
        updateNotification();
        notifyChange(META_CHANGED);
    } else if (mPlaylist.size() <= 0) {
        setShuffleMode(SHUFFLE_AUTO);
    }
}

From source file:air.com.snagfilms.cast.chromecast.VideoChromeCastManager.java

@SuppressLint("InlinedApi")
private void setUpRemoteControl(final MediaInfo info) {
    if (!isFeatureEnabled(BaseChromeCastManager.FEATURE_LOCKSCREEN)) {
        return;/*from  ww  w .ja v a  2  s  .c  o  m*/
    }
    Log.d(TAG, "setupRemoteControl() was called");
    mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

    ComponentName eventReceiver = new ComponentName(mContext, VideoIntentReceiver.class.getName());
    mAudioManager.registerMediaButtonEventReceiver(eventReceiver);

    if (mRemoteControlClientCompat == null) {
        Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        intent.setComponent(mMediaButtonReceiverComponent);
        mRemoteControlClientCompat = new RemoteControlClientCompat(
                PendingIntent.getBroadcast(mContext, 0, intent, 0));
        RemoteControlHelper.registerRemoteControlClient(mAudioManager, mRemoteControlClientCompat);
    }
    mRemoteControlClientCompat.addToMediaRouter(mMediaRouter);
    mRemoteControlClientCompat.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE);
    if (null == info) {
        mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
        return;
    } else {
        mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    }

    // Update the remote control's image
    updateLockScreenImage(info);

    // update the remote control's metadata
    updateLockScreenMetadata();
}

From source file:com.bluros.music.MusicService.java

public void play(boolean createNewNextTrack) {
    int status = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    if (D)// w w  w. j  a  v a  2s  .  c o  m
        Log.d(TAG, "Starting playback: audio focus request status = " + status);

    if (status != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        return;
    }

    final Intent intent = new Intent(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION);
    intent.putExtra(AudioEffect.EXTRA_AUDIO_SESSION, getAudioSessionId());
    intent.putExtra(AudioEffect.EXTRA_PACKAGE_NAME, getPackageName());
    sendBroadcast(intent);

    mAudioManager.registerMediaButtonEventReceiver(
            new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName()));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        mSession.setActive(true);

    if (createNewNextTrack) {
        setNextTrack();
    } else {
        setNextTrack(mNextPlayPos);
    }

    if (mPlayer.isInitialized()) {
        final long duration = mPlayer.duration();
        if (mRepeatMode != REPEAT_CURRENT && duration > 2000 && mPlayer.position() >= duration - 2000) {
            gotoNext(true);
        }

        mPlayer.start();
        mPlayerHandler.removeMessages(FADEDOWN);
        mPlayerHandler.sendEmptyMessage(FADEUP);

        setIsSupposedToBePlaying(true, true);

        cancelShutdown();
        updateNotification();
        notifyChange(META_CHANGED);
    } else if (mPlaylist.size() <= 0) {
        setShuffleMode(SHUFFLE_AUTO);
    }
}

From source file:com.shinymayhem.radiopresets.ServiceRadioPlayer.java

public static boolean validateUrl(String url) {

    if (!URLUtil.isHttpUrl(url) && !URLUtil.isHttpsUrl(url)) {
        //if (LOCAL_LOGV) log("not a valid http or https url", "v");
        return false;
    }// w  w w .java 2 s .  c  o  m
    //check for empty after prefix
    if (url.equals("http://") || url.equals("https://")) {
        return false;
    }

    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(url);
        mediaPlayer.release();
    } catch (IOException e) {
        return false;
    }
    return true;
}

From source file:com.youku.player.base.YoukuBasePlayerActivity.java

private void initSound() {
    am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    currentSound = am.getStreamVolume(AudioManager.STREAM_MUSIC);
}

From source file:com.youku.player.base.YoukuBasePlayerActivity.java

private boolean volumeUp() {
    if (!isMute) {
        am.setStreamVolume(AudioManager.STREAM_MUSIC,
                Math.min(am.getStreamVolume(AudioManager.STREAM_MUSIC) + 1,
                        am.getStreamMaxVolume(AudioManager.STREAM_MUSIC)),
                0);//w ww .j  ava2  s.  c o m
    } else {
        if (currentSound >= 0) {
            am.setStreamVolume(AudioManager.STREAM_MUSIC,
                    Math.min(currentSound + 1, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC)), 0);
        }

    }
    pluginManager.onVolumnUp();
    return false;
}