我正在使用 Sony Smart Eyeglass 开发录音机,但效果不佳。
我的应用程序只记录来自手机麦克风的声音,而不是来自智能眼镜麦克风的声音。
我想只用智能眼镜麦克风录制声音。
有任何想法吗?
这是我的代码。
public class AudioRecordControl extends ControlExtension {
private final AudioManager _audioManager;
private final File _file;
private SmartEyeglassControlUtils _util;
private static final int SMARTEYEGLASS_API_VERSION = 1;
private MediaRecorder _recorder;
private MediaPlayer _player;
enum State {
STOP,
RECORDING,
PLAYING,
}
private State _state;
public AudioRecordControl(Context context, String hostAppPackageName) {
super(context, hostAppPackageName);
_util = new SmartEyeglassControlUtils(hostAppPackageName, new SmartEyeglassEventListener() {
@Override
public void onDialogClosed(int code) {
super.onDialogClosed(code);
doNextAction(code);
showCurrentLayout();
}
});
_util.setRequiredApiVersion(SMARTEYEGLASS_API_VERSION);
_util.activate(context);
_audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
_audioManager.setMode(AudioManager.MODE_IN_CALL);
_audioManager.startBluetoothSco();
_state = State.STOP;
File directoryPath = Environment.getExternalStoragePublicDirectory("AudioRecord");
if (!directoryPath.exists()) {
if (!directoryPath.mkdirs()) {
Log.e(Constants.LOG_TAG, "failed to create directory '" + directoryPath.toString() + "'");
}
}
_file = new File(directoryPath, "record.3gp");
showCurrentLayout();
}
private void showCurrentLayout() {
showLayout(R.layout.layout, null);
switch (_state) {
case STOP:
String[] buttons;
if (_file.exists()) {
buttons = new String[]{
mContext.getString(R.string.record),
mContext.getString(R.string.play)};
} else {
buttons = new String[]{mContext.getString(R.string.record)};
}
_util.showDialogMessage(
mContext.getString(R.string.title),
mContext.getString(R.string.choose_one), buttons);
break;
case RECORDING:
_util.showDialogMessage(
mContext.getString(R.string.stop_recording),
com.sony.smarteyeglass.SmartEyeglassControl.Intents.DIALOG_MODE_OK);
break;
case PLAYING:
_util.showDialogMessage(
mContext.getString(R.string.stop_playing),
com.sony.smarteyeglass.SmartEyeglassControl.Intents.DIALOG_MODE_OK);
break;
default:
break;
}
}
private void doNextAction(int code) {
if (code == -1) {
stopRequest();
}
showLayout(R.layout.layout, null);
switch (_state) {
case STOP:
if (code == 0) {
Log.d(Constants.LOG_TAG, "start recording");
try {
_startRecording();
} catch (IOException e) {
Log.e(Constants.LOG_TAG, "failed to record", e);
_util.showDialogMessage(
mContext.getString(R.string.failed_to_record),
SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
_recorder = null;
return;
}
_state = State.RECORDING;
} else if (code == 1) {
Log.d(Constants.LOG_TAG, "start playing");
try {
_play();
} catch (IOException e) {
Log.e(Constants.LOG_TAG, "failed to play", e);
_util.showDialogMessage(
mContext.getString(R.string.failed_to_pla),
SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
_player = null;
return;
}
_state = State.PLAYING;
} else {
stopRequest();
}
break;
case RECORDING:
Log.d(Constants.LOG_TAG, "stop recording");
_stopRecord();
_state = State.STOP;
break;
case PLAYING:
Log.d(Constants.LOG_TAG, "stop playing");
_stopPlay();
_state = State.STOP;
break;
default:
_state = State.STOP;
break;
}
}
private void _startRecording() throws IOException {
if (_recorder == null) {
_recorder = new MediaRecorder();
_recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
_recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
_recorder.setOutputFile(String.valueOf(_file));
_recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
_recorder.prepare();
_recorder.start();
}
}
private void _stopRecord() {
if (_recorder != null) {
_recorder.stop();
_recorder.reset();
_recorder.release();
_recorder = null;
}
}
private void _play() throws IOException {
if (_player == null) {
_player = new MediaPlayer();
_player.setDataSource(String.valueOf(_file));
_player.prepare();
_player.start();
}
}
private void _stopPlay() {
if (_player != null) {
_player.stop();
_player.reset();
_player.release();
_player= null;
}
}
@Override
public void onResume() {
showCurrentLayout();
}
@Override
public void onTap(int action, long timeStamp) {
super.onTap(action, timeStamp);
showCurrentLayout();
}
@Override
public void onDestroy() {
Log.d(Constants.LOG_TAG, "Control On Desuptroy");
_util.deactivate();
_audioManager.setMode(AudioManager.MODE_NORMAL);
_audioManager.stopBluetoothSco();
}
}