Example usage for android.media AudioManager STREAM_MUSIC

List of usage examples for android.media AudioManager STREAM_MUSIC

Introduction

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

Prototype

int STREAM_MUSIC

To view the source code for android.media AudioManager STREAM_MUSIC.

Click Source Link

Document

Used to identify the volume of audio streams for music playback

Usage

From source file:Main.java

public static void setVivaVolume(Context context, int volume) {
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    am.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
    getPreferences(context).edit().putInt(VivaVolume, volume).apply();
}

From source file:Main.java

public static int getVivaVolume(Context context) {
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    int amStreamMusicVol = am.getStreamVolume(AudioManager.STREAM_MUSIC);
    return getPreferences(context).getInt(VivaVolume, amStreamMusicVol);
}

From source file:Main.java

/**
 * Utility method that/*ww w.jav a 2 s  . c o  m*/
 * @param am the AudioManager requesting the focus
 * @param afChangeListener the foucs listener associated to the AudioManager
 * @return true if focus is granted, false if not.
 */
public static boolean focusRequestGranted(AudioManager am, Object afChangeListener) {
    int result = am.requestAudioFocus((AudioManager.OnAudioFocusChangeListener) afChangeListener,
            AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    return result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}

From source file:Main.java

public static void adjustMusicVolume(Context context, boolean up, boolean showInterface) {
    int direction = up ? AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER;
    int flag = AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE | (showInterface ? AudioManager.FLAG_SHOW_UI : 0);
    getInstance(context).adjustStreamVolume(AudioManager.STREAM_MUSIC, direction, flag);
}

From source file:Main.java

public static void showDialogOk(String title, String message, Context context) {
    if (context != null) {
        Dialog dlg = new AlertDialog.Builder(context).setTitle(title).setMessage(message)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }// w w w  .  ja v  a  2  s.co  m
                }).create();
        dlg.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        dlg.show();
    }
}

From source file:Main.java

public static AudioTrack createTrack(int samplingRate) {
    AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, samplingRate, AudioFormat.CHANNEL_OUT_STEREO,
            AudioFormat.ENCODING_DEFAULT, samplingRate, AudioTrack.MODE_STREAM);
    return track;
}

From source file:Main.java

public static void playSound(String filePath, MediaPlayer.OnCompletionListener listener) {
    if (sMediaPlayer == null) {
        sMediaPlayer = new MediaPlayer();
        sMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override/*from w ww  .ja v a 2  s. c om*/
            public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
                sMediaPlayer.reset();
                return false;
            }
        });
    } else {
        sMediaPlayer.reset();
    }
    try {
        sMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        if (listener != null) {
            sMediaPlayer.setOnCompletionListener(listener);
        }
        sMediaPlayer.setDataSource(filePath);
        sMediaPlayer.prepare();
        sMediaPlayer.start();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void playSound(String filePath, MediaPlayer.OnCompletionListener onCompletionListener) {
    if (mMediaPlayer == null) {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override//from w  w w.  j  av a 2s.c o  m
            public boolean onError(MediaPlayer mp, int what, int extra) {
                mMediaPlayer.reset();
                return false;
            }
        });
    } else {
        mMediaPlayer.reset();
    }
    try {
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        if (onCompletionListener != null) {
            mMediaPlayer.setOnCompletionListener(onCompletionListener);
        }

        mMediaPlayer.setDataSource(filePath);
        mMediaPlayer.prepare();
        mMediaPlayer.start();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void showDialogOkWithGoBack(String title, String message, final Activity activity) {
    if (activity.getApplicationContext() != null) {
        AlertDialog.Builder adb = new AlertDialog.Builder(activity);
        adb.setTitle(title);//  ww w.j  a  va  2 s.c  o m
        adb.setMessage(message);
        adb.setCancelable(false);
        adb.setNeutralButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                //activity.onBackPressed();
            }
        });
        AlertDialog ad = adb.create();
        ad.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        ad.show();
    }
}

From source file:Main.java

public static synchronized void init() {
    if (soundPool == null)
        soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
}