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:Main.java

public static void runAlarm(Context context, int resourceId) {
    MediaPlayer mediaPlayer = MediaPlayer.create(context, resourceId);
    mediaPlayer.setVolume(1.0f, 1.0f);
    mediaPlayer.start();//ww  w .j  a  va2s.  c  o  m
}

From source file:org.fdroid.enigtext.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context) {
    try {/*from  w  w w . ja  v a  2 s .  c  o  m*/
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

        if (!sp.getBoolean(ApplicationPreferencesActivity.IN_THREAD_NOTIFICATION_PREF, true)) {
            return;
        }

        String ringtone = sp.getString(ApplicationPreferencesActivity.RINGTONE_PREF, null);

        if (ringtone == null)
            return;

        Uri uri = Uri.parse(ringtone);
        MediaPlayer player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setDataSource(context, uri);
        player.setLooping(false);
        player.setVolume(0.25f, 0.25f);
        player.prepare();

        final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));

        audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                audioManager.abandonAudioFocus(null);
            }
        });

        player.start();
    } catch (IOException ioe) {
        Log.w("MessageNotifier", ioe);
    }
}

From source file:com.securecomcode.text.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context) {
    try {/*from www.  ja va  2 s  . c  om*/
        if (!TextSecurePreferences.isInThreadNotifications(context)) {
            return;
        }

        String ringtone = TextSecurePreferences.getNotificationRingtone(context);

        if (ringtone == null)
            return;

        Uri uri = Uri.parse(ringtone);
        MediaPlayer player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setDataSource(context, uri);
        player.setLooping(false);
        player.setVolume(0.25f, 0.25f);
        player.prepare();

        final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));

        audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                audioManager.abandonAudioFocus(null);
            }
        });

        player.start();
    } catch (IOException ioe) {
        Log.w("MessageNotifier", ioe);
    }
}

From source file:org.thoughtcrime.SMP.notifications.MessageNotifier.java

private static void sendInThreadNotification(Context context, Recipients recipients) {
    try {/*from w ww  .  j  a va2s  .  c o  m*/
        if (!TextSecurePreferences.isInThreadNotifications(context)) {
            return;
        }

        Uri uri = recipients.getRingtone();

        if (uri == null) {
            String ringtone = TextSecurePreferences.getNotificationRingtone(context);

            if (ringtone == null) {
                Log.w(TAG, "ringtone preference was null.");
                return;
            } else {
                uri = Uri.parse(ringtone);
            }
        }

        if (uri == null) {
            Log.w(TAG, "couldn't parse ringtone uri " + TextSecurePreferences.getNotificationRingtone(context));
            return;
        }

        MediaPlayer player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setDataSource(context, uri);
        player.setLooping(false);
        player.setVolume(0.25f, 0.25f);
        player.prepare();

        final AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));

        audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                audioManager.abandonAudioFocus(null);
            }
        });

        player.start();
    } catch (IOException ioe) {
        Log.w("MessageNotifier", ioe);
    }
}

From source file:com.thatkawaiiguy.meleehandbook.activity.VideoInfoActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (getIntent().hasExtra("bundle") && savedInstanceState == null)
        savedInstanceState = getIntent().getExtras().getBundle("bundle");
    Preferences.applyTheme(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.collapsing_video_layout);
    Slidr.attach(this);

    bp = new BillingProcessor(this, getResources().getString(R.string.licensekey), this);
    bp.loadOwnedPurchasesFromGoogle();// w ww  .java2  s. c  o  m

    AdView mAdView = (AdView) findViewById(R.id.adView);
    if (!bp.isPurchased(getResources().getString(R.string.adproductid))) {
        mAdView.loadAd(new AdRequest.Builder().build());
        mAdView.setVisibility(View.VISIBLE);
    } else
        mAdView.setVisibility(View.GONE);

    Bundle mainData = getIntent().getExtras();
    if (mainData == null)
        return;
    optionPicked = mainData.getString("option");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(0x00000000);
        NestedScrollView scroll = (NestedScrollView) findViewById(R.id.video_scrollView);
        scroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX,
                    int oldScrollY) {
                if (scrollX < 5)
                    getWindow().setStatusBarColor(0x00000000);
                else
                    getWindow().setStatusBarColor(
                            adjustAlpha(ContextCompat.getColor(getParent(), R.color.theme_accent),
                                    (float) (scrollX + 20) / 10));

            }
        });
    }

    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
    assert getSupportActionBar() != null;
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle(optionPicked);

    infoVid = (MutedVideoView) findViewById(R.id.infoVid);
    infoVid.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.setVolume(0, 0);
            mp.setLooping(true);
        }
    });

    TextView text = (TextView) findViewById(R.id.infoText);
    text.setText(Html.fromHtml(ArrayHelper.getInfoString(optionPicked, this)));
    text.setTextSize(TypedValue.COMPLEX_UNIT_SP, Integer.parseInt(Preferences.getTextSize(this)));
}

From source file:de.lebenshilfe_muenster.uk_gebaerden_muensterland.sign_video_view.AbstractSignVideoFragment.java

