如何正确设置MediaPlayer的音频流类型 [英] How to correctly set MediaPlayer audio stream type

查看:923
本文介绍了如何正确设置MediaPlayer的音频流类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个方式来调整音量设置为每个不同的数据流(媒体,通知,铃声等)和有办法preVIEW每个流的输出音量。我相信我有正确执行,但是当我设置输出流类型,没有任何声音播放。

I'm trying to create a way to adjust volume settings for each of the different streams (media, notification, ringtone, etc) and have a way to preview the output sound level of each stream. I believe I have the correct implementation, but when I set the output stream type, there is no sound that plays.

下面是code,它可以正常播放用户选择的报警声:

Here is the code that correctly plays the user's selected alarm sound:

Uri mediaUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
MediaPlayer mp=MediaPlayer.create(getApplicationContext(), mediaUri);
//mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.start();`

这注释掉行是什么原因造成了我的问题。我想听到警报声在不同的音频流的音量电平,但是当我包括行STREAM_ALARM或任何其他音频流,根本没有声音播放。任何想法可能是怎么回事?

That commented out line is what is causing me problems. I would like to hear the alarm sound at the volume levels of the different audio streams, but when I include that line for STREAM_ALARM or any other audio stream, no sound at all plays. Any ideas what could be going on here?

推荐答案

好吧,我发现后,更多的测试解决方案,它看起来像这样,万一别人运行到同样的问题,我有。该MODIFY_AUDIO_SETTINGS权限需要在清单中的这个工作。

Okay, I found the solution after a bit more testing and it looks like this, in case anyone else runs into the same problem I was having. The MODIFY_AUDIO_SETTINGS permission is needed in the Manifest for this to work.

AudioManager am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_NORMAL);
MediaPlayer mp=new MediaPlayer();
Uri ringtoneUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
try
{
    mp.setDataSource(getApplicationContext(), ringtoneUri);
    mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
    mp.prepare();
    mp.start();
}
catch(Exception e)
{
    //exception caught in the end zone
}

这篇关于如何正确设置MediaPlayer的音频流类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