Java Code Examples for android.media.MediaPlayer#setAudioAttributes()

The following examples show how to use android.media.MediaPlayer#setAudioAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IncomingRinger.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private MediaPlayer createPlayer(@NonNull Uri ringtoneUri) {
  try {
    MediaPlayer mediaPlayer = new MediaPlayer();

    mediaPlayer.setOnErrorListener(new MediaPlayerErrorListener());
    mediaPlayer.setDataSource(context, ringtoneUri);
    mediaPlayer.setLooping(true);

    if (Build.VERSION.SDK_INT <= 21) {
      mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
    } else {
      mediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
                                                        .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                                                        .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION_SIGNALLING)
                                                        .build());
    }

    return mediaPlayer;
  } catch (IOException e) {
    Log.e(TAG, "Failed to create player for incoming call ringer");
    return null;
  }
}
 
Example 2
Source File: KcaUtils.java    From kcanotify_h5-master with GNU General Public License v3.0 6 votes vote down vote up
public static void playNotificationSound(MediaPlayer mediaPlayer, Context context, Uri uri) {
    try {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.reset();
        }
        if (uri != null && !Uri.EMPTY.equals(uri)) {
            mediaPlayer.setDataSource(context, uri);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                AudioAttributes attr = new AudioAttributes.Builder()
                        .setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)
                        .build();
                mediaPlayer.setAudioAttributes(attr);
            } else {
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
            }
            mediaPlayer.prepare();
            mediaPlayer.start();
        }
    } catch (IllegalArgumentException | SecurityException | IllegalStateException | IOException e) {
        e.printStackTrace();
    }
}
 
Example 3
Source File: KcaUtils.java    From kcanotify with GNU General Public License v3.0 6 votes vote down vote up
public static void playNotificationSound(MediaPlayer mediaPlayer, Context context, Uri uri) {
    try {
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.reset();
        }
        if (uri != null && !Uri.EMPTY.equals(uri)) {
            mediaPlayer.setDataSource(context, uri);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                AudioAttributes attr = new AudioAttributes.Builder()
                        .setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)
                        .build();
                mediaPlayer.setAudioAttributes(attr);
            } else {
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
            }
            mediaPlayer.prepare();
            mediaPlayer.start();
        }
    } catch (IllegalArgumentException | SecurityException | IllegalStateException | IOException e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: PlaybackView.java    From justaline-android with Apache License 2.0 5 votes vote down vote up
private void prepareMediaPlayer(Uri uri) {
    Log.d(TAG, "prepareMediaPlayer: " + uri);
    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setDataSource(getContext(), uri);
        mMediaPlayer.setSurface(mSurface);
        mMediaPlayer.setAudioAttributes(mAudioAttributes);
        mMediaPlayer.setLooping(true);
        mMediaPlayer.prepare();
    } catch (IOException | IllegalStateException e) {
        Log.e(TAG, "prepareMediaPlayer: ", e);
    }
}
 
Example 5
Source File: soundPlayer.java    From Game with GNU General Public License v3.0 5 votes vote down vote up
public static void playSoundFile(String key) {
    try {
        if (!orsc.mudclient.optionSoundDisabled) {
            File sound = orsc.mudclient.soundCache.get(key + ".wav");
            if (sound == null)
                return;
            try {
                MediaPlayer player = new MediaPlayer();
                AudioAttributes audioAttrib = new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_MEDIA)
                        .build();
                player.setDataSource(sound.getPath());
                player.setAudioAttributes(audioAttrib);
                player.setLooping(false);
                player.prepare();
                player.start();
                player.setOnCompletionListener(MediaPlayer::release);

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

    } catch (RuntimeException ignored) {
    }
}
 
Example 6
Source File: MediaPlayerCompatImplL.java    From android-openslmediaplayer with Apache License 2.0 5 votes vote down vote up
@Override
void setAudioAttributes(MediaPlayer instance, AudioAttributes attributes) {
    android.media.AudioAttributes attributes2;

    attributes2 = (new android.media.AudioAttributes.Builder())
            .setContentType(attributes.getContentType())
            .setUsage(attributes.getUsage())
            .setFlags(attributes.getFlags())
            .build();

    instance.setAudioAttributes(attributes2);
}