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.connectbot.ConsoleActivity.java

@Override
public void onOptionsMenuClosed(Menu menu) {
    super.onOptionsMenuClosed(menu);

    setVolumeControlStream(AudioManager.STREAM_MUSIC);
}

From source file:mp.teardrop.PlaybackService.java

private void processNewState(int oldState, int state) {
    int toggled = oldState ^ state;

    if ((toggled & FLAG_PLAYING) != 0) {
        if ((state & FLAG_PLAYING) != 0) {
            if (mMediaPlayerInitialized)
                mMediaPlayer.start();/*w  w  w .j a v a 2s . c  o  m*/

            if (mNotificationMode != NEVER)
                startForeground(NOTIFICATION_ID, createNotification(mCurrentSong, mState));

            mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

            mHandler.removeMessages(RELEASE_WAKE_LOCK);
            try {
                if (mWakeLock != null && mWakeLock.isHeld() == false)
                    mWakeLock.acquire();
            } catch (SecurityException e) {
                // Don't have WAKE_LOCK permission
            }
        } else {
            if (mMediaPlayerInitialized)
                mMediaPlayer.pause();

            if (mNotificationMode == ALWAYS || mForceNotificationVisible) {
                stopForeground(false);
                mNotificationManager.notify(NOTIFICATION_ID, createNotification(mCurrentSong, mState));
            } else {
                stopForeground(true);
            }

            // Delay release of the wake lock. This allows the headset
            // button to continue to function for a short period after
            // pausing.
            mHandler.sendEmptyMessageDelayed(RELEASE_WAKE_LOCK, WAKE_LOCK_DELAY);
        }

        setupSensor();
    }

    if ((toggled & FLAG_NO_MEDIA) != 0 && (state & FLAG_NO_MEDIA) != 0) {
        Song song = mCurrentSong;
        if (song != null && mMediaPlayerInitialized) {
            mPendingSeek = mMediaPlayer.getCurrentPosition();
            mPendingSeekSong = song.id;
        }
    }

    if ((toggled & MASK_SHUFFLE) != 0)
        mTimeline.setShuffleMode(shuffleMode(state));
    if ((toggled & MASK_FINISH) != 0)
        mTimeline.setFinishAction(finishAction(state));

    triggerGaplessUpdate();
    triggerReadAhead();
}

From source file:com.mobilis.android.nfc.activities.MagTekFragment.java

private void maxVolume() {
    mAudioMgr.setStreamVolume(AudioManager.STREAM_MUSIC,
            mAudioMgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_SHOW_UI);

}

From source file:com.mobilis.android.nfc.activities.MagTekFragment.java

private void minVolume() {
    mAudioMgr.setStreamVolume(AudioManager.STREAM_MUSIC, mIntCurrentVolume, AudioManager.FLAG_SHOW_UI);

}

From source file:io.puzzlebox.orbit.ui.OrbitFragment.java

public void playControl() {

    Log.d(TAG, "playControl()");

    // TODO Convert to service

    //      FragmentTabAdvanced fragmentAdvanced =
    //              (FragmentTabAdvanced) getActivity().getSupportFragmentManager().findFragmentByTag( getTabFragmentAdvanced() );
    ///*from  w w w .  j a v a2s .c  om*/
    if (OrbitSingleton.getInstance().generateAudio) {

        /**
         * Generate signal on the fly
         */

        //         // Handle controlled descent thread if activated
        //         if ((fragmentAdvanced.orbitControlledDescentTask != null) &&
        //                 (fragmentAdvanced.orbitControlledDescentTask.keepDescending)) {
        //            fragmentAdvanced.orbitControlledDescentTask.callStopAudio = false;
        //            fragmentAdvanced.orbitControlledDescentTask.keepDescending = false;
        //         }

        //         if (audioHandler != null) {

        //            serviceBinder.ifFlip = fragmentAdvanced.checkBoxInvertControlSignal.isChecked(); // if checked then flip
        OrbitSingleton.getInstance().audioHandler.ifFlip = OrbitSingleton.getInstance().invertControlSignal; // if checked then flip

        int channel = 0; // default "A"

        //         if (fragmentAdvanced != null)
        //            channel = fragmentAdvanced.radioGroupChannel.getCheckedRadioButtonId();

        //            if (demoFlightMode)
        //               updateAudioHandlerLoopNumberWhileMindControl(200);
        //            else
        //               updateAudioHandlerLoopNumberWhileMindControl(4500);
        //
        //         updateAudioHandlerLoopNumberWhileMindControl(5000);

        updateAudioHandlerLoopNumberWhileMindControl(-1); // Loop infinite for easier user testing

        updateAudioHandlerChannel(channel);

        OrbitSingleton.getInstance().audioHandler.mutexNotify();

        //         }
        //
        //
    } else {

        /**
         * Play audio control file
         */

        /** Getting the user sound settings */
        AudioManager audioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
        //         float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        //         float volume = actualVolume / maxVolume;

        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int) maxVolume, 0);
        /** Is the sound loaded already? */
        if (OrbitSingleton.getInstance().loaded) {
            //            soundPool.play(soundID, volume, volume, 1, 0, 1f);
            //            soundPool.setVolume(soundID, 1f, 1f);
            //            soundPool.play(soundID, maxVolume, maxVolume, 1, 0, 1f); // Fixes Samsung Galaxy S4 [SGH-M919]

            OrbitSingleton.getInstance().soundPool.play(OrbitSingleton.getInstance().soundID, 1f, 1f, 1, 0, 1f); // Fixes Samsung Galaxy S4 [SGH-M919]

            // TODO No visible effects of changing these variables on digital oscilloscope
            //            soundPool.play(soundID, 0.5f, 0.5f, 1, 0, 0.5f);
            //            if (DEBUG)
            Log.v(TAG, "Played sound");
        }

    }

}

