gpt4 book ai didi

android - 通过 AudioManager 访问音频设置会导致其他应用程序的音量降低?

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:55 25 4
gpt4 key购买 nike

在我正在开发的应用程序中,当前台服务发生更新时,我们必须通过蜂鸣声通知用户。

我们使用这个 foreground service用于录音。要播放哔哔声,我们使用扬声器。我们在播放 beep 之前启用电话扬声器,并在 beep 完全播放时禁用,如下所示:

    stopSoundPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
SpeakerManager.get().disable();
.....
}
});
SpeakerManager.get().enable();
stopSoundPlayer.start();

我在 list 中有以下权限:

// 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" />

运行时录音权限管理得当。

但是当我的应用程序安装在任何手机上时,它会自动降低所有其他正在播放音频的应用程序的音量。当我卸载我的应用程序时,音频音量会恢复。可能是什么原因造成的?

下面是我的 SpeakerManager 类供引用:

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();
}
}
}

关于android - 通过 AudioManager 访问音频设置会导致其他应用程序的音量降低?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47978295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com