Skip to content

Commit

Permalink
Added RecordingService to handle audio recording
Browse files Browse the repository at this point in the history
  • Loading branch information
dkim0419 committed Dec 28, 2014
1 parent 2ff0a41 commit 75f9ffb
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 223 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ android {
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.2'
compile 'com.melnykov:floatingactionbutton:1.1.0'
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:name=".activities.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".RecordingService" />
</application>

</manifest>
148 changes: 0 additions & 148 deletions app/src/main/java/com/danielkim/soundrecorder/MainActivity.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.danielkim.soundrecorder;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* Created by Daniel on 12/28/2014.
*/
public class RecordingService extends Service {

private static final String LOG_TAG = "SoundRecorder_RecordFragment";

SimpleDateFormat formatter;
Date now;
private String mFileName = null;

private MediaRecorder mRecorder = null;

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

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

@Override
public void onDestroy() {
if (mRecorder != null) {
stopRecording();
}

super.onDestroy();
}

public void startRecording() {
formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
now = new Date();
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/SoundRecorder/" + formatter.format(now) + "_soundrecorder.mp4";

mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

try {
mRecorder.prepare();
mRecorder.start();

} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}

public void stopRecording() {
mRecorder.stop();
mRecorder.release();
Toast.makeText(this, "Recording saved to " + mFileName, Toast.LENGTH_LONG).show();
mRecorder = null;
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package com.danielkim.soundrecorder.fragments;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.Toast;

import com.danielkim.soundrecorder.R;
import com.danielkim.soundrecorder.RecordingService;
import com.melnykov.fab.FloatingActionButton;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.File;

/**
* A simple {@link Fragment} subclass.
Expand All @@ -30,15 +28,10 @@
public class RecordFragment extends Fragment {
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_POSITION = "position";

private static final String LOG_TAG = "SoundRecorder";
private static final String LOG_TAG = "SoundRecorder_RecordFragment";

private int position;

private String mFileName = null;
private MediaRecorder mRecorder = null;
private MediaPlayer mPlayer = null;

private FloatingActionButton mRecordButton = null;
private ImageButton mPauseButton = null;

Expand All @@ -62,12 +55,6 @@ public static RecordFragment newInstance(int position) {
}

public RecordFragment() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
Date now = new Date();
String fileName = formatter.format(now) + "_soundrecorder.mp4";

mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/SoundRecorder/" + fileName;
}

@Override
Expand All @@ -84,7 +71,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
mChronometer = (Chronometer) recordView.findViewById(R.id.chronometer);

mRecordButton = (FloatingActionButton) recordView.findViewById(R.id.btnRecord);
mRecordButton.setColorNormal(getResources().getColor(R.color.accent));
mRecordButton.setColorNormal(getResources().getColor(R.color.primary));
mRecordButton.setColorPressed(getResources().getColor(R.color.primary_dark));
mRecordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -99,58 +87,33 @@ public void onClick(View v) {
return recordView;
}

@Override
public void onPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}

if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}

// Recording Start/Stop
private void onRecord(boolean start){

Intent intent = new Intent(getActivity(), RecordingService.class);

if (start) {
startRecording();
mRecordButton.setImageResource(R.drawable.ic_media_stop);
} else {
stopRecording();
mRecordButton.setImageResource(R.drawable.perm_group_microphone);
}
}
mPauseButton.setVisibility(View.VISIBLE);
Toast.makeText(getActivity(),"Recording started",Toast.LENGTH_SHORT).show();

private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

//Animation an = new RotateAnimation(0.0f, 270.0f, 250f, 273f);
//an.setFillAfter(true);
//timerCircle.startAnimation(an);
File folder = new File(Environment.getExternalStorageDirectory() + "/SoundRecorder");
//folder /SoundRecorder doesn't exist, create the folder
if (!folder.exists()) {
folder.mkdir();
}

try {
mRecorder.prepare();
mRecorder.start();
mChronometer.setBase(SystemClock.elapsedRealtime());
mChronometer.start();

} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
getActivity().startService(intent);

private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
mChronometer.stop();
}
} else {
mRecordButton.setImageResource(R.drawable.ic_mic_white_36dp);
mPauseButton.setVisibility(View.GONE);
mChronometer.stop();

getActivity().stopService(intent);
}
}
}
Binary file modified app/src/main/res/drawable-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file modified app/src/main/res/drawable-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file modified app/src/main/res/drawable-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file modified app/src/main/res/drawable-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading

0 comments on commit 75f9ffb

Please sign in to comment.