Skip to content

Commit

Permalink
0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
adrielcafe committed Sep 16, 2016
1 parent c922f7e commit ad1a6bb
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 39 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# AndroidAudioRecorder

> A fancy audio recorder for Android. It supports `WAV` format.
> A fancy audio recorder for Android. It supports `WAV` format at 48kHz.
![Screenshots](https://raw.githubusercontent.com/adrielcafe/AndroidAudioRecorder/master/demo.gif)

Expand All @@ -22,9 +22,17 @@ String filePath = Environment.getExternalStorageDirectory() + "/recorded_audio.w
int color = getResources().getColor(R.color.colorPrimaryDark);
int requestCode = 0;
AndroidAudioRecorder.with(this)
// Required
.setFilePath(filePath)
.setColor(color)
.setRequestCode(requestCode)

// Optional
.setSource(AudioSource.MIC)
.setChannel(AudioChannel.STEREO)
.setSampleRate(AudioSampleRate.HZ_48000)

// Start recording
.record();
```

Expand Down Expand Up @@ -53,7 +61,7 @@ repositories {
}
dependencies {
compile 'com.github.adrielcafe:AndroidAudioRecorder:0.0.9'
compile 'com.github.adrielcafe:AndroidAudioRecorder:0.1.0'
}
```

Expand All @@ -63,6 +71,7 @@ dependencies {
- [X] Wave visualization based on this [player concept](https://dribbble.com/shots/2369760-Player-Concept)
- [X] Play recorded audio
- [X] Pause recording
- [X] Configure audio source (Mic/Camcorder), channel (Stereo/Mono) and sample rate (8kHz to 48kHz)
- [ ] Skip silence
- [ ] Animations
- [ ] Landscape screen orientation (only supports portrait at the moment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import android.widget.Toast;

import cafe.adriel.androidaudiorecorder.AndroidAudioRecorder;
import cafe.adriel.androidaudiorecorder.Util;
import cafe.adriel.androidaudiorecorder.model.AudioChannel;
import cafe.adriel.androidaudiorecorder.model.AudioSampleRate;
import cafe.adriel.androidaudiorecorder.model.AudioSource;

public class MainActivity extends AppCompatActivity {
private static final String AUDIO_FILE_PATH = Environment.getExternalStorageDirectory() + "/recorded_audio.wav";
Expand Down Expand Up @@ -44,9 +46,17 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

public void recordAudio(View v) {
AndroidAudioRecorder.with(this)
// Required
.setFilePath(AUDIO_FILE_PATH)
.setColor(getResources().getColor(R.color.recorder_bg))
.setRequestCode(RECORD_AUDIO)

// Optional
.setSource(AudioSource.MIC)
.setChannel(AudioChannel.STEREO)
.setSampleRate(AudioSampleRate.HZ_48000)

// Start recording
.record();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cafe.adriel.androidaudiorecorder.example;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

public class Util {

public static void requestPermission(Activity activity, String permission) {
if (ContextCompat.checkSelfPermission(activity, permission)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{permission}, 0);
}
}

}
4 changes: 2 additions & 2 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ android {
}

dependencies {
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.kailashdabhi:om-recorder:1.1.0'
compile 'com.cleveroad:audiovisualization:0.9.4'
compile 'com.cleveroad:audiovisualization:1.0.0'
}
4 changes: 1 addition & 3 deletions lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"
package="cafe.adriel.androidaudiorecorder">

<application
android:label="@string/app_name"
android:allowBackup="true">
<application android:label="@string/app_name">
<activity
android:name=".AudioRecorderActivity"
android:theme="@style/Theme.AppCompat"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@
import android.graphics.Color;
import android.os.Environment;

import cafe.adriel.androidaudiorecorder.model.AudioChannel;
import cafe.adriel.androidaudiorecorder.model.AudioSampleRate;
import cafe.adriel.androidaudiorecorder.model.AudioSource;

public class AndroidAudioRecorder {
public static final String EXTRA_FILE_PATH = "filePath";
public static final String EXTRA_COLOR = "color";

protected static final String EXTRA_FILE_PATH = "filePath";
protected static final String EXTRA_COLOR = "color";
protected static final String EXTRA_SOURCE = "source";
protected static final String EXTRA_CHANNEL = "channel";
protected static final String EXTRA_SAMPLE_RATE = "sampleRate";

private Activity activity;

private String filePath = Environment.getExternalStorageDirectory() + "/recorded_audio.wav";
private AudioSource source = AudioSource.MIC;
private AudioChannel channel = AudioChannel.STEREO;
private AudioSampleRate sampleRate = AudioSampleRate.HZ_44100;
private int color = Color.parseColor("#546E7A");
private int requestCode = 0;

Expand All @@ -38,10 +49,28 @@ public AndroidAudioRecorder setRequestCode(int requestCode) {
return this;
}

public AndroidAudioRecorder setSource(AudioSource source) {
this.source = source;
return this;
}

public AndroidAudioRecorder setChannel(AudioChannel channel) {
this.channel = channel;
return this;
}

public AndroidAudioRecorder setSampleRate(AudioSampleRate sampleRate) {
this.sampleRate = sampleRate;
return this;
}

public void record() {
Intent intent = new Intent(activity, AudioRecorderActivity.class);
intent.putExtra(EXTRA_FILE_PATH, filePath);
intent.putExtra(EXTRA_COLOR, color);
intent.putExtra(EXTRA_SOURCE, source);
intent.putExtra(EXTRA_CHANNEL, channel);
intent.putExtra(EXTRA_SAMPLE_RATE, sampleRate);
activity.startActivityForResult(intent, requestCode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.util.Timer;
import java.util.TimerTask;

import cafe.adriel.androidaudiorecorder.model.AudioChannel;
import cafe.adriel.androidaudiorecorder.model.AudioSampleRate;
import cafe.adriel.androidaudiorecorder.model.AudioSource;
import omrecorder.AudioChunk;
import omrecorder.OmRecorder;
import omrecorder.PullTransport;
Expand All @@ -28,13 +31,16 @@
public class AudioRecorderActivity extends AppCompatActivity
implements PullTransport.OnAudioChunkPulledListener, MediaPlayer.OnCompletionListener {

private String filePath;
private AudioSource source;
private AudioChannel channel;
private AudioSampleRate sampleRate;
private int color;

private MediaPlayer player;
private Recorder recorder;
private VisualizerHandler visualizerHandler;

private String filePath;
private int color;

private Timer timer;
private MenuItem saveMenuItem;
private int recorderSecondsElapsed;
Expand All @@ -56,9 +62,15 @@ protected void onCreate(Bundle savedInstanceState) {

if(savedInstanceState != null) {
filePath = savedInstanceState.getString(AndroidAudioRecorder.EXTRA_FILE_PATH);
source = (AudioSource) savedInstanceState.getSerializable(AndroidAudioRecorder.EXTRA_SOURCE);
channel = (AudioChannel) savedInstanceState.getSerializable(AndroidAudioRecorder.EXTRA_CHANNEL);
sampleRate = (AudioSampleRate) savedInstanceState.getSerializable(AndroidAudioRecorder.EXTRA_SAMPLE_RATE);
color = savedInstanceState.getInt(AndroidAudioRecorder.EXTRA_COLOR);
} else {
filePath = getIntent().getStringExtra(AndroidAudioRecorder.EXTRA_FILE_PATH);
source = (AudioSource) getIntent().getSerializableExtra(AndroidAudioRecorder.EXTRA_SOURCE);
channel = (AudioChannel) getIntent().getSerializableExtra(AndroidAudioRecorder.EXTRA_CHANNEL);
sampleRate = (AudioSampleRate) getIntent().getSerializableExtra(AndroidAudioRecorder.EXTRA_SAMPLE_RATE);
color = getIntent().getIntExtra(AndroidAudioRecorder.EXTRA_COLOR, Color.BLACK);
}

Expand Down Expand Up @@ -248,7 +260,7 @@ private void resumeRecording() {
timerView.setText("00:00:00");

recorder = OmRecorder.wav(
new PullTransport.Default(Util.getMic(), AudioRecorderActivity.this),
new PullTransport.Default(Util.getMic(source, channel, sampleRate), AudioRecorderActivity.this),
new File(filePath));
}
recorder.resumeRecording();
Expand Down
36 changes: 11 additions & 25 deletions lib/src/main/java/cafe/adriel/androidaudiorecorder/Util.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,34 @@
package cafe.adriel.androidaudiorecorder;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.media.AudioFormat;
import android.media.MediaRecorder;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import omrecorder.AudioSource;
import cafe.adriel.androidaudiorecorder.model.AudioChannel;
import cafe.adriel.androidaudiorecorder.model.AudioSampleRate;
import cafe.adriel.androidaudiorecorder.model.AudioSource;


public class Util {
private static final Handler HANDLER = new Handler();
private static final int AUDIO_FREQUENCY = 44100;

private Util() {
}

public static void requestPermission(Activity activity, String permission) {
if (ContextCompat.checkSelfPermission(activity, permission)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{permission}, 0);
}
}

public static void wait(int millis, Runnable callback){
HANDLER.postDelayed(callback, millis);
}

public static AudioSource getMic() {
return new AudioSource.Smart(
MediaRecorder.AudioSource.MIC,
public static omrecorder.AudioSource getMic(AudioSource source,
AudioChannel channel,
AudioSampleRate sampleRate) {
return new omrecorder.AudioSource.Smart(
source.getSource(),
AudioFormat.ENCODING_PCM_16BIT,
AudioFormat.CHANNEL_IN_STEREO,
AUDIO_FREQUENCY);
channel.getChannel(),
sampleRate.getSampleRate());
}

/**
* Function to check brightness of background color
* @param color
* @return true if color is bright
*/
public static boolean isBrightColor(int color) {
if(android.R.color.transparent == color) {
return true;
Expand All @@ -51,7 +38,6 @@ public static boolean isBrightColor(int color) {
rgb[0] * rgb[0] * 0.241 +
rgb[1] * rgb[1] * 0.691 +
rgb[2] * rgb[2] * 0.068);
//color is bright
return brightness >= 200;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cafe.adriel.androidaudiorecorder.model;

import android.media.AudioFormat;

public enum AudioChannel {
STEREO,
MONO;

public int getChannel(){
switch (this){
case MONO:
return AudioFormat.CHANNEL_IN_MONO;
default:
return AudioFormat.CHANNEL_IN_STEREO;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cafe.adriel.androidaudiorecorder.model;

public enum AudioSampleRate {
HZ_48000,
HZ_44100,
HZ_32000,
HZ_22050,
HZ_16000,
HZ_11025,
HZ_8000;

public int getSampleRate(){
return Integer.parseInt(name().replace("HZ_", ""));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cafe.adriel.androidaudiorecorder.model;

import android.media.MediaRecorder;

public enum AudioSource {
MIC,
CAMCORDER;

public int getSource(){
switch (this){
case CAMCORDER:
return MediaRecorder.AudioSource.CAMCORDER;
default:
return MediaRecorder.AudioSource.MIC;
}
}
}

0 comments on commit ad1a6bb

Please sign in to comment.