###前提・実現したいこと
Android端末で音声を録音するアプリを作ろうと思い、以下のURLを参考にしてプログラムを作りました。
http://steavevaivai.hatenablog.com/entry/2015/01/03/225505
いまはMediaRecordクラスを使って試しています。
プログラムを実行したところ、
mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
のところで、例外処理が発生し、
W/System.err: java.lang.RuntimeException: setAudioSource failed.
というエラーが発生してしまいます。
パーミッションとしては、以下のものを書いています。
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
何が原因なのでしょうか。よろしくお願いします。
###発生している問題・エラーメッセージ
12/08 09:46:28: Launching app $ adb push /Users/ara/AndroidStudioProjects/sample/app/build/outputs/apk/app-debug.apk /data/local/tmp/com.example.ara.sample $ adb shell pm install -r "/data/local/tmp/com.example.ara.sample" pkg: /data/local/tmp/com.example.ara.sample Success $ adb shell am start -n "com.example.ara.sample/com.example.ara.sample.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D Waiting for application to come online: com.example.ara.sample.test | com.example.ara.sample Waiting for application to come online: com.example.ara.sample.test | com.example.ara.sample Waiting for application to come online: com.example.ara.sample.test | com.example.ara.sample Connecting to com.example.ara.sample I/System.out: Sending WAIT chunk I/art: Debugger is active I/System.out: Debugger has connected I/System.out: waiting for debugger to settle... Connected to the target VM, address: 'localhost:8618', transport: 'socket' I/System.out: waiting for debugger to settle... I/System.out: waiting for debugger to settle... I/System.out: waiting for debugger to settle... I/System.out: waiting for debugger to settle... I/System.out: waiting for debugger to settle... I/System.out: waiting for debugger to settle... I/System.out: debugger has settled (1336) W/System: ClassLoader referenced unknown path: /data/app/com.example.ara.sample-2/lib/arm64 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable I/HwSecImmHelper: mSecurityInputMethodService is null E/HAL: load: id=gralloc != hmi->id=gralloc E/HAL: load: id=gralloc != hmi->id=gralloc I/OpenGLRenderer: Initialized EGL, version 1.4 W/OpenGLRenderer: load: so=/system/lib64/libhwuibp.so dlopen failed: library "/system/lib64/libhwuibp.so" not found W/OpenGLRenderer: Initialize Binary Program Cache: Load Failed E/HAL: load: id=gralloc != hmi->id=gralloc I/HwSecImmHelper: mSecurityInputMethodService is null I/HwSecImmHelper: mSecurityInputMethodService is null W/System.err: java.lang.RuntimeException: setAudioSource failed. W/System.err: at android.media.MediaRecorder.setAudioSource(Native Method) W/System.err: at com.example.ara.sample.MainActivity.startMediaRecord(MainActivity.java:61) W/System.err: at com.example.ara.sample.MainActivity.access$300(MainActivity.java:13) W/System.err: at com.example.ara.sample.MainActivity$1.onClick(MainActivity.java:45) W/System.err: at android.view.View.performClick(View.java:5264) W/System.err: at android.view.View$PerformClick.run(View.java:21297) W/System.err: at android.os.Handler.handleCallback(Handler.java:743) W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) W/System.err: at android.os.Looper.loop(Looper.java:150) W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5546) W/System.err: at java.lang.reflect.Method.invoke(Native Method) W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794) W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684) Disconnected from the target VM, address: 'localhost:8618', transport: 'socket'
###該当のソースコード
package com.example.ara.sample; import android.media.MediaRecorder; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.io.File; public class MainActivity extends AppCompatActivity { private TextView textView; private boolean flag = false; private MediaRecorder mediarecorder; //録音用のメディアレコーダークラス static final String filePath = "/sdcard/sampleWav.wav"; //録音用のファイルパス @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // ボタンを設定 Button button = (Button)findViewById(R.id.button); // TextView の設定 textView = (TextView) findViewById(R.id.text_view); // リスナーをボタンに登録 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // flagがtrueの時 if (flag) { textView.setText("off"); flag = false; stopRecord(); } // flagがfalseの時 else { textView.setText("on"); flag = true; startMediaRecord(); } } }); } private void startMediaRecord(){ try{ File mediafile = new File(filePath); if(mediafile.exists()) { //ファイルが存在する場合は削除する mediafile.delete(); } mediafile = null; mediarecorder = new MediaRecorder(); //マイクからの音声を録音する mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //ファイルへの出力フォーマット DEFAULTにするとwavが扱えるはず mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); //音声のエンコーダーも合わせてdefaultにする mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); //ファイルの保存先を指定 mediarecorder.setOutputFile(filePath); //録音の準備をする mediarecorder.prepare(); //録音開始 mediarecorder.start(); } catch(Exception e){ e.printStackTrace(); } } //停止 private void stopRecord(){ if(mediarecorder == null){ Toast.makeText(MainActivity.this, "mediarecorder = null", Toast.LENGTH_SHORT).show(); }else{ try{ //録音停止 mediarecorder.stop(); mediarecorder.reset(); }catch (Exception e){ e.printStackTrace(); } } } }
###試したこと
課題に対してアプローチしたことを記載してください
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/12/08 02:38