Awais Ahmad
Awais Ahmad

Reputation: 427

Media Recorder Strange Behavior in Service

I'm starting a Service for recording voice with mice. It runs successfully because it shows notification and toast in OnCreate of service. But the control never reach on the mediaRecorder.prepare try block and it don't even show my toast in the catch block. Here is my AudioRecorder Service

public class ServiceBgAudioRecorder extends Service {
    public static boolean isRecording = false;
    public static MediaRecorder mediaRecorder;

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

    @Override
    public void onCreate() {
        Toast.makeText(this,"Oncreate of service",Toast.LENGTH_LONG).show();
        startRecording();
    }
    private void startRecording() {
        Notification notification = new Notification.Builder(this)
                .setContentTitle("Background Audio Recorder" )
                .setContentText("Audio recording is started")
                .setSmallIcon(R.drawable.appicon)
                .build();
        startForeground(1234, notification);
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFile(AppUtilities.getOutputMediaFile(AppUtilities.MEDIA_TYPE_3GP).getAbsolutePath());
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
            Toast.makeText(this,"Audio Recording is started",Toast.LENGTH_LONG);
            isRecording = true;
        } catch (IOException e) {
            Toast.makeText(this,"Can't start Audio Recording",Toast.LENGTH_LONG);
        }
    }
    private void stopRecording() {
        mediaRecorder.release();
        mediaRecorder = null;
        isRecording = false;
    }

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

I don't know why the control is not reaching in try/catch block.

Upvotes: 0

Views: 39

Answers (1)

Sushil Dubey
Sushil Dubey

Reputation: 36

You need to display the toast ? append show() method to the makeText(). Toast.makeText(getActivity(),"Audio Recording is started",Toast.LENGTH_LONG).show();

Upvotes: 1

Related Questions