Example usage for android.media AudioManager getStreamVolume

List of usage examples for android.media AudioManager getStreamVolume

Introduction

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

Prototype

public int getStreamVolume(int streamType) 

Source Link

Document

Returns the current volume index for a particular stream.

Usage

From source file:it.interfree.leonardoce.bootreceiver.AlarmKlaxon.java

private void startAlarm(MediaPlayer player)
        throws java.io.IOException, IllegalArgumentException, IllegalStateException {
    final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    // Non deve suonare se il cellulare e' silenzioso
    Log.v(LTAG, "Stato del telefono: " + audioManager.getRingerMode());

    // do not play alarms if stream volume is 0
    // (typically because ringer mode is silent).
    if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0
            && audioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT
            && audioManager.getRingerMode() != AudioManager.RINGER_MODE_VIBRATE) {
        player.setAudioStreamType(AudioManager.STREAM_ALARM);
        player.setLooping(true);/*w  w w  . jav  a  2 s.com*/
        player.prepare();
        player.start();
    }
}

From source file:se.erichansander.retrotimer.TimerKlaxon.java

private void play(boolean ring, boolean vibrate) {
    TinyTracelog.trace("5");

    // stop() checks to see if we are already playing.
    stop();/*from  www .ja  v  a 2s.  c  o m*/

    if (ring) {
        TinyTracelog.trace("5.1");
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnErrorListener(new OnErrorListener() {
            public boolean onError(MediaPlayer mp, int what, int extra) {
                TinyTracelog.trace("5.1.e1");
                stop();
                return true;
            }
        });

        try {
            /*
             * Check if we are in a call. If we are, use the in-call alarm
             * resource at a low volume to not disrupt the call.
             */
            if (mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE) {
                TinyTracelog.trace("5.1.1");
                mMediaPlayer.setVolume(IN_CALL_VOLUME, IN_CALL_VOLUME);
                setDataSourceFromResource(getResources(), mMediaPlayer, R.raw.in_call_alarm);
            } else {
                TinyTracelog.trace("5.1.2");
                setDataSourceFromResource(getResources(), mMediaPlayer, R.raw.classic_alarm);
            }
        } catch (Exception ex) {
            // Failed to set data source. Not much we can do to save
            // the situation though...
            TinyTracelog.trace("5.1.e2");
        }

        try {
            final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            // do not play alarms if stream volume is 0
            int volume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
            TinyTracelog.trace("5.1.3 " + volume);
            if (volume != 0) {
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                mMediaPlayer.setLooping(true);
                mMediaPlayer.prepare();
                if (mAudioFocusHelper != null) {
                    mAudioFocusHelper.requestFocus();
                }
                mMediaPlayer.start();
            }
        } catch (Exception ex) {
            // Failed to play ring tone. Not much we can do to save
            // the situation though...
            TinyTracelog.trace("5.1.e3");
        }
    }

    /* Start the vibrator after everything is ok with the media player */
    if (vibrate) {
        TinyTracelog.trace("5.2");
        mVibrator.vibrate(sVibratePattern, 0);
    } else {
        mVibrator.cancel();
    }

    mPlaying = true;
}

From source file:com.mobileman.moments.android.frontend.activity.IncomingCallActivity.java

private void playRingtone() {
    AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    if ((audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE)
            || (audioManager.getStreamVolume(AudioManager.STREAM_RING) == 0)) {
        vibrate();//from  w w  w  .  j  a v a 2 s.  co  m
        delayHandler = new Handler();
        delayHandler.postDelayed(new Runnable() {
            public void run() {
                sendNotificationAndFinish();
            }
        }, MAXIMUM_CALL_NOTIFICATION_DURATION);
    } else if (audioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT) {
        if (mPlayer == null) {
            mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.moments_30s);
            //            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    sendNotificationAndFinish();
                }
            });
            mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mediaPlayer, int i, int i2) {
                    sendNotificationAndFinish();
                    return false;
                }
            });
            mPlayer.start();
        }
    }
}

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

