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:com.cyanogenmod.eleven.MusicPlaybackService.java

/**
 * Resumes or starts playback./*from ww  w .  java2s . co  m*/
 *
 * @param createNewNextTrack True if you want to figure out the next track, false
 *                           if you want to re-use the existing next track (used for going back)
 */
public void play(boolean createNewNextTrack) {
    int status = mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

    if (D)
        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()));
    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();
    } else if (mPlaylist.size() <= 0) {
        setShuffleMode(SHUFFLE_AUTO);
    }
}

From source file:com.plusot.senselib.SenseMain.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    final int action = event.getAction();
    final int keyCode = event.getKeyCode();
    boolean result = false;

    if ((lastAction == action && lastKeyCode == keyCode)) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_CAMERA:
            newInterval();//from  ww w . j a v a  2 s  .  c o m
            break;
        case KeyEvent.KEYCODE_POWER:
            //if (action == KeyEvent.)
            LLog.d(Globals.TAG, CLASSTAG + " Power button pressed");
            break;
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_DOWN) {
                dimScreen(true);
            }
            result = true;
            break;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_DOWN) {
                dimScreen(false);
            }
            result = true;
            break;
        }
    } else {
        AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        switch (keyCode) {
        case KeyEvent.KEYCODE_CAMERA:
            newInterval();
            break;
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (sameActionKeyCode < 2 && lastAction == KeyEvent.ACTION_DOWN && action == KeyEvent.ACTION_UP
                    && lastKeyCode == KeyEvent.KEYCODE_VOLUME_UP) {
                mgr.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE,
                        AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_PLAY_SOUND);
            }
            result = true;
            break;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (sameActionKeyCode < 2 && lastAction == KeyEvent.ACTION_DOWN && action == KeyEvent.ACTION_UP
                    && lastKeyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                mgr.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER,
                        AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_PLAY_SOUND);
            }
            result = true;
            break;
        }
    }
    if (!result) {
        // LLog.d(Globals.TAG, CLASSTAG +
        // ".distpatchKeyEvent: Calling super with " + delta);
        result = super.dispatchKeyEvent(event);
    }
    if (lastAction == action && lastKeyCode == keyCode) {
        sameActionKeyCode++;
    } else
        sameActionKeyCode = 0;

    lastKeyCode = keyCode;
    lastAction = action;
    return result;

}

