通过 AudioManager 访问音频设置会导致其他应用程序的音量降低?
Audio settings access via AudioManager causes decrease in volume for other apps?
在我正在开发的应用程序中,当前台服务发生更新时,我们必须通过蜂鸣声通知用户。
我们使用此 foreground service 进行录音。要播放哔哔声,我们使用扬声器。我们在播放 beep 之前启用 phones 扬声器并在 beep 完全播放时禁用,如下所示:
stopSoundPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
SpeakerManager.get().disable();
.....
}
});
SpeakerManager.get().enable();
stopSoundPlayer.start();
我在清单中拥有以下权限:
// Audio related permissions
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
// Other permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
运行时录音权限管理得当
但是当我的应用程序安装在任何 phone 上时,它会自动降低所有其他正在播放音频的应用程序的音量。当我卸载我的应用程序时,音频音量会恢复。这可能是什么原因造成的?
下面是我的 SpeakerManager class 供参考:
public class SpeakerManager {
private static SpeakerManager instance;
private final AudioManager audioManager;
private SpeakerManager(Context context) {
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.STREAM_MUSIC);
}
public static void init(Context context) {
instance = new SpeakerManager(context);
}
public static SpeakerManager get() {
if (instance == null) {
throw new IllegalStateException("SpeakerManager should be initialized by calling init()");
}
return instance;
}
public void enable() {
if (null != audioManager) {
audioManager.setSpeakerphoneOn(true);
}
}
public void disable() {
if (null != audioManager) {
audioManager.setSpeakerphoneOn(false);
}
}
}
尝试下面播放提示音
public final class BeepManager {
private static final String TAG = BeepManager.class.getSimpleName();
private static final float BEEP_VOLUME = 0.10f;
private final Context context;
public BeepManager(Activity activity) {
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// We do not keep a reference to the Activity itself, to prevent leaks
this.context = activity.getApplicationContext();
}
public void playBeepSound() {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (am != null) {
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
case AudioManager.RINGER_MODE_VIBRATE:
return;
}
}
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.w(TAG, "Failed to beep " + what + ", " + extra);
// possibly media player error, so release and recreate
mp.stop();
mp.release();
return true;
}
});
try {
AssetFileDescriptor file = context.getResources().openRawResourceFd(R.raw.beep_sound);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
} finally {
file.close();
}
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException ioe) {
Log.w(TAG, ioe);
mediaPlayer.release();
}
}
}
我已经关注 ,但有一些小改动:
更改 1:从构造函数中删除了下面一行,因为我想在服务中使用它。
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
更改 2:因为我想播放此提示音作为通知声音,所以我将以下行更改为 mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
到 mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
因此赏金给了 Akhil :)
在我正在开发的应用程序中,当前台服务发生更新时,我们必须通过蜂鸣声通知用户。
我们使用此 foreground service 进行录音。要播放哔哔声,我们使用扬声器。我们在播放 beep 之前启用 phones 扬声器并在 beep 完全播放时禁用,如下所示:
stopSoundPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
SpeakerManager.get().disable();
.....
}
});
SpeakerManager.get().enable();
stopSoundPlayer.start();
我在清单中拥有以下权限:
// Audio related permissions
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
// Other permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
运行时录音权限管理得当
但是当我的应用程序安装在任何 phone 上时,它会自动降低所有其他正在播放音频的应用程序的音量。当我卸载我的应用程序时,音频音量会恢复。这可能是什么原因造成的?
下面是我的 SpeakerManager class 供参考:
public class SpeakerManager {
private static SpeakerManager instance;
private final AudioManager audioManager;
private SpeakerManager(Context context) {
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.STREAM_MUSIC);
}
public static void init(Context context) {
instance = new SpeakerManager(context);
}
public static SpeakerManager get() {
if (instance == null) {
throw new IllegalStateException("SpeakerManager should be initialized by calling init()");
}
return instance;
}
public void enable() {
if (null != audioManager) {
audioManager.setSpeakerphoneOn(true);
}
}
public void disable() {
if (null != audioManager) {
audioManager.setSpeakerphoneOn(false);
}
}
}
尝试下面播放提示音
public final class BeepManager {
private static final String TAG = BeepManager.class.getSimpleName();
private static final float BEEP_VOLUME = 0.10f;
private final Context context;
public BeepManager(Activity activity) {
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// We do not keep a reference to the Activity itself, to prevent leaks
this.context = activity.getApplicationContext();
}
public void playBeepSound() {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (am != null) {
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
case AudioManager.RINGER_MODE_VIBRATE:
return;
}
}
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.w(TAG, "Failed to beep " + what + ", " + extra);
// possibly media player error, so release and recreate
mp.stop();
mp.release();
return true;
}
});
try {
AssetFileDescriptor file = context.getResources().openRawResourceFd(R.raw.beep_sound);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
} finally {
file.close();
}
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException ioe) {
Log.w(TAG, ioe);
mediaPlayer.release();
}
}
}
我已经关注
更改 1:从构造函数中删除了下面一行,因为我想在服务中使用它。
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
更改 2:因为我想播放此提示音作为通知声音,所以我将以下行更改为 mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
到 mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
因此赏金给了 Akhil :)