private void setStreamVolume() {
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    if (volume != 0) {
        Log.d("ChatActivity", "The volume changed and saved to : " + volume);
        MainActivity.volume = volume;// w w  w.  j a  v a 2 s  .  com
    }
}

From source file:org.protocoderrunner.apprunner.AppRunnerFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    MLog.d(TAG, "onActivityCreated");

    String toolbarName = "";
    if (mProjectFolder.equals("examples")) {
        toolbarName = "example > " + mProjectName;
    } else {// www.  ja v  a2 s  .com
        toolbarName = mProjectName;
    }
    mActivity.setToolBar(toolbarName, null, null);

    //catch errors and send them to the webIDE or the app console
    AppRunnerInterpreter.InterpreterInfo appRunnerCb = new AppRunnerInterpreter.InterpreterInfo() {
        @Override
        public void onError(String message) {
            MLog.d(TAG, "error " + message);
            showConsole(message);

            // send to web ide
            JSONObject obj = new JSONObject();
            try {
                obj.put("type", "error");
                obj.put("values", message);
                //MLog.d(TAG, "error " + obj.toString(2));
                IDEcommunication.getInstance(mActivity).send(obj);
            } catch (JSONException er1) {
                er1.printStackTrace();
            }
        }
    };
    interp.addListener(appRunnerCb);

    // load the libraries
    MLog.d(TAG, "loaded preloaded script" + mIntentPrefixScript);
    interp.eval(AppRunnerInterpreter.SCRIPT_PREFIX);
    if (!mIntentPrefixScript.isEmpty())
        interp.eval(mIntentPrefixScript);

    // run the script
    if (null != mScript) {
        interp.eval(mScript, mProjectName);
    }
    //can accept intent code if no project is loaded
    if (!mIsProjectLoaded) {
        interp.eval(mIntentCode);
    }

    //script postfix
    if (!mIntentPostfixScript.isEmpty())
        interp.eval(mIntentPostfixScript);
    interp.eval(AppRunnerInterpreter.SCRIPT_POSTFIX);

    //call the javascript method setup
    interp.callJsFunction("setup");

    // TODO fix actionbar color
    //if (mActivity.mActionBarSet == false) {
    //    mActivity.setToolBar(null, , getResources().getColor(R.color.white));
    //}
    // Call the onCreate JavaScript function.
    interp.callJsFunction("onCreate", savedInstanceState);

    //audio
    AudioManager audio = (AudioManager) mActivity.getSystemService(Context.AUDIO_SERVICE);
    int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
    mActivity.setVolumeControlStream(AudioManager.STREAM_MUSIC);

    //nfc
    mActivity.initializeNFC();

    //file observer will notifify project file changes
    startFileObserver();

    // send ready to the webIDE
    //TODO this is gone ?!
}

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.wso2.mobile.mdm.services.PolicyTester.java

private boolean isMuted() {
    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0) {
        return false;
    } else {/* w  ww. ja  va2  s  . c om*/
        return true;
    }
}

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

public boolean isPhoneMuted() {
    final AudioManager audioMan = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    return audioMan.getStreamVolume(AudioManager.STREAM_RING) == 0;
}

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

public boolean isAudioStreamMuted() {
    final AudioManager audioMan = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    return audioMan.getStreamVolume(prefs.getAudioStream()) == 0;
}