From source file:com.google.android.libraries.cast.companionlibrary.cast.VideoCastManager.java

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

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

    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 (info == null) {
        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:de.rosche.spectraTelemetry.SpectraTelemetry.java

private void setDefaultPrefs(Map<String, String> DbPrefs) {
    String valueTmp = "";
    mVoltageAlarm = getDouble(mVoltageAlarm, DbPrefs.get("mVoltageAlarm"));
    mUsedCap = getDouble(mUsedCap, DbPrefs.get("mUsedCap"));
    mCurrentAlarm = getDouble(mCurrentAlarm, DbPrefs.get("mCurrentAlarm"));
    mPowerAlarm = getDouble(mPowerAlarm, DbPrefs.get("mPowerAlarm"));

    mSpeedAlarm = getInteger(mSpeedAlarm, DbPrefs.get("mSpeedAlarm"));
    mRpmAlarm = getInteger(mRpmAlarm, DbPrefs.get("mRpmAlarm"));
    mAltitudeAlarm = getInteger(mAltitudeAlarm, DbPrefs.get("mAltitudeAlarm"));
    mFuelAlarm = getInteger(mFuelAlarm, DbPrefs.get("mFuelAlarm"));
    voltage_hysteresis = getInteger(voltage_hysteresis, DbPrefs.get("voltage_hysteresis"));
    current_hysteresis = getInteger(current_hysteresis, DbPrefs.get("current_hysteresis"));
    rpm_hysteresis = getInteger(rpm_hysteresis, DbPrefs.get("rpm_hysteresis"));
    temperature_hysteresis = getInteger(temperature_hysteresis, DbPrefs.get("temperature_hysteresis"));
    power_hysteresis = getInteger(power_hysteresis, DbPrefs.get(""));
    mTemperatureAlarm = getInteger(mTemperatureAlarm, DbPrefs.get("power_hysteresis"));
    speech_volume = getInteger(speech_volume, DbPrefs.get("speech_volume"));
    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int sb2value = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    sb2value = sb2value * speech_volume / 100;
    am.setStreamVolume(AudioManager.STREAM_MUSIC, sb2value, 0);
    vibrate_on = getBoolean(vibrate_on, DbPrefs.get("vibrate_on"));
    voltage_speech_ack = getBoolean(voltage_speech_ack, DbPrefs.get("voltage_speech_ack"));
    current_speech_ack = getBoolean(current_speech_ack, DbPrefs.get("current_speech_ack"));
    height_speech_ack = getBoolean(height_speech_ack, DbPrefs.get("height_speech_ack"));
    used_speech_ack = getBoolean(used_speech_ack, DbPrefs.get("used_speech_ack"));
    speed_speech_ack = getBoolean(speed_speech_ack, DbPrefs.get("speed_speech_ack"));
    rpm_speech_ack = getBoolean(rpm_speech_ack, DbPrefs.get("rpm_speech_ack"));
    temp_speech_ack = getBoolean(temp_speech_ack, DbPrefs.get("temp_speech_ack"));

    voltage_speech_label = DbPrefs.get("voltage_speech_label");
    voltage_speech_unit = DbPrefs.get("voltage_speech_unit");

    height_speech_label = DbPrefs.get("height_speech_label");
    height_speech_unit = DbPrefs.get("height_speech_unit");

    current_speech_label = DbPrefs.get("current_speech_label");
    current_speech_unit = DbPrefs.get("current_speech_unit");

    used_speech_label = DbPrefs.get("used_speech_label");
    used_speech_unit = DbPrefs.get("used_speech_unit");

    speed_speech_label = DbPrefs.get("speed_speech_label");
    speed_speech_unit = DbPrefs.get("speed_speech_unit");

    rpm_speech_label = DbPrefs.get("rpm_speech_label");
    rpm_speech_unit = DbPrefs.get("rpm_speech_unit");

    temp_speech_label = DbPrefs.get("temp_speech_label");
    temp_speech_unit = DbPrefs.get("temp_speech_unit");

    valueTmp = DbPrefs.get("log_file");
    if (valueTmp != "") {
        log_file = valueTmp;//from w  w  w  .  j  a va2 s  . c  om
    }

    valueTmp = DbPrefs.get("bluetooth_device");
    if (valueTmp != "") {
        bluetooth_device = valueTmp;
    }
    mSound_on = getBoolean(mSound_on, DbPrefs.get("mSound_on"));
    speech_on = getBoolean(speech_on, DbPrefs.get("speech_on"));
    speech_intervall = DbPrefs.get("speech_intervall");
    mAlarm_on = getBoolean(mAlarm_on, DbPrefs.get("mAlarm_on"));
    mlog_file = getBoolean(mlog_file, DbPrefs.get("mlog_file"));
    alert_tone = DbPrefs.get("alert_tone");
    label_rpm1 = DbPrefs.get("label_rpm1");
    label_rpm2 = DbPrefs.get("label_rpm2");
    label_temp1 = DbPrefs.get("label_temp1");
    label_temp2 = DbPrefs.get("label_temp2");
    unit_rpm1 = DbPrefs.get("unit_rpm1");
    unit_rpm2 = DbPrefs.get("unit_rpm2");
    unit_temp1 = DbPrefs.get("unit_temp1");
    unit_temp2 = DbPrefs.get("unit_temp2");
    current_sensor_type = DbPrefs.get("current_sensor_type");
    alarm_voltage = getBoolean(alarm_voltage, DbPrefs.get("alarm_voltage"));
    alarm_used = getBoolean(alarm_used, DbPrefs.get("alarm_used"));
    alarm_current = getBoolean(alarm_current, DbPrefs.get("alarm_current"));
    alarm_speed = getBoolean(alarm_speed, DbPrefs.get("alarm_speed"));
    alarm_fuel = getBoolean(alarm_fuel, DbPrefs.get("alarm_fuel"));
    alarm_temperature = getBoolean(alarm_temperature, DbPrefs.get("alarm_temperature"));
    alarm_altitude = getBoolean(alarm_altitude, DbPrefs.get("alarm_altitude"));
    alarm_rpm = getBoolean(alarm_rpm, DbPrefs.get("alarm_rpm"));
    alarm_power = getBoolean(alarm_power, DbPrefs.get("alarm_power"));

    if (rpm1Label != null)
        rpm1Label.setText(label_rpm1);
    if (rpm2Label != null)
        rpm2Label.setText(label_rpm2);
    if (tempLabel != null)
        tempLabel.setText(label_temp1);
    if (temp2Label != null)
        temp2Label.setText(label_temp2);

    if (rpm1_unit != null)
        rpm1_unit.setText(unit_rpm1);
    if (rpm2_unit != null)
        rpm2_unit.setText(unit_rpm2);
    if (temp1_unit != null)
        temp1_unit.setText(unit_temp1);
    if (temp1_unit != null)
        temp1_unit.setText(unit_temp2);

    if (alert_tone.equals("")) {
        uri = null;
    } else {
        uri = Uri.parse(alert_tone);
    }

    if (mp != null) {
        mp.stop();
        mp.release();
        mp = null;
        if (uri != null && !uri.toString().equals("res/raw/alarm.mp3")) {
            mp = MediaPlayer.create(getApplicationContext(), uri);
        } else {
            mp = MediaPlayer.create(getApplicationContext(), R.raw.alarm);
        }
    }
}

From source file:com.aimfire.demo.CamcorderActivity.java

public void playShutterSound(boolean isStartSound) {
    int maxVolume = AudioConfigure.getMaxAudioVolume();

    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int volume = am.getStreamVolume(AudioManager.STREAM_MUSIC);

    /*/*from ww w .j  av  a 2s  .  c o m*/
     * clamp shutter volume to half of max (more than that it could be annoying)
     */
    if (volume > maxVolume / 2) {
        volume = maxVolume / 2;
    }
    AudioConfigure.setAudioVolume(volume);

    if (isStartSound && mCamStartSoundPlayer != null) {
        mCamStartSoundPlayer.start();
    } else if (!isStartSound && mCamStopSoundPlayer != null) {
        mCamStopSoundPlayer.start();
    }
}

From source file:org.kontalk.ui.ComposeMessageFragment.java

private boolean prepareAudio(File audioFile, final AudioContentViewControl view, final long messageId) {
    stopMediaPlayerUpdater();/*from www  .ja  v  a 2s .com*/
    try {
        AudioFragment audio = getAudioFragment();
        final MediaPlayer player = audio.getPlayer();
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setDataSource(audioFile.getAbsolutePath());
        player.prepare();

        // prepare was successful
        audio.setMessageId(messageId);
        mAudioControl = view;

        view.prepare(player.getDuration());
        player.seekTo(view.getPosition());
        view.setProgressChangeListener(true);
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                stopMediaPlayerUpdater();
                view.end();
                AudioFragment audio = findAudioFragment();
                if (audio != null)
                    audio.seekPlayerTo(0);
                setAudioStatus(AudioContentView.STATUS_ENDED);
            }
        });
        return true;
    } catch (IOException e) {
        Toast.makeText(getActivity(), R.string.err_file_not_found, Toast.LENGTH_SHORT).show();
        return false;
    }
}

