Android MediaPlayer - how to play in the STREAM_ALARM?

前端 未结 5 876
南旧
南旧 2020-12-16 16:32

I\'ve tried settings the audio stream of the media player in my application using the following code but when I do this I hear no sound in the emulator. If I don\'t set the

5条回答
  •  不思量自难忘°
    2020-12-16 16:57

    The solution here was deprecated in API 22

    I opened my own thread to figure this out.

    Here is an updated working solution.

    mediaPlayerScan = new MediaPlayer();
    try {
      mediaPlayerScan.setDataSource(getContext(),
              Uri.parse(getString(R.string.res_path) + R.raw.scan_beep));
    
      if (Build.VERSION.SDK_INT >= 21) {
        mediaPlayerScan.setAudioAttributes(new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_ALARM)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build());
      } else {
        mediaPlayerScan.setAudioStreamType(AudioManager.STREAM_ALARM);
      }
      mediaPlayerScan.prepare();
    } catch (IOException e) {
      e.printStackTrace();
    }
    

提交回复
热议问题