블루투스 헤드셋의 마이크로 녹음을 해야할 일이 생겼는데, 검색어를 mp3로 했더니 정말 찾기도 힘들고, 어렵게 되어있더라 ;;;;
일본 개발자가 만들어 놓은 소스를 수정해서 블루투스에서 mp3로 저장하게 만들어 봤다.
https://github.com/susemi99/AndroidBluetoothVoiceRecorder
하지만 다른 프로젝트에서 가져가서 붙이려니 뭐가 이리 어려운지 ㅠㅠ
그러다가 문득 m4a로 하면 안되나 싶어서 검색해봤더니 아주 쉽게 되어있다 -_-;;;;
바보인가 ㅠ
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BROADCAST_STICKY" />
주요 부분의 소스는 이렇다.
블루투스를 열어놓고?, 파일을 만들어 놓고, MediaRecorder를 실행하면 되는 거였다.
File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/AudioRecordTest"); if (!path.exists()) path.mkdirs(); stopRecord(); try { File file = File.createTempFile("audio_", ".m4a", path); audioManager.startBluetoothSco(); recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(file.toString()); recorder.prepare(); recorder.start(); textFilePath.setText(file.getAbsolutePath()); textStatus.setText("recording"); } catch (Exception e) { e.printStackTrace(); }
public class MainActivity extends Activity { private AudioManager audioManager; private MediaRecorder recorder; private TextView textFilePath, textStatus; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); textFilePath = (TextView) findViewById(R.id.text_file_path); textStatus = (TextView) findViewById(R.id.text_status); findViewById(R.id.btn_record).setOnClickListener(clickListener); findViewById(R.id.btn_stop).setOnClickListener(clickListener); } @Override protected void onDestroy() { super.onDestroy(); stopRecord(); } private void startRecord() { File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/AudioRecordTest"); if (!path.exists()) path.mkdirs(); stopRecord(); try { File file = File.createTempFile("audio_", ".m4a", path); audioManager.startBluetoothSco(); recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(file.toString()); recorder.prepare(); recorder.start(); textFilePath.setText(file.getAbsolutePath()); textStatus.setText("recording"); } catch (Exception e) { e.printStackTrace(); } } private void stopRecord() { try { audioManager.stopBluetoothSco(); } catch (Exception e) { e.printStackTrace(); } try { recorder.stop(); recorder.release(); recorder = null; textStatus.setText("stopped"); } catch (Exception e) { e.printStackTrace(); } } private OnClickListener clickListener = new OnClickListener() { @Override public void onClick(View v) { if (v.getId() == R.id.btn_record) startRecord(); else if (v.getId() == R.id.btn_stop) stopRecord(); } }; }
처음부터 mp3 대신에 mp4나 m4a 로 검색했으면 쉽게 끝났을 일을 ㅠㅠ
code : https://github.com/susemi99/AudioRecordByBluetooth