开发者

Android: How do you play a sound at a volume unaffected by any of the Android system volumes (ringtone, media, etc)?

I'd like to play a sound from a (ringtone) URI in my Android application. However, I would like to play this sound at an arbitrary volume and not have system volu开发者_运维知识库mes, such as Media and Ringtone volume, affect playback.

I've been paging through the API docs, but I'm at a loss. Is this possible with Android and if so, how would I do it?


I believe most apps aiming to do this choose a stream type, stash the current volume level (using AudioManager.getStreamVolume), set that volume to whatever they want it to be (using setStreamVolume() to some percentage of getStreamMaxVolume), and then restore the stashed volume level after playback completes. But think hard before you do this -- unless you're creating a "find my lost phone" app, there's rarely a reason to ignore the user's requested volume levels, and you run a high risk of irritating someone silencing their phone for a meeting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