我正在尝试使用三星音频SDK创建一个服务,没有UI将声音从麦克风重定向到耳机插孔:http://developer.samsung.com/galaxy/professional-audio
目前,此代码已成功找到麦克风和耳机,但我无法听到耳机上的任何声音。这个SDK没有很多例子,所以我很难理解,还需要做些什么才能让它发挥作用。我需要音频通过延迟,因为Android设备本身允许。也许有人可以建议另一个SDK,或者如何让实时音频通过? 这是我目前的代码:
public class MainActivityBck extends Activity {
public static String string = null;
SapaPortConnection sapaPortConnection = null;
Activity activity = null;
BroadcastReceiver broadcastReceiver = new HeadsetStatusClass();
private boolean initializedSapaBoolean = true;
NotificationManager notificationManager = null;
Sapa sapa = null;
SapaService sapaService = null;
SapaProcessor sapaProcessor = null;
class HeadsetStatusClass extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.HEADSET_PLUG")) {
switch (intent.getIntExtra("state", -1)) {
case 0:
WriteToDebugLog.write("DEBUG", "headphone unplugged");
Log.d("headphones", "disconnected");
case 1:
Log.d("headphones", "connected");
setVolume(12);
}
} else if (intent.getAction().equals("android.intent.action.SCREEN_ON")) {
} else if (intent.getAction().equals("android.intent.action.SCREEN_OFF") && sapaProcessor != null){
}
}
}
private void setVolume(int i) {
if (this.sapaProcessor != null) {
int i2 = (i * 2) - 27;
if (i2 >= -27 && i2 <= 3) {
WriteToDebugLog.write("DEBUG", "sendVolume: volume" + i + "/ nativeVolume: " + i2);
try {
this.sapaProcessor.sendCommand("/volume:" + i2);
} catch (Exception e) {
WriteToDebugLog.write("DEBUG", e.toString());
}
}
}
}
private void setEffectSapa() {
if (this.sapaProcessor != null) {
this.sapaProcessor.sendCommand("/seteffecttype:" + 0);
}
}
private void setCustomFiltersSapa() {
if (this.sapaProcessor != null) {
this.sapaProcessor.sendCommand("/finaldelay:" + 0);
this.sapaProcessor.sendCommand("/finalfeedback:" + 50);
this.sapaProcessor.sendCommand("/drymix:" + 40);
this.sapaProcessor.sendCommand("/wetmix:" + 40);
this.sapaProcessor.sendCommand("/subdelay:" + 100);
this.sapaProcessor.sendCommand("/subfeedback:" + 40);
this.sapaProcessor.sendCommand("/subdecay:" + 30);
this.sapaProcessor.sendCommand("/subdelay2:" + 0);
this.sapaProcessor.sendCommand("/subfeedback2:" + 40);
this.sapaProcessor.sendCommand("/subdecay2:" + 30);
this.sapaProcessor.sendCommand("/subdelay3:" + 115);
this.sapaProcessor.sendCommand("/subfeedback3:" + 40);
this.sapaProcessor.sendCommand("/subdecay3:" + 30);
this.sapaProcessor.sendCommand("/subdelay4:" + 120);
this.sapaProcessor.sendCommand("/subfeedback4:" + 40);
this.sapaProcessor.sendCommand("/subdecay4:" + 30);
this.sapaProcessor.sendCommand("/subdelay5:" + 130);
this.sapaProcessor.sendCommand("/subfeedback5:" + 40);
this.sapaProcessor.sendCommand("/subdecay5:" + 20);
this.sapaProcessor.sendCommand("/subdelay6:" + 160);
this.sapaProcessor.sendCommand("/subfeedback6:" + 40);
this.sapaProcessor.sendCommand("/subdecay6:" + 10);
}
}
private void SetTremoloSapa() {
if (this.sapaProcessor != null) {
this.sapaProcessor.sendCommand("/tremolo_wet_mix:" + 40);
this.sapaProcessor.sendCommand("/tremolo_dry_mix:" + 40);
this.sapaProcessor.sendCommand("/tremolo_frequency:" + 500);
this.sapaProcessor.sendCommand("/tremolo_depth:" + -12);
this.sapaProcessor.sendCommand("/tremolo_waveform:" + 0);
}
}
private void setPhaserSapa() {
if (this.sapaProcessor != null) {
this.sapaProcessor.sendCommand("/phaser_drymix:" + 40.0f);
this.sapaProcessor.sendCommand("/phaser_sweep_gain:" + 10.0f);
this.sapaProcessor.sendCommand("/phaser_center_frequency:" + 10000.0f);
this.sapaProcessor.sendCommand("/phaser_depth:" + 5.0f);
this.sapaProcessor.sendCommand("/phaser_resonance:" + 7.0f);
this.sapaProcessor.sendCommand("/phaser_sweep_frequency:" + 10.0f);
}
}
private void ShowSapaAlertDialogAndCloseApp() {
Log.d("FATAL ERROR", "FATAL ERROR");
}
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
WriteToDebugLog.write("DEBUG", "onCreated");
this.activity = this;
this.sapa = new Sapa();
try {
sapa.initialize(this);
initializedSapaBoolean = true;
} catch (Exception e2) {
initializedSapaBoolean = false;
WriteToDebugLog.write("Fatal exception", e2.toString());
finish();
}
try {
sapaService = new SapaService();
if (!sapaService.isStarted()){
sapaService.start(SapaService.START_PARAM_DEFAULT_LATENCY);
}
try {
sapaProcessor = new SapaProcessor(activity, null, new SapaProcessor.StatusListener(){
@Override
public void onKilled() {
Log.d("Sapa Processor", "SapaSimpleClient will be closed. because of the SapaProcessor was closed");
finish();
}
});
sapaService.register(sapaProcessor, false);
sapaProcessor.activate();
setVolume(12);
setEffectSapa();
setCustomFiltersSapa();
SetTremoloSapa();
setPhaserSapa();
List arrayList = new ArrayList();
List arrayList2 = new ArrayList();
try {
for (SapaPort sapaPort : sapaProcessor.getPorts()){
if (sapaPort.getInOutType() == 1) {
arrayList.add(sapaPort);
} else {
arrayList2.add(sapaPort);
}
}
List playbackDevices = new ArrayList();
List captureDevices = new ArrayList();
for (SapaPort sapaPort2 : sapaService.getSystemPorts()){
if (sapaPort2.getInOutType() == 2 && sapaPort2.getName().contains("playback")) {
playbackDevices.add(sapaPort2);
} else if (sapaPort2.getInOutType() == 1 && sapaPort2.getName().contains("capture")) {
captureDevices.add(sapaPort2);
}
}
if (arrayList.size() >= 2 && playbackDevices.size() >= 2) {
sapaService.connect(new SapaPortConnection((SapaPort) arrayList.get(0), (SapaPort) playbackDevices.get(0)));
sapaService.connect(new SapaPortConnection((SapaPort) arrayList.get(1), (SapaPort) playbackDevices.get(1)));
}
if (arrayList2.size() >= 1 && captureDevices.size() >= 2) {
sapaPortConnection = new SapaPortConnection((SapaPort) captureDevices.get(0), (SapaPort) arrayList2.get(0));
sapaService.connect(sapaPortConnection);
WriteToDebugLog.write("DEBUG", "using mic 1");
}
} catch (Exception e) {
e.printStackTrace();
ShowSapaAlertDialogAndCloseApp();
return;
}
} catch (Exception e2) {
e2.printStackTrace();
ShowSapaAlertDialogAndCloseApp();
return;
}
} catch (Exception e3) {
e3.printStackTrace();
ShowSapaAlertDialogAndCloseApp();
return;
}
IntentFilter intentFilter = new IntentFilter();
intentFilter = new IntentFilter("android.intent.action.HEADSET_PLUG");
intentFilter.addAction("android.intent.action.SCREEN_ON");
intentFilter.addAction("android.intent.action.SCREEN_OFF");
registerReceiver(this.broadcastReceiver, intentFilter);
sapaProcessor.sendCommand("/start_record:");
}
protected void onDestroy() {
if (this.initializedSapaBoolean) {
unregisterReceiver(this.broadcastReceiver);;
this.notificationManager.cancelAll();
if (!(this.sapaService == null || this.sapaProcessor == null)) {
this.sapaProcessor.deactivate();
this.sapaService.unregister(this.sapaProcessor);
this.sapaProcessor = null;
}
if (this.sapaService != null && this.sapaService.isStarted()) {
this.sapaService.stop(false);
}
}
super.onDestroy();
}
}