From source file:com.rfo.basic.Run.java

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Log.v(LOGTAG, "onCreate " + this.toString());

    if (Basic.lines == null) {
        Log.e(LOGTAG, "onCreate: Lost context. Bail out.");
        finish();/* w ww.  j  av  a2 s.c  o m*/
        return;
    }

    //      Log.v(LOGTAG, "isOld  " + isOld);
    if (isOld) {
        if (theWakeLock != null) {
            theWakeLock.release();
        }
        if (theWifiLock != null) {
            theWifiLock.release();
        }
    }
    theWakeLock = null;
    theWifiLock = null;
    isOld = true;

    ContextManager cm = Basic.getContextManager();
    cm.registerContext(ContextManager.ACTIVITY_RUN, this);
    cm.setCurrent(ContextManager.ACTIVITY_RUN);

    mConsoleIntent = getIntent(); // keep a reference to the Intent that started Run
    InitRunVars();

    // Establish the output screen
    setContentView(R.layout.console);
    TextStyle style = new TextStyle(Basic.defaultTextStyle, Settings.getConsoleTypeface(this));
    mConsole = new Basic.ColoredTextAdapter(this, mOutput, style);
    clearConsole();
    lv = (ConsoleListView) findViewById(R.id.console);
    lv.setAdapter(mConsole);
    lv.setKeyboardManager(mKeyboardChangeListener);
    lv.setTextFilterEnabled(false);
    lv.setBackgroundColor(Settings.getEmptyConsoleColor(this).equals("line") ? mConsole.getLineColor()
            : mConsole.getBackgroundColor()); // default is "background"
    if (Settings.getLinedConsole(this)) {
        lv.setDivider(new ColorDrawable(mConsole.getLineColor())); // override default from theme, sometimes it's invisible
        if (lv.getDividerHeight() < 1) {
            lv.setDividerHeight(1);
        } // make sure the divider shows
    } else {
        lv.setDividerHeight(0); // don't show the divider
    }

    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    setRequestedOrientation(Settings.getSreenOrientation(this));

    headsetBroadcastReceiver = new BroadcastsHandler();
    this.registerReceiver(headsetBroadcastReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));

    // Listeners for Console Touch

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            triggerInterrupt(Interrupt.CONS_TOUCH_BIT);
            TouchedConsoleLine = position + 1;
            ConsoleLongTouch = false;
        }
    });

    lv.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            triggerInterrupt(Interrupt.CONS_TOUCH_BIT);
            TouchedConsoleLine = position + 1;
            ConsoleLongTouch = true;
            return true;
        }
    });

    mInterpreter = new Interpreter(); // start the interpreter in a background Thread
    mInterpreter.start();

}

