List of usage examples for android.media AudioManager getStreamVolume
public int getStreamVolume(int streamType)
From source file:uk.ac.ucl.excites.sapelli.shared.util.android.DeviceControl.java
/** * Decreases the Media Volume only up to half of the device's max volume * //from www. jav a2s . c o m * @param context */ public static void safeDecreaseMediaVolume(Context context) { // Get the volume final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); final int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); final int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); final int minVolume = maxVolume / 2; Debug.d("currentVolume: " + currentVolume); Debug.d("maxVolume: " + maxVolume); Debug.d("minVolume: " + minVolume); if (currentVolume > minVolume) decreaseMediaVolume(context); }
From source file:com.wifi.brainbreaker.mydemo.spydroid.api.RequestHandler.java
/** * The implementation of all the possible requests is here * -> "sounds": returns a list of available sounds on the phone * -> "screen": returns the screen state (whether the app. is on the foreground or not) * -> "play": plays a sound on the phone * -> "set": update Spydroid's configuration * -> "get": returns Spydroid's configuration (framerate, bitrate...) * -> "state": returns a JSON containing information about the state of the application * -> "battery": returns an approximation of the battery level on the phone * -> "buzz": makes the phone buuz /*from w ww. ja v a 2s .c o m*/ * -> "volume": sets or gets the volume * @throws JSONException * @throws IllegalAccessException * @throws IllegalArgumentException **/ static private void exec(JSONObject object, StringBuilder response) throws JSONException, IllegalArgumentException, IllegalAccessException { SpydroidApplication application = SpydroidApplication.getInstance(); Context context = application.getApplicationContext(); String action = object.getString("action"); // Returns a list of available sounds on the phone if (action.equals("sounds")) { Field[] raws = R.raw.class.getFields(); response.append("["); for (int i = 0; i < raws.length - 1; i++) { response.append("\"" + raws[i].getName() + "\","); } response.append("\"" + raws[raws.length - 1].getName() + "\"]"); } // Returns the screen state (whether the app. is on the foreground or not) else if (action.equals("screen")) { response.append(application.applicationForeground ? "\"1\"" : "\"0\""); } // Plays a sound on the phone else if (action.equals("play")) { Field[] raws = R.raw.class.getFields(); for (int i = 0; i < raws.length; i++) { if (raws[i].getName().equals(object.getString("name"))) { mSoundPool.load(application, raws[i].getInt(null), 0); } } response.append("[]"); } // Returns Spydroid's configuration (framerate, bitrate...) else if (action.equals("get")) { final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); response.append("{\"streamAudio\":" + settings.getBoolean("stream_audio", false) + ","); response.append("\"audioEncoder\":\"" + (application.audioEncoder == SessionBuilder.AUDIO_AMRNB ? "AMR-NB" : "AAC") + "\","); response.append("\"streamVideo\":" + settings.getBoolean("stream_video", true) + ","); response.append("\"videoEncoder\":\"" + (application.videoEncoder == SessionBuilder.VIDEO_H263 ? "H.263" : "H.264") + "\","); response.append("\"videoResolution\":\"" + application.videoQuality.resX + "x" + application.videoQuality.resY + "\","); response.append("\"videoFramerate\":\"" + application.videoQuality.framerate + " fps\","); response.append("\"videoBitrate\":\"" + application.videoQuality.bitrate / 1000 + " kbps\"}"); } // Update Spydroid's configuration else if (action.equals("set")) { final JSONObject settings = object.getJSONObject("settings"); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); final Editor editor = prefs.edit(); editor.putBoolean("stream_video", settings.getBoolean("stream_video")); application.videoQuality = VideoQuality.parseQuality(settings.getString("video_quality")); editor.putInt("video_resX", application.videoQuality.resX); editor.putInt("video_resY", application.videoQuality.resY); editor.putString("video_framerate", String.valueOf(application.videoQuality.framerate)); editor.putString("video_bitrate", String.valueOf(application.videoQuality.bitrate / 1000)); editor.putString("video_encoder", settings.getString("video_encoder").equals("H.263") ? "2" : "1"); editor.putBoolean("stream_audio", settings.getBoolean("stream_audio")); editor.putString("audio_encoder", settings.getString("audio_encoder").equals("AMR-NB") ? "3" : "5"); editor.commit(); response.append("[]"); } // Returns a JSON containing information about the state of the application else if (action.equals("state")) { Exception exception = application.lastCaughtException; response.append("{"); if (exception != null) { // Used to display the message on the user interface String lastError = exception.getMessage(); // Useful to display additional information to the user depending on the error StackTraceElement[] stack = exception.getStackTrace(); StringBuilder builder = new StringBuilder( exception.getClass().getName() + " : " + lastError + "||"); for (int i = 0; i < stack.length; i++) builder.append("at " + stack[i].getClassName() + "." + stack[i].getMethodName() + " (" + stack[i].getFileName() + ":" + stack[i].getLineNumber() + ")||"); response.append("\"lastError\":\"" + (lastError != null ? lastError : "unknown error") + "\","); response.append("\"lastStackTrace\":\"" + builder.toString() + "\","); } response.append("\"activityPaused\":\"" + (application.applicationForeground ? "1" : "0") + "\""); response.append("}"); } else if (action.equals("clear")) { application.lastCaughtException = null; response.append("[]"); } // Returns an approximation of the battery level else if (action.equals("battery")) { response.append("\"" + application.batteryLevel + "\""); } // Makes the phone vibrates for 300ms else if (action.equals("buzz")) { Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(300); response.append("[]"); } // Sets or gets the system's volume else if (action.equals("volume")) { AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); if (object.has("set")) { audio.setStreamVolume(AudioManager.STREAM_MUSIC, object.getInt("set"), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); response.append("[]"); } else { int max = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC); int current = audio.getStreamVolume(AudioManager.STREAM_MUSIC); response.append("{\"max\":" + max + ",\"current\":" + current + "}"); } } }
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;/* w w w .j a v a2 s.co m*/ } 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.shinymayhem.radiopresets.FragmentPlayer.java
public void updateSlider() { SeekBar volumeSlider = (SeekBar) this.getView().findViewById(R.id.volume_slider); AudioManager audio = (AudioManager) this.getActivity().getSystemService(Context.AUDIO_SERVICE); volumeSlider.setProgress(audio.getStreamVolume(AudioManager.STREAM_MUSIC)); }
From source file:org.hansel.myAlert.ReminderService.java
private void playSound() { mMediaPlayer = new MediaPlayer(); try {//from ww w .java2 s . c o m mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(Util.getRingtone(getApplicationContext()))); final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) { mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mMediaPlayer.prepare(); mMediaPlayer.setLooping(true); mMediaPlayer.start(); } } catch (IOException e) { System.out.println("Error tocando alarma"); } }
From source file:org.hansel.myAlert.AlarmFragment.java
private void playSound(Context context, Uri alert) { mMediaPlayer = new MediaPlayer(); try {//from ww w . j a v a 2 s . co m mMediaPlayer.setDataSource(context, alert); final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) { mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mMediaPlayer.prepare(); mMediaPlayer.start(); } } catch (IOException e) { System.out.println("Error tocando alarma"); } }
From source file:com.intel.xdk.notification.Notification.java
public void beep(int count) { beepCount = (count > 0) ? count - 1 : 0; AudioManager mgr = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE); final int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC); soundPool.play(SOUND_BEEP, streamVolume, streamVolume, 1, beepCount, 1f); }
From source file:com.meiste.tempalarm.ui.Alarm.java
@Override protected void onResume() { super.onResume(); final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); // do not play alarms if stream volume is 0 (typically because ringer mode is silent). if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) { final Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); mMediaPlayer = new MediaPlayer(); mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { public boolean onError(final MediaPlayer mp, final int what, final int extra) { Timber.e("Error occurred while playing audio."); mp.stop();/*from w w w.j a va 2 s . com*/ mp.reset(); mp.release(); mMediaPlayer = null; return true; } }); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); try { mMediaPlayer.setDataSource(this, alert); mMediaPlayer.setLooping(true); mMediaPlayer.prepare(); mMediaPlayer.start(); } catch (final IOException e) { Timber.e("Failed to play alarm tone: %s", e); } } mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); mVibrator.vibrate(sVibratePattern, 0); mPlaying = true; mHandler.sendEmptyMessageDelayed(KILLER, ALARM_TIMEOUT); }
From source file:com.brayanarias.alarmproject.activity.AlarmScreenActivity.java
private void playRingtone(String tone) { mediaPlayer = new MediaPlayer(); try {/*from www . j a v a 2 s .c o m*/ Uri ringtoneUri; if (tone.equals("default")) { ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); } else { ringtoneUri = Uri.parse(tone); } if (ringtoneUri != null) { mediaPlayer.setDataSource(this, ringtoneUri); mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mediaPlayer.setLooping(true); mediaPlayer.prepare(); AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); actualVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM); incrementVolume(); mediaPlayer.start(); audioManager.setStreamVolume(AudioManager.STREAM_ALARM, 1, 0); } } catch (Exception e) { Log.e(tag, e.getLocalizedMessage(), e); } }
From source file:net.xisberto.work_schedule.alarm.AlarmMessageActivity.java
private void prepareSound(Context context, Uri alert) { mMediaPlayer = new MediaPlayer(); mMediaPlayer.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK); try {/*from w ww . j a v a 2s .c o m*/ mMediaPlayer.setDataSource(context, alert); final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) { mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mMediaPlayer.setLooping(true); mMediaPlayer.prepare(); } } catch (IOException e) { System.out.println("OOPS"); } }