2015-12-02 39 views
2

我是全新的編碼,事實上我不是一個編碼器,但試圖解決的問題,當一個耳機連接,仍然我想使用耳機。Android:強制音頻到耳機>> setDeviceConnectionState失敗:java.lang.NoSuchMethodException:

經過幾天的搜索,我偶然發現了一個樣本(http://oneyoung.im/2014/12/26/force-route-audio-stream-to-headphone/)。

我下載並安裝了Android Studio,並試過上課。但我得到一個錯誤。

12-02 13:28:48.652 6475-6475/com.free.alahdal.stopheadset E/AudioTest: setDeviceConnectionState failed: java.lang.NoSuchMethodException: setDeviceConnectionState [int, int, class java.lang.String] 

以下是MyActivity.java & AudioTest.java

MyActivity:

package com.free.alahdal.stopheadset; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 


public class MyActivity extends AppCompatActivity { 
    AudioTest at = new AudioTest(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_my); 
    } 


    public void offHeadset(View view) { 
     at.forceRouteHeadset(false); 
    } 

    public void onHeadset(View view) { 
     at.forceRouteHeadset(true); 
    } 

} 

下面是AudioTest

import android.util.Log; 
import java.lang.reflect.Method; 

public class AudioTest { 
    private final String TAG = "AudioTest"; 
    // Constants copied from AudioSystem 
    private static final int DEVICE_IN_WIRED_HEADSET = 0x400000; 
    private static final int DEVICE_OUT_EARPIECE  = 0x1; 
    private static final int DEVICE_OUT_WIRED_HEADSET = 0x4; 
    private static final int DEVICE_STATE_UNAVAILABLE = 0; 
    private static final int DEVICE_STATE_AVAILABLE  = 1; 

    /* force route function through AudioSystem */ 
    private void setDeviceConnectionState(final int device, final int state, final String address) { 
     try { 
      Class<?> audioSystem = Class.forName("android.media.AudioSystem"); 
      Method setDeviceConnectionState = audioSystem.getMethod(
        "setDeviceConnectionState", int.class, int.class, String.class); 

      setDeviceConnectionState.invoke(audioSystem, device, state, address); 
     } catch (Exception e) { 
      Log.e(TAG, "setDeviceConnectionState failed: " + e); 
     } 
    } 

    public void forceRouteHeadset(boolean enable) { 
     if (enable) { 
      Log.i(TAG, "force route to Headset"); 
      setDeviceConnectionState(DEVICE_IN_WIRED_HEADSET, DEVICE_STATE_AVAILABLE, ""); 
      setDeviceConnectionState(DEVICE_OUT_WIRED_HEADSET, DEVICE_STATE_AVAILABLE, ""); 
     } else { 
      Log.i(TAG, "force route to Earpirce"); 
      setDeviceConnectionState(DEVICE_IN_WIRED_HEADSET, DEVICE_STATE_UNAVAILABLE, ""); 
      setDeviceConnectionState(DEVICE_OUT_WIRED_HEADSET, DEVICE_STATE_UNAVAILABLE, ""); 
      setDeviceConnectionState(DEVICE_OUT_EARPIECE, DEVICE_STATE_AVAILABLE, ""); 
     } 
    } 
} 
+0

*我是全新的編碼*然後**不使用黑客**,你不明白...只是這**黑客**使用反射來獲得一些類,並調用一些方法**這是不是Android API的一部分** ...這個類(和/或方法)可能存在也可能不存在於設備上......在你的情況下(對於給定的設備/模擬器)這個類確實存在,但沒有給出方法... – Selvin

+0

您的意思是代碼是正確的,但在我的設備中不起作用? – Alahdal

+0

我不會將「使用未公開的API」稱爲正確的代碼......但是,它可能適用於某些(大多數是較舊的)設備... – Selvin

回答

3

這裏是你要使用反射方法。

/** 
* Indicate wired accessory connection state change. 
* @param device type of device connected/disconnected (AudioManager.DEVICE_OUT_xxx) 
* @param state new connection state: 1 connected, 0 disconnected 
* @param name device name 
* {@hide} 
*/ 
public void setWiredDeviceConnectionState(int type, int state, String address, String name) { 
    IAudioService service = getService(); 
    try { 
     service.setWiredDeviceConnectionState(type, state, address, name, 
       mApplicationContext.getOpPackageName()); 
    } catch (RemoteException e) { 
     Log.e(TAG, "Dead object in setWiredDeviceConnectionState "+e); 
    } 
} 

此方法使用四個參數(以上kitkat版本)和三個參數直到kitkat。 你正在使用的方法不在api中,這就是爲什麼它拋出這樣的異常。 如果有幫助,不要忘記接受。

相關問題