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:com.ieeton.agency.activity.ChatActivity.java

private void playSound() {
    AudioManager mAudioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    int curVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
    if (curVolume == 0) {
        return;/*  www.j  a va2s.  c o  m*/
    }

    mStreamID = mSoundPool.play(mSoundID, curVolume, curVolume, 0, 0, 1);
}

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

public void submitChat() {

    ChatConfig config = new ChatConfig();
    config.instance = this.instance.id;
    config.conversation = MainActivity.conversation;
    config.speak = !MainActivity.deviceVoice;
    config.avatar = this.avatarId;
    if (MainActivity.translate && MainActivity.voice != null) {
        config.language = MainActivity.voice.language;
    }//from   w  ww.j  a va2s.c o  m
    if (MainActivity.disableVideo) {
        config.avatarFormat = "image";
    } else {
        config.avatarFormat = MainActivity.webm ? "webm" : "mp4";
    }
    config.avatarHD = MainActivity.hd;

    EditText v = (EditText) findViewById(R.id.messageText);
    config.message = v.getText().toString().trim();
    if (config.message.equals("")) {
        return;
    }
    this.messages.add(config);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            ListView list = (ListView) findViewById(R.id.chatList);
            ((ChatListAdapter) list.getAdapter()).notifyDataSetChanged();
            list.invalidateViews();
        }

    });

    Spinner emoteSpin = (Spinner) findViewById(R.id.emoteSpin);
    config.emote = emoteSpin.getSelectedItem().toString();

    HttpChatAction action = new HttpChatAction(ChatActivity.this, config);
    action.execute();

    v.setText("");
    emoteSpin.setSelection(0);
    resetToolbar();

    WebView responseView = (WebView) findViewById(R.id.responseText);
    responseView.loadDataWithBaseURL(null, "thinking...", "text/html", "utf-8", null);

    //Check the volume
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    if (volume <= 3 && volumeChecked) {
        Toast.makeText(this, "Please check 'Media' volume", Toast.LENGTH_LONG).show();
        volumeChecked = false;
    }

    //stop letting the mic on.
    stopListening();
    //its Important for "sleep" "scream" ...etc commands.
    //this will turn off the mic
    MainActivity.listenInBackground = false;
}

From source file:com.metinkale.prayerapp.vakit.AlarmReceiver.java

