2016-05-05 5 views
1

работает в режиме записи, когда я запись в первый раз это работает нормально, но второй раз начинает запись и когда остановка, после чего записи, то это крах НАД дать java.lang.IllegalStateExceptionApp Сбой при остановке записи второго раза

я Dont underswtand почему

мой код

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    session = new SessionManager(getApplicationContext()); 
    session.checkLogin(); 
    HashMap<String, String> user = session.getUserDetails(); 
    String name = user.get(SessionManager.KEY_NAME); 
    String email = user.get(SessionManager.KEY_EMAIL); 
    // outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp"; 
    _Start = (Button)findViewById(R.id.start); 
    _Stop =(Button)findViewById(R.id.stop); 
    _Send = (Button)findViewById(R.id.sendbtn); 
    _Play =(Button)findViewById(R.id.play); 
    _CountTimer =(TextView)findViewById(R.id.timer_view); 
    _ImgView =(ImageView)findViewById(R.id.imgView); 
    boolean exists = (new File(path)).exists(); 
    if(!exists) { 
     new File(path).mkdirs(); 
    } 
    else { 
     mRecorder = new MediaRecorder(); 
     mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
     mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
     mRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); 
     mRecorder.setOutputFile(path + audioname); 

    } 
    _CountTimer.setText("00:30"); 
    final GumzoCounterClass timer = new GumzoCounterClass(30000, 1000); 
    _Start.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      try { 
       try { 
        mRecorder.prepare(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       mRecorder.start(); 
       timer.start(); 
      } catch (IllegalStateException e) { 
       e.printStackTrace(); 
      } 
      _ImgView.setVisibility(View.VISIBLE); 
      _Start.setVisibility(View.INVISIBLE); 
      _Play.setVisibility(View.INVISIBLE); 
      _Stop.setVisibility(View.VISIBLE); 
      Toast.makeText(getApplicationContext(), "Start Recording", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    _Stop.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      _ImgView.setVisibility(View.INVISIBLE); 
      _Start.setVisibility(View.VISIBLE); 
      _Stop.setVisibility(View.INVISIBLE); 
      _Play.setVisibility(View.VISIBLE); 
      mRecorder.stop(); 
      timer.cancel(); 
      _CountTimer.setText("Stoped Recording"); 
      Toast.makeText(getApplicationContext(), "Stop Recording", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    _Play.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      MediaPlayer m = new MediaPlayer(); 
      // _Start.setEnabled(false); 
      try { 
       m.setDataSource(outputFile); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      try { 
       m.prepare(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      m.start(); 
      Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show(); 
     } 

    }); 
} 

public class GumzoCounterClass extends CountDownTimer { 
    public GumzoCounterClass(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
    } 
    @Override 

    public void onFinish() { 
     _CountTimer.setText("Completed"); 
     mRecorder.stop(); 
     mRecorder.release(); 
    } 
    @Override 
    public void onTick(long millisUntilFinished) { 
     _CountTimer.setText((millisUntilFinished/1000) + ""); 
    } 
} 

public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.activity_menu, menu); 
    return true; 
} 

public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.logout: 
      AccessToken accessToken = AccessToken.getCurrentAccessToken(); 
      if(accessToken != null){ 
       LoginManager.getInstance().logOut(); 
       startActivity(new Intent(getApplicationContext(),Login_Activity.class)); 
       finish(); 
      } 
      Toast.makeText(MainActivity.this, "Logout User", Toast.LENGTH_SHORT).show(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 

} 

Мои Logcate

java.lang.IllegalStateException 
at android.media.MediaRecorder.stop(Native Method) 
at user.example.com.myApp.MainActivity$2.onClick(MainActivity.java:95) 
at android.view.View.performClick(View.java:4848) 
at android.view.View$PerformClick.run(View.java:20262) 
at android.os.Handler.handleCallback(Handler.java:815) 
at android.os.Handler.dispatchMessage(Handler.java:104) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5631) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 

помогите мне пожалуйста

+1

мы можем LogCat?! –

+0

@NiravRanpara теперь я обновляю logcate –

ответ

1

Его, потому что вы призывают остановить на null объекта. Вы должны проверить state вашего MediaRecorder перед остановкой

+0

tahnks, но в первый раз он работает правильно как. я не понимаю –

+0

попробуйте инициализировать mRecorder = new MediaRecorder(); внутри onclick _Start –

+0

благодаря его работе сейчас –

0

В соответствии с official example from google вам необходимо сделать следующее при прекращении записи.

// stop recording and release camera 
      mMediaRecorder.stop(); // stop the recording 
      releaseMediaRecorder(); // release the MediaRecorder object 

И releaseMediaRecorder() это выглядеть,

private void releaseMediaRecorder(){ 
    if (mMediaRecorder != null) { 
     // clear recorder configuration 
     mMediaRecorder.reset(); 
     // release the recorder object 
     mMediaRecorder.release(); 
     mMediaRecorder = null; 
    } 
} 
Смежные вопросы