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

        }
    }
}

關鍵代碼就這些,要完整代碼的,私信我。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章