Tuesday, June 14, 2016

Record audio in background in android

Service class to record audio in background :

below is the service to record audio from background and save them to sdcard on desire location.

public class AudioRecorderService extends Service


{
    private MediaRecorder myRecorder;
//    private String outputFile = null;

    public IBinder onBind(Intent arg0)
    {
        return null;
    }


    @Override
    public void onCreate()
    {

        // Start foreground service to avoid unexpected kill

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
        {
            Notification notification = null;
            notification = new Notification.Builder(this)
                    .setContentTitle("Recording Audio")
                    .setContentText("")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
            startForeground(22, notification);
        }


        File imagesFolder = new File(
                Environment.getExternalStorageDirectory(), "folder name");
        if (!imagesFolder.exists())
            imagesFolder.mkdirs(); // <----


        File audio = new File(imagesFolder, System.currentTimeMillis()
                + ".3gpp");


        myRecorder = new MediaRecorder();
        myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        myRecorder.setOutputFile(audio.getPath());

        try
        {
            myRecorder.prepare();
            myRecorder.start();
        }
        catch (Exception | Error e)

        {
            // start:it is called before prepare()
            // prepare: it is called after start() or before setOutputFormat()
            e.printStackTrace();
        }


    }

    public int onStartCommand(Intent intent, int flags, int startId)
    {
        //onCreate();
//        // return 1;
//        return START_STICKY; //not sure about this one
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public void onDestroy()
    {
        try
        {
            myRecorder.stop();
            myRecorder.reset();
            myRecorder.release();
            myRecorder = null;
        }
        catch (Exception | Error e)
        {
            e.printStackTrace();
        }

    }


}


just fire a intent for this service ,it will start recording audio & stop service to stop recording.

to start recording :
startService(new Intent(context, AudioRecorderService.class));

to stop recording :
stopService(new Intent(context, AudioRecorderService.class));


don't forget to write permissions in manifest file (read,write,record)..

0 comments:

Post a Comment

Don't lose faith when you see others receive answers to their prayers

An elephant and a dog became pregnant at same time. Three months down the line the dog gave birth to six puppies. Six months later the dog...

 

G-Expo Template by Ipietoon Cute Blog Design