我在编码方面是全新的,事实上我不是编码器,而是试图解决连接耳机时仍然想要使用耳机的问题。
经过几天的搜索,我从这里看到了一个样本(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 :(得分: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中,这就是为什么它会抛出这样的异常。 如果它有帮助,别忘了接受。