Example usage for android.media MediaPlayer setVolume

List of usage examples for android.media MediaPlayer setVolume

Introduction

In this page you can find the example usage for android.media MediaPlayer setVolume.

Prototype


public void setVolume(float leftVolume, float rightVolume) 

Source Link

Document

Sets the volume on this player.

Usage

From source file:com.sentaroh.android.TaskAutomation.TaskExecutor.java

final static public void playBackMusic(TaskManagerParms taskMgrParms, EnvironmentParms envParms,
        CommonUtilities util, TaskResponse taskResponse, ActionResponse ar, String action, String dlg_id,
        String fp, String vol_left, String vol_right) {
    ar.action_resp = ActionResponse.ACTION_SUCCESS;
    File lf = new File(fp);
    if (!lf.exists()) {
        ar.action_resp = ActionResponse.ACTION_ERROR;
        ar.resp_msg_text = String.format(taskMgrParms.teMsgs.msgs_thread_task_play_sound_notfound, fp);
        return;//  www  .  j av  a 2  s .co  m
    }
    if (!isRingerModeNormal(envParms)) {
        ar.action_resp = ActionResponse.ACTION_WARNING;
        ar.resp_msg_text = taskMgrParms.teMsgs.msgs_thread_task_exec_ignore_sound_ringer_not_normal;
        return;
    }
    MediaPlayer player = MediaPlayer.create(taskMgrParms.context, Uri.parse(fp));
    taskResponse.active_action_name = action;

    if (player != null) {
        int duration = player.getDuration();
        TaskManager.showMessageDialog(taskMgrParms, envParms, util, taskResponse.active_group_name,
                taskResponse.active_task_name, taskResponse.active_action_name, taskResponse.active_dialog_id,
                MESSAGE_DIALOG_MESSAGE_TYPE_SOUND, fp);

        if (!vol_left.equals("-1") && !vol_left.equals(""))
            player.setVolume(Float.valueOf(vol_left) / 100, Float.valueOf(vol_right) / 100);
        player.start();
        waitTimeTc(taskResponse, duration + 10);
        if (!taskResponse.active_thread_ctrl.isEnable()) {
            ar.action_resp = ActionResponse.ACTION_CANCELLED;
            ar.resp_msg_text = "Action was cancelled";
        }
        player.stop();
        player.release();
        TaskManager.closeMessageDialog(taskMgrParms, envParms, util, taskResponse);
    } else {
        ar.action_resp = ActionResponse.ACTION_ERROR;
        ar.resp_msg_text = String.format(taskMgrParms.teMsgs.msgs_thread_task_play_sound_error, fp);
    }
}

From source file:com.sentaroh.android.TaskAutomation.TaskExecutor.java

final static public void playBackRingtone(TaskManagerParms taskMgrParms, EnvironmentParms envParms,
        CommonUtilities util, TaskResponse taskResponse, ActionResponse ar, String dlg_id, String rt, String rn,
        String rp, String vol_left, String vol_right) {
    ar.action_resp = ActionResponse.ACTION_SUCCESS;
    if (!isRingerModeNormal(envParms)) {
        ar.action_resp = ActionResponse.ACTION_WARNING;
        ar.resp_msg_text = taskMgrParms.teMsgs.msgs_thread_task_exec_ignore_sound_ringer_not_normal;
        return;/*from  w w  w. j  ava2s .c o m*/
    }
    MediaPlayer player = MediaPlayer.create(taskMgrParms.context, Uri.parse("content://media" + rp));
    //      taskResponse.active_action_name=action;
    if (player != null) {
        int duration = 0;
        if (!rt.equals(PROFILE_ACTION_RINGTONE_TYPE_NOTIFICATION))
            duration = RINGTONE_PLAYBACK_TIME;
        else
            duration = player.getDuration();

        if (duration >= 5000) {
            TaskManager.showMessageDialog(taskMgrParms, envParms, util, taskResponse.active_group_name,
                    taskResponse.active_task_name, taskResponse.active_action_name,
                    taskResponse.active_dialog_id, MESSAGE_DIALOG_MESSAGE_TYPE_SOUND, rt + " " + rn);

        }
        if (!vol_left.equals("-1") && !vol_left.equals(""))
            player.setVolume(Float.valueOf(vol_left) / 100, Float.valueOf(vol_right) / 100);
        player.start();
        waitTimeTc(taskResponse, duration + 10);
        if (!taskResponse.active_thread_ctrl.isEnable()) {
            ar.action_resp = ActionResponse.ACTION_CANCELLED;
            ar.resp_msg_text = "Action was cancelled";
        }
        player.stop();
        player.release();
        if (duration >= 5000) {
            TaskManager.closeMessageDialog(taskMgrParms, envParms, util, taskResponse);
        }
    } else {
        ar.action_resp = ActionResponse.ACTION_ERROR;
        ar.resp_msg_text = String.format(taskMgrParms.teMsgs.msgs_thread_task_play_sound_error, rp);
    }
}