From source file:org.distantshoresmedia.keyboard.LatinIME.java

private float getKeyClickVolume() {
    if (mAudioManager == null)
        return 0.0f; // shouldn't happen

    // The volume calculations are poorly documented, this is the closest I could
    // find for explaining volume conversions:
    // http://developer.android.com/reference/android/media/MediaPlayer.html#setAuxEffectSendLevel(float)
    ///*from w  w  w.  ja v a  2  s. co m*/
    //   Note that the passed level value is a raw scalar. UI controls should be scaled logarithmically:
    //   the gain applied by audio framework ranges from -72dB to 0dB, so an appropriate conversion 
    //   from linear UI input x to level is: x == 0 -> level = 0 0 < x <= R -> level = 10^(72*(x-R)/20/R)

    int method = sKeyboardSettings.keyClickMethod; // See click_method_values in strings.xml
    if (method == 0)
        return FX_VOLUME;

    float targetVol = sKeyboardSettings.keyClickVolume;

    if (method > 1) {
        // TODO(klausw): on some devices the media volume controls the click volume?
        // If that's the case, try to set a relative target volume.
        int mediaMax = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int mediaVol = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        //Log.i(TAG, "getKeyClickVolume relative, media vol=" + mediaVol + "/" + mediaMax);
        float channelVol = (float) mediaVol / mediaMax;
        if (method == 2) {
            targetVol *= channelVol;
        } else if (method == 3) {
            if (channelVol == 0)
                return 0.0f; // Channel is silent, won't get audio
            targetVol = Math.min(targetVol / channelVol, 1.0f); // Cap at 1.0
        }
    }
    // Set absolute volume, treating the percentage as a logarithmic control
    float vol = (float) Math.pow(10.0, FX_VOLUME_RANGE_DB * (targetVol - 1) / 20);
    //Log.i(TAG, "getKeyClickVolume absolute, target=" + targetVol + " amp=" + vol);
    return vol;
}

From source file:com.nest5.businessClient.Initialactivity.java

private boolean checkResetVolume() {

    boolean ret = true;

    int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

    int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

    if (currentVolume < maxVolume) {

        showMessageDialog("Atencin",
                "Para leer una tarjeta Nest5 debes subir el volumen de tu dispositivo al mximo.");
        ret = false;/*from   ww w  . j a  v a  2 s.co  m*/
    }

    return ret;
}