public void fireAlarm(Intent intent) throws InterruptedException {

    Context c = App.getContext();

    if ((intent == null) || !intent.hasExtra("json")) {

        return;/*from   w  w  w.ja  v  a2s  .com*/
    }
    Alarm next = Alarm.fromString(intent.getStringExtra("json"));
    intent.removeExtra("json");

    if (next.city == 0) {
        return;
    }

    Times t = Times.getTimes(next.city);
    if (t == null)
        return;
    boolean active;
    if (next.cuma) {
        active = t.isCumaActive();
    } else if (next.early) {
        active = t.isEarlyNotificationActive(next.vakit);
    } else {
        active = t.isNotificationActive(next.vakit);
    }
    if (!active) {
        return;
    }

    boolean vibrate;
    String sound;
    String dua;
    long silenter;
    if (next.cuma) {
        vibrate = t.hasCumaVibration();
        sound = t.getCumaSound();
        dua = "silent";
        silenter = t.getCumaSilenterDuration();
    } else if (next.early) {
        vibrate = t.hasEarlyVibration(next.vakit);
        sound = t.getEarlySound(next.vakit);
        dua = "silent";
        silenter = t.getEarlySilenterDuration(next.vakit);
    } else {
        vibrate = t.hasVibration(next.vakit);
        sound = t.getSound(next.vakit);
        dua = t.getDua(next.vakit);
        silenter = t.getSilenterDuration(next.vakit);
    }

    NotificationManager nm = (NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE);

    nm.cancel(next.city + "", NotIds.ALARM);
    String text;

    text = t.getName() + " (" + t.getSource() + ")";

    String txt = "";
    if (next.early) {
        String[] left_part = App.getContext().getResources().getStringArray(R.array.lefttext_part);
        txt = App.getContext().getString(R.string.earlyText, left_part[next.vakit.index],
                "" + t.getEarlyTime(next.vakit));
    } else if (next.cuma) {
        String[] left_part = App.getContext().getResources().getStringArray(R.array.lefttext_part);
        txt = App.getContext().getString(R.string.earlyText, left_part[next.vakit.index], "" + t.getCumaTime());
    } else if (next.vakit != null) {
        txt = next.vakit.getString();
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(c).setContentTitle(text)
            .setContentText(txt).setContentIntent(Main.getPendingIntent(t)).setSmallIcon(R.drawable.ic_abicon);
    Notification not = builder.build();

    if (vibrate) {
        not.vibrate = VibrationPreference.getPattern(c, "vibration");
    }

    AudioManager am = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);

    class MPHolder {
        MediaPlayer mp;
    }
    not.deleteIntent = PendingIntent.getBroadcast(c, 0, new Intent(c, Audio.class),
            PendingIntent.FLAG_UPDATE_CURRENT);

    nm.notify(next.city + "", NotIds.ALARM, not);

    final MPHolder mp = new MPHolder();

    if (Prefs.showNotificationScreen() && (sound != null) && !sound.startsWith("silent")) {
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        if (!pm.isScreenOn()) {
            Intent i = new Intent(c, NotificationPopup.class);
            i.putExtra("city", next.city);
            i.putExtra("name", text);
            i.putExtra("vakit", txt);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            c.startActivity(i);

            Thread.sleep(1000);
        }
    }

    sInterrupt = false;
    boolean hasSound = false;
    while ((sound != null) && !sound.startsWith("silent") && !sInterrupt) {
        int volume = -2;
        hasSound = true;

        if (!sound.startsWith("silent") && !sound.startsWith("picker")) {

            if (sound.contains("$volume")) {
                volume = Integer.parseInt(sound.substring(sound.indexOf("$volume") + 7));
                sound = sound.substring(0, sound.indexOf("$volume"));
            }
            if (volume != -2) {
                int oldvalue = am.getStreamVolume(getStreamType(c));
                am.setStreamVolume(getStreamType(c), volume, 0);
                volume = oldvalue;
            }

            try {
                mp.mp = play(c, sound);
            } catch (IOException e) {
                e.printStackTrace();
                if (next.cuma) {
                    t.setCumaSound("silent");
                } else if (next.early) {
                    t.setEarlySound(next.vakit, "silent");
                } else {
                    if ("sound".equals(t.getSound(next.vakit))) {
                        t.setSound(next.vakit, "silent");
                    } else {
                        t.setDua(next.vakit, "silent");
                    }
                }
                mp.mp = null;
            }

            if (mp.mp != null) {

                mp.mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        if (mp.mp == null) {
                            return;
                        }
                        mp.mp.stop();
                        mp.mp.release();
                        mp.mp = null;
                    }
                });

                mp.mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                    @Override
                    public void onSeekComplete(MediaPlayer mediaPlayer) {
                        if (mp.mp == null) {
                            return;
                        }
                        mp.mp.stop();
                        mp.mp.release();
                        mp.mp = null;
                    }
                });

            }

            sInterrupt = false;

            while ((mp.mp != null) && mp.mp.isPlaying()) {
                if (sInterrupt) {
                    mp.mp.stop();
                    mp.mp.release();
                    mp.mp = null;

                    dua = null;
                } else {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ignore) {
                    }
                }
            }
            sInterrupt = false;

        }

        if (volume != -2) {
            am.setStreamVolume(getStreamType(c), volume, 0);
        }
        sound = dua;
        dua = null;
    }

    if (hasSound && Prefs.autoRemoveNotification()) {
        nm.cancel(next.city + "", NotIds.ALARM);
    }
    if (silenter != 0) {
        silenter(c, silenter);
    }

}

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

protected void setVolume(int newVolume) {
    AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audio.setStreamVolume(AudioManager.STREAM_MUSIC, newVolume, 0);
    if (LOCAL_LOGV)
        log("volume set:" + String.valueOf(audio.getStreamVolume(AudioManager.STREAM_MUSIC)), "v");
}

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  w  w w  .j  av  a 2  s . c  o  m
            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: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);

    /*//w w  w.  j av  a2 s. 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:com.nest5.businessClient.Initialactivity.java

public void playSound(Context context)
        throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    MediaPlayer mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setDataSource(context, soundUri);
    final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        mMediaPlayer.setLooping(false);//from  ww  w.ja  v  a  2 s.  co m
        mMediaPlayer.prepare();
        mMediaPlayer.start();
    }
}