MediaPlayer - setAudioAttributes 无法正常工作
MediaPlayer - setAudioAttributes not working properly
我正在尝试创建警报,一切正常,但流类型始终是媒体,即使我使用 STREAM_ALARM
,因为 setStreamType
已弃用,我正在使用 setAudioAttributes
而不是,但它似乎不起作用。
这是我的代码:
class AlarmRingtoneManager(val context: Context) {
private lateinit var mediaPlayer: MediaPlayer
fun start() {
mediaPlayer = MediaPlayer.create(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM))
.apply {
setAudioAttributes(AudioAttributes.Builder()
.setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
.setLegacyStreamType(AudioManager.STREAM_ALARM)
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build())
isLooping = true
start()
}
}
fun stop() {
mediaPlayer.stop()
}
}
问题是您正在使用 MediaPlayer.create()
方法创建 MediaPlayer
,如果您那样做,以后将无法更改 AudioAttributes
。
Convenience method to create a MediaPlayer for a given resource id. On
success, prepare() will already have been called and must not be
called again.
When done with the MediaPlayer, you should call release(), to free the
resources. If not released, too many MediaPlayer instances will result
in an exception.
Note that since prepare() is called automatically in this method, you
cannot change the audio session ID (see setAudioSessionId(int)) or
audio attributes (see
setAudioAttributes(android.media.AudioAttributes) of the new
MediaPlayer.
不使用 create()
,只需使用默认构造函数 new MediaPlayer();
实例化 MediaPlayer
。然后,使用 setDataSource()
方法设置源并像之前一样设置其余的 AudioAttributes。
我不知道 Kotlin,但在 Java 中它看起来像这样:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioAttributes(AudioAttributes.Builder()
.setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
.setLegacyStreamType(AudioManager.STREAM_ALARM)
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build());
mediaPlayer.setDataSource(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
mediaPlayer.setLooping(true);
mediaPlayer.prepare();
mediaPlayer.start();
Kotlin 版本
val uri = Settings.System.DEFAULT_ALARM_ALERT_URI ?: Settings.System.DEFAULT_RINGTONE_URI
uri?.let {
val player = MediaPlayer()
player.setAudioAttributes(
AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
)
//player.reset() //uncomment this line if you use local variable of MediaPlayer
player.setDataSource(this@AlarmActivity, it)
player.prepare()
player.start()
}
我正在尝试创建警报,一切正常,但流类型始终是媒体,即使我使用 STREAM_ALARM
,因为 setStreamType
已弃用,我正在使用 setAudioAttributes
而不是,但它似乎不起作用。
这是我的代码:
class AlarmRingtoneManager(val context: Context) {
private lateinit var mediaPlayer: MediaPlayer
fun start() {
mediaPlayer = MediaPlayer.create(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM))
.apply {
setAudioAttributes(AudioAttributes.Builder()
.setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
.setLegacyStreamType(AudioManager.STREAM_ALARM)
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build())
isLooping = true
start()
}
}
fun stop() {
mediaPlayer.stop()
}
}
问题是您正在使用 MediaPlayer.create()
方法创建 MediaPlayer
,如果您那样做,以后将无法更改 AudioAttributes
。
Convenience method to create a MediaPlayer for a given resource id. On success, prepare() will already have been called and must not be called again.
When done with the MediaPlayer, you should call release(), to free the resources. If not released, too many MediaPlayer instances will result in an exception.
Note that since prepare() is called automatically in this method, you cannot change the audio session ID (see setAudioSessionId(int)) or audio attributes (see setAudioAttributes(android.media.AudioAttributes) of the new MediaPlayer.
不使用 create()
,只需使用默认构造函数 new MediaPlayer();
实例化 MediaPlayer
。然后,使用 setDataSource()
方法设置源并像之前一样设置其余的 AudioAttributes。
我不知道 Kotlin,但在 Java 中它看起来像这样:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioAttributes(AudioAttributes.Builder()
.setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
.setLegacyStreamType(AudioManager.STREAM_ALARM)
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build());
mediaPlayer.setDataSource(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
mediaPlayer.setLooping(true);
mediaPlayer.prepare();
mediaPlayer.start();
Kotlin 版本
val uri = Settings.System.DEFAULT_ALARM_ALERT_URI ?: Settings.System.DEFAULT_RINGTONE_URI
uri?.let {
val player = MediaPlayer()
player.setAudioAttributes(
AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build()
)
//player.reset() //uncomment this line if you use local variable of MediaPlayer
player.setDataSource(this@AlarmActivity, it)
player.prepare()
player.start()
}