Android开发暂停其他app的音频播放

其实你是没办法控制其他app,只是把audio这个服务抢过来。具体原理还没搞清楚。先提供代码给你们,能用最好。

public void stopOtherAppVoice(final Context context){
    this.mAudioManager = (AudioManager)context.getApplicationContext().getSystemService("audio");
    if (!this.isHeadphonesPlugged(this.mAudioManager)) {
        this._sensorManager = (SensorManager)context.getApplicationContext().getSystemService("sensor");
        if (this._sensorManager != null) {
            this._sensor = this._sensorManager.getDefaultSensor(8);
            this._sensorManager.registerListener(this, this._sensor, 3);
        }
    }

    this.muteAudioFocus(this.mAudioManager, true);
}

@TargetApi(8)
private void muteAudioFocus(AudioManager audioManager, boolean bMute) {
    synchronized(this.mLock) {
        if (audioManager != null) {
            if (bMute) {
                audioManager.requestAudioFocus(this.afChangeListener, 3, 2);
            } else {
                audioManager.abandonAudioFocus(this.afChangeListener);
                this.afChangeListener = null;
            }

        }
    }
}

关键代码就这些,要完整代码的,私信我。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容