From source file:com.rks.musicx.services.MusicXService.java

/**
 * FadeOut/*from ww  w  .j av  a 2 s .  co m*/
 *
 * @param _player
 * @param duration
 */
public void fadeOut(final MediaPlayer _player, final int duration) {
    final float deviceVolume = getDeviceVolume();
    final Handler h = new Handler();
    h.postDelayed(new Runnable() {
        private float time = duration;
        private float volume = 0.0f;

        @Override
        public void run() {
            // can call h again after work!
            time -= 100;
            volume = (deviceVolume * time) / duration;
            _player.setVolume(volume, volume);
            if (time > 0)
                h.postDelayed(this, 100);
            else {
                _player.pause();
            }
        }
    }, 100); // 1 second delay (takes millis)
}

From source file:com.rks.musicx.services.MusicXService.java

/**
 * Fade In//  w ww . j av a  2  s . c  om
 *
 * @param _player
 * @param duration
 */
public void fadeIn(final MediaPlayer _player, final int duration) {
    final float deviceVolume = getDeviceVolume();
    final Handler h = new Handler();
    h.postDelayed(new Runnable() {
        private float time = 0.0f;
        private float volume = 0.0f;

        @Override
        public void run() {
            _player.start();
            // can call h again after work!
            time += 100;
            volume = (deviceVolume * time) / duration;
            _player.setVolume(volume, volume);
            if (time < duration)
                h.postDelayed(this, 100);
        }
    }, 100); // 1 second delay (takes millis)

}

From source file:mp.teardrop.PlaybackService.java

/**
 * Adjusts volume according to RG information.
 *
 * If rgTrack and rgAlbum are both null, the file which path points to (must be local)
 * will be checked for RG tags. If rgTrack or rgAlbum are provided, path is ignored.
 *//*from  w ww  . jav  a 2s .c om*/
