1

First time when i run this code it runs fine but when i pause and play it again mainthread stop for more than or equal to 20 second and then show ANR dialog and after that it didn't start again.Reviewed this code many time but not able to debug.I also comment startRecording() and stopRecording() but after that it runs okay i.e, the problem is in starting and stopping the service.

public class RecordingService extends Service {

private String filePath = null;
private String dateSuffix = null;
private MediaRecorder mediaRecorder = null;
private DatabaseHelperClass databaseHelperClass;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    databaseHelperClass = new DatabaseHelperClass(getApplicationContext());
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    startRecording();
    return START_STICKY;
}


@Override
public void onDestroy() {
    stopRecording();
    super.onDestroy();
}

private void stopRecording() {
    try {
        if (mediaRecorder != null) {
            mediaRecorder.stop();
            mediaRecorder.release();
            mediaRecorder = null;
        }
    }
    catch (IllegalStateException e){

    }
}

private void startRecording() {
    if (mediaRecorder != null)
        mediaRecorder.release();

    pathCreater();

    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    mediaRecorder.setAudioChannels(1);
    mediaRecorder.setOutputFile(filePath);
    try {
        mediaRecorder.prepare();
        mediaRecorder.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void pathCreater() {
    String fileName;
    File file;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM MM dd, yyyy h:mm a");
    long date = System.currentTimeMillis();
    dateSuffix = simpleDateFormat.format(date);

    do {
        fileName = getString(R.string.file_name_suffix) + "_" + dateSuffix + ".mp3";
        filePath = Environment.getExternalStorageDirectory().getAbsolutePath();
        filePath += "/" + fileName;

        file = new File(filePath);
    } while (file.exists() && !file.isDirectory());
}
}
ShubhamBhama
  • 27
  • 2
  • 6
  • Use a thread. Too much is being done on the UI thread. Any app that takes more than 5 seconds to do something on the UI thread becomes ANR. https://developer.android.com/training/articles/perf-anr.html and https://stackoverflow.com/questions/14702922/android-service-and-ui-thread – CmosBattery Jun 06 '17 at 05:00
  • When iam doing stopRecording() in AsyncTask but it is again showing ANR. What i understood is when i play recording first time and stop and again play recording it didn't stop the previous recoding and wait for previous recoding to stop it shows ANR dialog – ShubhamBhama Jun 06 '17 at 05:17
  • Finally,Sort out the problem.The problem is in pathCreater() method line number 3,4,5. – ShubhamBhama Jun 06 '17 at 08:09

0 Answers0