通过AudioManager进行音频设置访问会导致其他应用的音量减少?

时间:2017-12-26 11:54:47

标签: android audio android-audiomanager android-mediarecorder

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

我们使用此foreground service进行录音。要发出哔哔声,我们使用扬声器。我们在播放蜂鸣声之前启用手机扬声器,并在完全播放蜂鸣声时禁用,如下所示:

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

我在Manifest中拥有以下权限:

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

2 个答案:

答案 0 :(得分:1)

请尝试以下播放哔声

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 :(得分:0)

我已关注Akhils answer,但稍作修改:

更改1:从构造函数中删除了以下行,因为我想在服务中使用它。

activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);

更改2:因为我希望播放此哔声作为通知声音,我更改了mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);行到mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);

因此,赏金归于Akhil:)