From source file:net.sourceforge.servestream.activity.MediaPlayerActivity.java

private synchronized void updateVolumeBar() {
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    if (mVolume != null) {
        mVolume.setProgress(curVolume);//ww w .j a v  a 2s  .c o m
        mVolume.setMax(maxVolume);
    }
}

From source file:androidx.media.widget.VideoView2.java

@SuppressWarnings("deprecation")
private void requestAudioFocus(int focusType) {
    int result;//ww  w  .  java 2 s .c  om
    if (android.os.Build.VERSION.SDK_INT >= 26) {
        AudioFocusRequest focusRequest;
        focusRequest = new AudioFocusRequest.Builder(focusType).setAudioAttributes(mAudioAttributes)
                .setOnAudioFocusChangeListener(mAudioFocusListener).build();
        result = mAudioManager.requestAudioFocus(focusRequest);
    } else {
        result = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, focusType);
    }
    if (result == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
        mAudioFocused = false;
    } else if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        mAudioFocused = true;
    } else if (result == AudioManager.AUDIOFOCUS_REQUEST_DELAYED) {
        mAudioFocused = false;
    }
}

From source file:com.Beat.RingdroidEditActivity.java

private synchronized void onPlay(int startPosition) {
    if (mIsPlaying) {
        handlePause();/*from   w w w  .ja va 2s.  co m*/
        return;
    }

    if (mPlayer == null) {
        // Not initialized yet
        return;
    }

    try {
        mPlayStartMsec = mWaveformView.pixelsToMillisecs(startPosition);
        if (startPosition < mStartPos) {
            mPlayEndMsec = mWaveformView.pixelsToMillisecs(mStartPos);
        } else if (startPosition > mEndPos) {
            mPlayEndMsec = mWaveformView.pixelsToMillisecs(mMaxPos);
        } else {
            mPlayEndMsec = mWaveformView.pixelsToMillisecs(mEndPos);
        }

        mPlayStartOffset = 0;

        int startFrame = mWaveformView.secondsToFrames(mPlayStartMsec * 0.001);
        int endFrame = mWaveformView.secondsToFrames(mPlayEndMsec * 0.001);
        int startByte = mSoundFile.getSeekableFrameOffset(startFrame);
        int endByte = mSoundFile.getSeekableFrameOffset(endFrame);
        if (mCanSeekAccurately && startByte >= 0 && endByte >= 0) {
            try {
                mPlayer.reset();
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                FileInputStream subsetInputStream = new FileInputStream(mFile.getAbsolutePath());
                mPlayer.setDataSource(subsetInputStream.getFD(), startByte, endByte - startByte);
                mPlayer.prepare();
                mPlayStartOffset = mPlayStartMsec;
            } catch (Exception e) {
                System.out.println("Exception trying to play file subset");
                mPlayer.reset();
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mPlayer.setDataSource(mFile.getAbsolutePath());
                mPlayer.prepare();
                mPlayStartOffset = 0;
            }
        }

        mPlayer.setOnCompletionListener(new OnCompletionListener() {
            public synchronized void onCompletion(MediaPlayer arg0) {
                handlePause();
            }
        });
        mIsPlaying = true;

        if (mPlayStartOffset == 0) {
            mPlayer.seekTo(mPlayStartMsec);
        }
        mPlayer.start();
        updateDisplay();
        enableDisableButtons();
    } catch (Exception e) {
        showFinalAlert(e, R.string.play_error);
        return;
    }
}

From source file:org.drrickorang.loopback.LoopbackActivity.java

/**
 * Refresh the sound level bar on the main activity to reflect the current sound level
 * of the system./*  w ww  . j a v a 2 s . c  o  m*/
 */
private void refreshSoundLevelBar() {
    mBarMasterLevel.setEnabled(true);
    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
    mBarMasterLevel.setProgress(currentVolume);
}

From source file:com.thejoshwa.ultrasonic.androidapp.util.Util.java

public static void requestAudioFocus(final Context context) {
    if (!hasFocus) {
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        hasFocus = true;//from w  ww  .j av  a  2s. c o  m
        audioManager.requestAudioFocus(new OnAudioFocusChangeListener() {
            public void onAudioFocusChange(int focusChange) {
                DownloadServiceImpl downloadService = (DownloadServiceImpl) context;
                if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
                        || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
                        && !downloadService.isJukeboxEnabled()) {
                    if (downloadService.getPlayerState() == PlayerState.STARTED) {
                        SharedPreferences prefs = getPreferences(context);
                        int lossPref = Integer
                                .parseInt(prefs.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1"));
                        if (lossPref == 2 || (lossPref == 1
                                && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) {
                            lowerFocus = true;
                            downloadService.setVolume(0.1f);
                        } else if (lossPref == 0
                                || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) {
                            pauseFocus = true;
                            downloadService.pause();
                        }
                    }
                } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                    if (pauseFocus) {
                        pauseFocus = false;
                        downloadService.start();
                    } else if (lowerFocus) {
                        lowerFocus = false;
                        downloadService.setVolume(1.0f);
                    }
                } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isJukeboxEnabled()) {
                    hasFocus = false;
                    downloadService.pause();
                    audioManager.abandonAudioFocus(this);
                }
            }
        }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    }
}