简体   繁体   English

在Android中处理AudioManager的最佳方法

[英]Best way to handle AudioManager in android

I'm wondering, what is the best way to handle the AudioManager in Android, for media. 我想知道,对于媒体来说,处理Android中AudioManager的最佳方法是什么。 As you know, AudiManager manage different type of audio, like music or ringtone. 如您所知,AudiManager管理不同类型的音频,例如音乐或铃声。

If I have an applicaiton playing sound effects and vibrating, with 3 activity, how do I have to handle this class ? 如果我有一个玩声音效果和振动的应用程序,并且有3个活动,那么我该如何处理此类课程?

-Do I have to set the volume on the onCreate method in each activity ? -是否必须在每个活动的onCreate方法上设置音量? Then use it 然后用

audioManager.setVolumeControlStream(AudioManager.STREAM_MUSIC);

-Or do I have to make a static reference, and use it each time ? -还是我必须做一个静态引用,并每次使用?

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

-Or is these wrong, and I didn't understand how it works ? -或者这些是错误的,我不知道它是如何工作的?

To tell the truth, I'm having problem handling this im my 3 activities : in the first 2 activities, I have sound effect, and in the 3rd one, only vibration. 说实话,我在3个活动中遇到问题:在前2个活动中,我有声音效果,在第3个活动中,只有振动。 In the first activity, the sound is in "Ringtone mode", and the other two "Media mode". 在第一个活动中,声音处于“铃声模式”,其他两个处于“媒体模式”。 So the sound volume is different which is bad. 因此音量不同,这是不好的。

In order to use the AudioManager you have to have an instance of it (in every Activity or Service you want to use it) so: 为了使用AudioManager您必须拥有它的一个实例(在您要使用它的每个Activity或Service中),因此:

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 

then you can use it: 那么您可以使用它:

audioManager.setVolumeControlStream(AudioManager.STREAM_MUSIC);

This doesn't need to be called inside the OnCreate() method but need to be called before you play the sound. 不需要在OnCreate()方法中调用此方法,而需要在播放声音之前调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM