I am new in android guys I was trying to record voice. the program run normally on the emulator put when I try to run it on the mobile it crash getting unfortunately your app stopped
this is where the function that record exist (start)
final MediaRecorder recorder = new MediaRecorder();
private MediaController mediaController;
public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state + ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
File path_file=new File(path);
if(path_file.exists())
{
path_file.delete();
Log.w("path_file", path_file.toString());
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
Log.w("noooooo", path_file.toString());
recorder.setAudioChannels(1);
recorder.setAudioSamplingRate(8000);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
start_time=System.currentTimeMillis() ;
}
public void stop() throws IOException {
recorder.stop();
recorder.reset();
recorder.release();
}
this is where i call it
public class VoiceRecorder extends Activity implements OnPreparedListener, MediaController.MediaPlayerControl{
AudioRecorder audio=new AudioRecorder(path);
private MediaController mediaController;
private MediaPlayer mediaPlayer;
private Handler mHandler = new Handler();
private long elapsedTime;
private final int REFRESH_RATE = 100;
private boolean stopped = false;
private Runnable startTimer = new Runnable() {
public void run() {
if(!stopped){
elapsedTime = System.currentTimeMillis() - start_time;
updateTimer(elapsedTime);
mHandler.postDelayed(this,REFRESH_RATE);
}
}
};
long secs,mins,hrs;
String seconds,minutes,hours,milliseconds;
private void updateTimer (float time){
secs = (long)(time/1000);
mins = (long)((time/1000)/60);
hrs = (long)(((time/1000)/60)/60);
/* Convert the seconds to String
* and format to ensure it has
* a leading zero when required
*/
secs = secs % 60;
seconds=String.valueOf(secs);
if(secs == 0){
seconds = "00";
}
if(secs <10 && secs > 0){
seconds = "0"+seconds;
}
/* Convert the minutes to String and format the String */
mins = mins % 60;
minutes=String.valueOf(mins);
if(mins == 0){
minutes = "00";
}
if(mins <10 && mins > 0){
minutes = "0"+minutes;
}
/* Convert the hours to String and format the String */
hours=String.valueOf(hrs);
if(hrs == 0){
hours = "00";
}
if(hrs <10 && hrs > 0){
hours = "0"+hours;
}
/* Although we are not using milliseconds on the timer in this example
* I included the code in the event that you wanted to include it on your own
*/
milliseconds = String.valueOf((long)time);
if(milliseconds.length()==2){
milliseconds = "0"+milliseconds;
}
if(milliseconds.length()<=1){
milliseconds = "00";
}
milliseconds = milliseconds.substring(milliseconds.length()-3, milliseconds.length()-2);
/* Setting the timer text to the elapsed time */
((TextView)findViewById(R.id.textView3)).setText(hours + ":" + minutes + ":" + seconds);
//((TextView)findViewById(R.id.timerMs)).setText("." + milliseconds);
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice_recorder);
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
//perform action
try{
b1.setVisibility(View.INVISIBLE);//start
b2.setVisibility(View.VISIBLE);//stop
b2.setEnabled(true);//stop
b2.requestFocus();//stop
audio.start();
start_time=audio.start_time;
// updateTextView();
try{
mHandler.removeCallbacks(startTimer);
mHandler.postDelayed(startTimer, 0);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
//--MediaPlayerControl methods--
public void start() {
mediaPlayer.start();
}
public void pause() {
mediaPlayer.pause();
}
public int getDuration() {
return mediaPlayer.getDuration();
}
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
public void seekTo(int i) {
mediaPlayer.seekTo(i);
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public int getBufferPercentage() {
return 0;
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
//--------------------------------------------------------------------------------
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
Log.d(TAG, "onPrepared");
mediaPlayer.start();
mediaController.setMediaPlayer(this);
mediaController.setAnchorView(findViewById(R.id.main_audio_view));
handler.post(new Runnable() {
public void run() {
mediaController.setEnabled(true);
mediaController.show();
}
});
}
@Override
protected void onStop() {
super.onStop();
mediaController.hide();
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
}
@Override
public void onDestroy() {
super.onDestroy();
mediaPlayer.release();
mediaPlayer = null;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//the MediaController will hide after 3 seconds - tap the screen to make it appear again
mediaController.show();
return false;
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
intent.setClassName("vodafone.voice", "vodafone.voice.AndroidListViewActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
super.onBackPressed();
}
}
and this is the logcat:
05-23 12:46:18.028: E/AndroidRuntime(7389): FATAL EXCEPTION: main
05-23 12:46:18.028: E/AndroidRuntime(7389): java.lang.StringIndexOutOfBoundsException: length=2; regionStart=-1; regionLength=1
05-23 12:46:18.028: E/AndroidRuntime(7389): at java.lang.String.startEndAndLength(String.java:593)
05-23 12:46:18.028: E/AndroidRuntime(7389): at java.lang.String.substring(String.java:1474)
05-23 12:46:18.028: E/AndroidRuntime(7389): at vodafone.voice.VoiceRecorder.updateTimer(VoiceRecorder.java:109)
05-23 12:46:18.028: E/AndroidRuntime(7389): at vodafone.voice.VoiceRecorder.access$3(VoiceRecorder.java:60)
05-23 12:46:18.028: E/AndroidRuntime(7389): at vodafone.voice.VoiceRecorder$1.run(VoiceRecorder.java:48)
05-23 12:46:18.028: E/AndroidRuntime(7389): at android.os.Handler.handleCallback(Handler.java:608)
05-23 12:46:18.028: E/AndroidRuntime(7389): at android.os.Handler.dispatchMessage(Handler.java:92)
05-23 12:46:18.028: E/AndroidRuntime(7389): at android.os.Looper.loop(Looper.java:156)
05-23 12:46:18.028: E/AndroidRuntime(7389): at android.app.ActivityThread.main(ActivityThread.java:4987)
05-23 12:46:18.028: E/AndroidRuntime(7389): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 12:46:18.028: E/AndroidRuntime(7389): at java.lang.reflect.Method.invoke(Method.java:511)
05-23 12:46:18.028: E/AndroidRuntime(7389): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-23 12:46:18.028: E/AndroidRuntime(7389): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-23 12:46:18.028: E/AndroidRuntime(7389): at dalvik.system.NativeStart.main(Native Method)