2016-05-08 8 views
3

Ich versuche Ton mit Mikrofon aufzunehmen. Der Code funktioniert perfekt auf Samsung Note 2 (N7100), aber ich erhalte einige Fehler auf einigen Geräten. Was ist der Mittelwert dieser Fehler? Meine Codes hier unten. Danke im Voraus.java.lang.IllegalStateException bei android.media.MediaRecorder

private void startRecording() { 
    CharSequence now = android.text.format.DateFormat.format("dd-MM-yyyy HH:mm:ss", new Date()); 
    String outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "The Looppad" 
      + File.separator + now + ".3gp"; 
    mRecorder = new MediaRecorder(); 
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); 
    mRecorder.setAudioSamplingRate(44100); 
    mRecorder.setAudioEncodingBitRate(96000); 
    mRecorder.setOutputFile(outputFile); 
    try { 
     mRecorder.prepare(); 
    } catch (IllegalStateException | IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    mRecorder.start(); 
    record.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN); 
    isRecording = true; 
} 

Fehler:

Fehler bei General Mobile Android 4,2

java.lang.IllegalStateException 
at android.media.MediaRecorder.start(Native Method) 
at org.uusoftware.thelaunchpadhouse.ActivityFutureHouse.m(Unknown Source) 
at org.uusoftware.thelaunchpadhouse.ActivityFutureHouse.onOptionsItemSelected(Unknown Source) 
at android.app.Activity.onMenuItemSelected(Activity.java:2566) 
at android.support.v4.b.ab.onMenuItemSelected(Unknown Source) 
at android.support.v7.a.ag.onMenuItemSelected(Unknown Source) 
at android.support.v7.view.n.onMenuItemSelected(Unknown Source) 
at android.support.v7.a.as.a(Unknown Source) 
at android.support.v7.view.menu.i.a(Unknown Source) 
at android.support.v7.view.menu.m.b(Unknown Source) 
at android.support.v7.view.menu.i.a(Unknown Source) 
at android.support.v7.view.menu.i.a(Unknown Source) 
at android.support.v7.widget.ActionMenuView.a(Unknown Source) 
at android.support.v7.view.menu.ActionMenuItemView.onClick(Unknown Source) 
at android.view.View.performClick(View.java:4211) 
at android.view.View$PerformClick.run(View.java:17446) 
at android.os.Handler.handleCallback(Handler.java:725) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:153) 
at android.app.ActivityThread.main(ActivityThread.java:5336) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
at dalvik.system.NativeStart.main(Native Method) 

Fehler bei LG G2 Android 4,4

java.lang.IllegalStateException 
at android.media.MediaRecorder.native_start(Native Method) 
at android.media.MediaRecorder.start(MediaRecorder.java:758) 
at org.uusoftware.thelaunchpadhouse.ActivityTechnoHouse.m(Unknown Source) 
at org.uusoftware.thelaunchpadhouse.ActivityTechnoHouse.onOptionsItemSelected(Unknown Source) 
at android.app.Activity.onMenuItemSelected(Activity.java:2644) 
at android.support.v4.b.ab.onMenuItemSelected(Unknown Source) 
at android.support.v7.a.ag.onMenuItemSelected(Unknown Source) 
at android.support.v7.view.n.onMenuItemSelected(Unknown Source) 
at android.support.v7.a.as.a(Unknown Source) 
at android.support.v7.view.menu.i.a(Unknown Source) 
at android.support.v7.view.menu.m.b(Unknown Source) 
at android.support.v7.view.menu.i.a(Unknown Source) 
at android.support.v7.view.menu.i.a(Unknown Source) 
at android.support.v7.widget.ActionMenuView.a(Unknown Source) 
at android.support.v7.view.menu.ActionMenuItemView.onClick(Unknown Source) 
at android.view.View.performClick(View.java:4442) 
at android.view.View$PerformClick.run(View.java:18473) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5105) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608) 
at dalvik.system.NativeStart.main(Native Method) 
+0

kopieren Sie bitte den/die Fehler einfügen. Später werden einige mit dem gleichen Problem diesen Beitrag finden können. –

+0

Schauen Sie sich http://StackOverflow.com/Questions/22877833/Why-show-error-illegalstateException-when-set-mediarecorder http://stackoverflow.com/questions/14640734/mediarecorder-ioexception-prepare-failed – Nezih

+0

Vielen Dank für die Antwort, aber beide sind nicht mein Problem verbunden. –

Antwort

0

Diese Codes lösen mein Problem:

private void startRecording() { 
    isRecording = true; 
    RECORDER_SAMPLERATE = getMaxSupportedSampleRate(); 
    bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, AudioFormat.CHANNEL_IN_STEREO, 
      AudioFormat.ENCODING_PCM_16BIT); 
    recorder = new AudioRecord(MediaRecorder.AudioSource.VOICE_RECOGNITION, RECORDER_SAMPLERATE, 
      AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, bufferSize); 
    if (android.os.Build.VERSION.SDK_INT >= 16) { 
     NoiseSuppressor.create(recorder.getAudioSessionId()); 
     AutomaticGainControl.create(recorder.getAudioSessionId()); 
     AcousticEchoCanceler.create(recorder.getAudioSessionId()); 
    } else { 
     am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); 
     am.setParameters("noise_suppression=auto"); 
    } 
    int i = recorder.getState(); 
    if (i == 1) { 
     recorder.startRecording(); 
     recordingThread = new Thread(new Runnable() { 
      @Override 
      public void run() { 
       writeAudioDataToFile(); 
      } 
     }, "AudioRecorder Thread"); 
     recordingThread.start(); 
    } else { 
     System.out.println(i); 
    } 
    record.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN); 
} 

private int getMaxSupportedSampleRate() { 
    final int validSampleRates[] = new int[]{48000, 44100, 22050, 11025, 8000}; 
    for (int i = 0; i < validSampleRates.length; i++) { 
     int result = AudioRecord.getMinBufferSize(validSampleRates[i], AudioFormat.CHANNEL_IN_STEREO, 
       AudioFormat.ENCODING_PCM_16BIT); 
     if (result != AudioRecord.ERROR && result != AudioRecord.ERROR_BAD_VALUE && result > 0) { 
      return validSampleRates[i]; 
     } 
    } 
    return -1; 
} 

private void writeAudioDataToFile() { 
    byte data[] = new byte[bufferSize]; 
    String filename = getTempFilename(); 
    FileOutputStream os = null; 
    int read = 0; 

    try { 
     os = new FileOutputStream(filename); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    if (null != os) { 
     while (isRecording) { 
      read = recorder.read(data, 0, bufferSize); 
      if (AudioRecord.ERROR_INVALID_OPERATION != read) { 
       try { 
        os.write(data); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     try { 
      os.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

Sie vollständigen Code sehen auf Github