private void applyReplayGain(MediaPlayer mp, Song song) {

    float rgTrack, rgAlbum;

    if (song.rgTrack == null && song.rgAlbum == null) {
        float[] rg = getReplayGainValues(song.path); /* track, album */
        rgTrack = rg[0];
        rgAlbum = rg[1];
    } else {
        rgTrack = song.rgTrack == null ? 0f : song.rgTrack;
        rgAlbum = song.rgAlbum == null ? 0f : song.rgAlbum;
    }

    float adjust = 0f;

    if (mReplayGainAlbumEnabled) {
        adjust = (rgTrack != 0 ? rgTrack : adjust); /* do we have track adjustment ? */
        adjust = (rgAlbum != 0 ? rgAlbum : adjust); /* ..or, even better, album adj? */
    }

    if (mReplayGainTrackEnabled || (mReplayGainAlbumEnabled && adjust == 0)) {
        adjust = (rgAlbum != 0 ? rgAlbum : adjust); /* do we have album adjustment ? */
        adjust = (rgTrack != 0 ? rgTrack : adjust); /* ..or, even better, track adj? */
    }

    if (adjust == 0) {
        /* No RG value found: decrease volume for untagged song if requested by user */
        adjust = (mReplayGainUntaggedDeBump - 150) / 10f;
    } else {
        /* This song has some replay gain info, we are now going to apply the 'bump' value
        ** The preferences stores the raw value of the seekbar, that's 0-150
        ** But we want -15 <-> +15, so 75 shall be zero */
        adjust += 2 * (mReplayGainBump - 75) / 10f; /* 2* -> we want +-15, not +-7.5 */
    }

    if (mReplayGainAlbumEnabled == false && mReplayGainTrackEnabled == false) {
        /* Feature is disabled: Make sure that we are going to 100% volume */
        adjust = 0f;
    }

    float rg_result = ((float) Math.pow(10, (adjust / 20))) * mFadeOut;
    if (rg_result > 1.0f) {
        rg_result = 1.0f; /* android would IGNORE the change if this is > 1 and we would end up with the wrong volume */
    } else if (rg_result < 0.0f) {
        rg_result = 0.0f;
    }
    mp.setVolume(rg_result, rg_result);
}

From source file:github.daneren2005.dsub.service.DownloadService.java

private void applyReplayGain(MediaPlayer mediaPlayer, DownloadFile downloadFile) {
    if (currentPlaying == null) {
        return;/* w  w w  .  j  a  v  a 2s .  com*/
    }

    SharedPreferences prefs = Util.getPreferences(this);
    try {
        float[] rg = BastpUtil
                .getReplayGainValues(downloadFile.getFile().getCanonicalPath()); /* track, album */
        float adjust = 0f;
        if (prefs.getBoolean(Constants.PREFERENCES_KEY_REPLAY_GAIN, false)) {
            boolean singleAlbum = false;

            String replayGainType = prefs.getString(Constants.PREFERENCES_KEY_REPLAY_GAIN_TYPE, "1");
            // 1 => Smart replay gain
            if ("1".equals(replayGainType)) {
                // Check if part of at least <REQUIRED_ALBUM_MATCHES> consequetive songs of the same album

                int index = downloadList.indexOf(downloadFile);
                if (index != -1) {
                    String albumName = downloadFile.getSong().getAlbum();
                    int matched = 0;

                    // Check forwards
                    for (int i = index + 1; i < downloadList.size() && matched < REQUIRED_ALBUM_MATCHES; i++) {
                        if (albumName.equals(downloadList.get(i).getSong().getAlbum())) {
                            matched++;
                        } else {
                            break;
                        }
                    }

                    // Check backwards
                    for (int i = index - 1; i >= 0 && matched < REQUIRED_ALBUM_MATCHES; i--) {
                        if (albumName.equals(downloadList.get(i).getSong().getAlbum())) {
                            matched++;
                        } else {
                            break;
                        }
                    }

                    if (matched >= REQUIRED_ALBUM_MATCHES) {
                        singleAlbum = true;
                    }
                }
            }
            // 2 => Use album tags
            else if ("2".equals(replayGainType)) {
                singleAlbum = true;
            }
            // 3 => Use track tags
            // Already false, no need to do anything here

            // If playing a single album or no track gain, use album gain
            if ((singleAlbum || rg[0] == 0) && rg[1] != 0) {
                adjust = rg[1];
            } else {
                // Otherwise, give priority to track gain
                adjust = rg[0];
            }

            if (adjust == 0) {
                /* No RG value found: decrease volume for untagged song if requested by user */
                int untagged = Integer
                        .parseInt(prefs.getString(Constants.PREFERENCES_KEY_REPLAY_GAIN_UNTAGGED, "0"));
                adjust = (untagged - 150) / 10f;
            } else {
                int bump = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_REPLAY_GAIN_BUMP, "150"));
                adjust += (bump - 150) / 10f;
            }
        }

        float rg_result = ((float) Math.pow(10, (adjust / 20))) * volume;
        if (rg_result > 1.0f) {
            rg_result = 1.0f; /* android would IGNORE the change if this is > 1 and we would end up with the wrong volume */
        } else if (rg_result < 0.0f) {
            rg_result = 0.0f;
        }
        mediaPlayer.setVolume(rg_result, rg_result);
    } catch (IOException e) {
        Log.w(TAG, "Failed to apply replay gain values", e);
    }
}

