How to record audio with Smart Eye Glass?

165 Views Asked by At

I am developing an audio recorder with Sony Smart Eyeglass, but it does not work well.

My application records just a voice from phone microphone, not from Smart Eyeglass microphone.

I'd like to record a voice just only from Smart Eyeglass microphone.

Any ideas?

Here is my code.

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();
    }
}
2

There are 2 best solutions below

0
On

Would you put your recording code in the Activity and test without using the eyeglass first?

As you are using the phone's capability only, it should work if you can record from microphone in Activity alone. Please ensure you have added uses-permission tag in AndroidManifest.xml

0
On

Some phones require an extra method call after setMode:

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

To learn more about rest of the operation, there is a guide on Audio I/O with SmartEyeglass: https://developer.sony.com/develop/wearables/smarteyeglass-sdk/guides/use-bluetooth-for-audio-io/