0

我正在使用 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();
    }
}
4

2 回答 2

0

有些手机在 setMode 之后需要额外的方法调用:

private AudioManager m_amAudioManager;  
m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);  
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL); 
m_amAudioManager.setSpeakerphoneOn(false); 

要了解有关其余操作的更多信息,请参阅 SmartEyeglass 的音频 I/O 指南: https ://developer.sony.com/develop/wearables/smarteyeglass-sdk/guides/use-bluetooth-for-audio-io /

于 2015-05-29T04:05:20.613 回答
0

你会把你的录音代码放在 Activity 中并在不先使用眼镜的情况下进行测试吗?

由于您仅使用手机的功能,因此如果您可以仅在活动中从麦克风录制,它应该可以工作。请确保您在 AndroidManifest.xml 中添加了 uses-permission 标签

于 2016-02-04T08:42:08.680 回答