protected void setupVideoView(final Sign sign, final SOUND sound, final CONTROLS controls) {
    initializeMediaController();//from   w  w w  .  java2  s  .c om
    final String mainActivityPackageName = getActivity().getPackageName();
    final int signIdentifier = getActivity().getResources().getIdentifier(sign.getName(), RAW,
            mainActivityPackageName);
    if (0 == signIdentifier) {
        throw new VideoSetupException(getActivity().getString(R.string.ASVF_1));
    }
    final Uri uri = Uri.parse(ANDROID_RESOURCE + mainActivityPackageName + SLASH + signIdentifier);
    setVideoViewDimensionToMatchVideoMetadata(this.videoView, uri);
    this.videoView.setVideoURI(uri);
    this.videoView.requestFocus();
    this.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
            AbstractSignVideoFragment.this.progressBar.setVisibility(View.GONE);
            if (sound.equals(SOUND.OFF)) {
                mp.setVolume(0f, 0f);
            }
            AbstractSignVideoFragment.this.videoView.start();
            AbstractSignVideoFragment.this.videoView.setContentDescription(
                    getActivity().getString(R.string.videoIsPlaying) + ": " + sign.getName());
            Log.d(TAG,
                    String.format("Actual width: %s, Actual height: %s",
                            AbstractSignVideoFragment.this.videoView.getWidth(),
                            AbstractSignVideoFragment.this.videoView.getHeight()));
            // Set the MediaController to null so the controls are not 'popping up'
            // when the video plays for the first time.
            AbstractSignVideoFragment.this.videoView.setMediaController(null);
        }
    });
    this.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            if (controls.equals(CONTROLS.SHOW)) {
                initializeMediaController();
            }
        }
    });
}

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  ww  . j  a  va  2s. c o  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:org.schabi.terminightor.NightKillerService.java

private MediaPlayer setupNewMediaPlayer(Alarm alarm) {
    MediaPlayer mediaPlayer = null;
    try {/*from  w  w w .j  a v a  2s .  c o  m*/
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(this, Uri.parse(alarm.getAlarmTone()));
        boolean overrideVolume = PreferenceManager.getDefaultSharedPreferences(this)
                .getBoolean(getString(R.string.overrideAlarmVolume), false);
        if (overrideVolume) {
            mediaPlayer.setVolume(1.0f, 1.0f);
        }
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        mediaPlayer.setLooping(true);
        mediaPlayer.prepare();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mediaPlayer;
}

From source file:net.potterpcs.recipebook.TimerFragment.java

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

    startButton = (Button) getActivity().findViewById(R.id.timerstartbutton);
    stopButton = (Button) getActivity().findViewById(R.id.timerstopbutton);
    display = (TextView) getActivity().findViewById(R.id.timerdisplay);
    minutePicker = (NumberPicker) getActivity().findViewById(R.id.minutepicker);
    secondPicker = (NumberPicker) getActivity().findViewById(R.id.secondpicker);
    timermin = (EditText) getActivity().findViewById(R.id.timerdisplayminutes);
    timersec = (EditText) getActivity().findViewById(R.id.timerdisplayseconds);

    if (minutePicker != null) {
        minutePicker.setMinValue(0);/*www . j av a2s. co  m*/
        minutePicker.setMaxValue(99);
        secondPicker.setMinValue(0);
        secondPicker.setMaxValue(59);
        secondPicker.setFormatter(new Formatter() {
            @Override
            public String format(int value) {
                return String.format("%02d", value);
            }
        });
        getActivity().findViewById(R.id.pickerlayout).setVisibility(View.VISIBLE);
        display.setVisibility(View.GONE);
    }

    startButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int seconds;
            if (minutePicker != null) {
                seconds = minutePicker.getValue() * 60 + secondPicker.getValue();
            } else {
                int m = Integer.parseInt(timermin.getEditableText().toString());
                int s = Integer.parseInt(timersec.getEditableText().toString());
                seconds = m * 60 + s;
                timermin.setText(timermin.getText(), BufferType.NORMAL);
                timersec.setText(timersec.getText(), BufferType.NORMAL);
            }
            startButton.setEnabled(false);
            stopButton.setEnabled(true);

            if (minutePicker == null) {
                timermin.setEnabled(false);
                timersec.setEnabled(false);
            } else {
                minutePicker.setEnabled(false);
                secondPicker.setEnabled(false);
                getActivity().findViewById(R.id.pickerlayout).setVisibility(View.GONE);
                display.setVisibility(View.VISIBLE);
            }

            if (display != null) {
                display.setText(DateUtils.formatElapsedTime(seconds));
            }

            timer = new CountDownTimer(seconds * 1000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    //                  Log.i(TAG, "tick " + millisUntilFinished);
                    if (display != null) {
                        display.setText(DateUtils.formatElapsedTime(millisUntilFinished / 1000));
                    } else {
                        int totalsec = (int) (millisUntilFinished / 1000);
                        int s = totalsec % 60;
                        int m = totalsec / 60;
                        timermin.setText(String.format("%02d", m));
                        timersec.setText(String.format("%02d", s));
                    }
                }

                @Override
                public void onFinish() {
                    // Play a sound and show a message when the timer ends
                    MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.ding);
                    mp.setOnCompletionListener(new OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            mp.release();
                        }
                    });
                    mp.setVolume(1.0f, 1.0f);
                    Toast.makeText(getActivity(), "Done!", Toast.LENGTH_LONG).show();
                    mp.start();
                    clearToZero();
                }
            };

            timer.start();
        }
    });

    stopButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            timer.cancel();
            clearToZero();
        }
    });
}

From source file:org.cocos2dx.lib.Cocos2dxMusic.java

/**
 * create mediaplayer for music/*from  w w  w  . j a v  a2 s.  c o  m*/
 * @param path the path relative to assets
 * @return 
 */
private MediaPlayer createMediaplayerFromAssets(String path) {
    MediaPlayer mediaPlayer = new MediaPlayer();

    try {
        if (path.startsWith("/")) {
            mediaPlayer.setDataSource(path);
        } else {
            AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path);
            mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(),
                    assetFileDescritor.getStartOffset(), assetFileDescritor.getLength());
        }

        mediaPlayer.prepare();

        mediaPlayer.setVolume(mLeftVolume, mRightVolume);
    } catch (Exception e) {
        mediaPlayer = null;
        Log.e(TAG, "error: " + e.getMessage(), e);
    }

    return mediaPlayer;
}