From source file:com.nbplus.vbroadlauncher.RealtimeBroadcastActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    acquireCpuWakeLock();/*from w  w  w  .  j  a  va 2s.  co m*/

    KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    km.inKeyguardRestrictedInputMode();
    if (km.inKeyguardRestrictedInputMode()) {
        Log.i(TAG, " ??");
        isKeyguardRestrictedInputMode = true;
    } else {
        Log.i(TAG, "   ??");
    }

    mLastNetworkStatus = NetworkUtils.isConnected(this);
    Intent intent = getIntent();
    if (intent == null || !PushConstants.ACTION_PUSH_MESSAGE_RECEIVED.equals(intent.getAction())) {
        Log.d(TAG, "empty or none broadcast intent value ...");
        finishActivity();
        return;
    }

    mBroadcastData = intent.getParcelableExtra(Constants.EXTRA_BROADCAST_PAYLOAD_DATA);
    if (mBroadcastData == null) {
        Log.d(TAG, ">> payload data is null");
        finishActivity();
        return;
    }

    mBroadcastPayloadIdx = intent.getLongExtra(Constants.EXTRA_BROADCAST_PAYLOAD_INDEX, -1);
    Log.d(TAG, ">> onCreate() mBroadcastPayloadIdx= " + mBroadcastPayloadIdx);

    // ? ms  ? ?? ? ?   ?  ????
    // broadcast ? .
    //  ?? ?? ??.
    Intent i = new Intent(this, RealtimeBroadcastActivity.class);
    i.setAction(intent.getAction());
    i.putExtra(Constants.EXTRA_BROADCAST_PAYLOAD_DATA, mBroadcastData);
    i.putExtra(Constants.EXTRA_BROADCAST_PAYLOAD_INDEX, mBroadcastPayloadIdx);
    LocalBroadcastManager.getInstance(this).sendBroadcast(i);

    IntentFilter filter = new IntentFilter();
    filter.addAction(PushConstants.ACTION_PUSH_MESSAGE_RECEIVED);
    filter.addAction(Constants.ACTION_BROWSER_ACTIVITY_CLOSE);
    LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, filter);

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(mBroadcastReceiver, intentFilter);

    hideSystemUI();
    /*
    final Window win = getWindow();
    win.setFlags(
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_FULLSCREEN
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_FULLSCREEN
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
            
    win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
    */

    if (Constants.PUSH_PAYLOAD_TYPE_TEXT_BROADCAST.equals(mBroadcastData.getServiceType())) {
        setContentView(R.layout.fragment_text_broadcast);
    } else {
        setContentView(R.layout.fragment_audio_broadcast);
    }
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

    if (Constants.PUSH_PAYLOAD_TYPE_TEXT_BROADCAST.equals(mBroadcastData.getServiceType())) {
        mcheckText2SpeechLister = this;
        // ?
        mTextView = (TextView) findViewById(R.id.broadcast_text);
        mTextView.setText(mBroadcastData.getMessage());
        mTextView.setVerticalScrollBarEnabled(true);
        mTextView.setHorizontalScrollBarEnabled(false);
        mTextView.setMovementMethod(new ScrollingMovementMethod());

        mHandler.sendEmptyMessageDelayed(HANDLER_MESSAGE_SETUP_CURRENT_PLAYING, 800);
        Log.d(TAG, "text broadcast = " + mBroadcastData.getMessage());

        mText2SpeechHandler = new TextToSpeechHandler(this, this);
        checkText2SpeechAvailable();
        mIsTTS = true;
    } else {
        // , ??
        mWebView = (WebView) findViewById(R.id.webview);
        mWebViewClient = new RealtimeBroadcastWebViewClient(this, mWebView, this);
        mWebViewClient.setBackgroundTransparent();

        String url = mBroadcastData.getMessage();
        if (url.indexOf("?") > 0) {
            if (!url.contains("UUID=")) {
                url += ("&UUID=" + LauncherSettings.getInstance(this).getDeviceID());
            }
            if (!url.contains("APPID=")) {
                url += ("&APPID=" + getApplicationContext().getPackageName());
            }
        } else {
            if (!url.contains("UUID=")) {
                url += ("?UUID=" + LauncherSettings.getInstance(this).getDeviceID());
            }
            if (!url.contains("APPID=")) {
                if (!url.contains("UUID=")) {
                    url += ("?APPID=" + getApplicationContext().getPackageName());
                } else {
                    url += ("&APPID=" + getApplicationContext().getPackageName());
                }
            }
        }

        mWebViewClient.loadUrl(url);
        mIsTTS = false;
    }
    AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mStreamMusicVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
    audio.setStreamVolume(AudioManager.STREAM_MUSIC, audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
            AudioManager.FLAG_PLAY_SOUND);

    if (Constants.OPEN_BETA_PHONE && LauncherSettings.getInstance(this).isSmartPhone()) {
        StateListener phoneStateListener = new StateListener();
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }
}