From source file:github.popeen.dsub.service.DownloadService.java

private void applyReplayGain(MediaPlayer mediaPlayer, DownloadFile downloadFile) {
    if (currentPlaying == null) {
        return;//from ww w .  j  a v a 2s  .  c o  m
    }

    SharedPreferences prefs = Util.getPreferences(this);
    try {
        float adjust = 0f;
        if (prefs.getBoolean(Constants.PREFERENCES_KEY_REPLAY_GAIN, false)) {
            float[] rg = BastpUtil
                    .getReplayGainValues(downloadFile.getFile().getCanonicalPath()); /* track, album */
            boolean singleAlbum = false;

            String replayGainType = prefs.getString(Constants.PREFERENCES_KEY_REPLAY_GAIN_TYPE, "1");
            // 1 => Smart replay gain
            if ("1".equals(replayGainType)) {
                // Check if part of at least <REQUIRED_ALBUM_MATCHES> consequetive songs of the same album

                int index = downloadList.indexOf(downloadFile);
                if (index != -1) {
                    String albumName = downloadFile.getSong().getAlbum();
                    int matched = 0;

                    // Check forwards
                    for (int i = index + 1; i < downloadList.size() && matched < REQUIRED_ALBUM_MATCHES; i++) {
                        if (Util.equals(albumName, downloadList.get(i).getSong().getAlbum())) {
                            matched++;
                        } else {
                            break;
                        }
                    }

                    // Check backwards
                    for (int i = index - 1; i >= 0 && matched < REQUIRED_ALBUM_MATCHES; i--) {
                        if (Util.equals(albumName, downloadList.get(i).getSong().getAlbum())) {
                            matched++;
                        } else {
                            break;
                        }
                    }

                    if (matched >= REQUIRED_ALBUM_MATCHES) {
                        singleAlbum = true;
                    }
                }
            }
            // 2 => Use album tags
            else if ("2".equals(replayGainType)) {
                singleAlbum = true;
            }
            // 3 => Use track tags
            // Already false, no need to do anything here

            // If playing a single album or no track gain, use album gain
            if ((singleAlbum || rg[0] == 0) && rg[1] != 0) {
                adjust = rg[1];
            } else {
                // Otherwise, give priority to track gain
                adjust = rg[0];
            }

            if (adjust == 0) {
                /* No RG value found: decrease volume for untagged song if requested by user */
                int untagged = Integer
                        .parseInt(prefs.getString(Constants.PREFERENCES_KEY_REPLAY_GAIN_UNTAGGED, "0"));
                adjust = (untagged - 150) / 10f;
            } else {
                int bump = Integer.parseInt(prefs.getString(Constants.PREFERENCES_KEY_REPLAY_GAIN_BUMP, "150"));
                adjust += (bump - 150) / 10f;
            }
        }

        float rg_result = ((float) Math.pow(10, (adjust / 20))) * volume;
        if (rg_result > 1.0f) {
            rg_result = 1.0f; /* android would IGNORE the change if this is > 1 and we would end up with the wrong volume */
        } else if (rg_result < 0.0f) {
            rg_result = 0.0f;
        }
        mediaPlayer.setVolume(rg_result, rg_result);
    } catch (IOException e) {
        Log.w(TAG, "Failed to apply replay gain values", e);
    }
}