/
RecordingService.java
174 lines (140 loc) · 5.23 KB
/
RecordingService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.danielkim.soundrecorder;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
import com.danielkim.soundrecorder.activities.MainActivity;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by Daniel on 12/28/2014.
*/
public class RecordingService extends Service {
private static final String LOG_TAG = "RecordingService";
private String mFileName = null;
private String mFilePath = null;
private MediaRecorder mRecorder = null;
private DBHelper mDatabase;
private long mStartingTimeMillis = 0;
private long mElapsedMillis = 0;
private int mElapsedSeconds = 0;
private OnTimerChangedListener onTimerChangedListener = null;
private static final SimpleDateFormat mTimerFormat = new SimpleDateFormat("mm:ss", Locale.getDefault());
private Timer mTimer = null;
private TimerTask mIncrementTimerTask = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
public interface OnTimerChangedListener {
void onTimerChanged(int seconds);
}
@Override
public void onCreate() {
super.onCreate();
mDatabase = new DBHelper(getApplicationContext());
}
@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() {
setFileNameAndPath();
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setOutputFile(mFilePath);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setAudioChannels(1);
if (MySharedPreferences.getPrefHighQuality(this)) {
mRecorder.setAudioSamplingRate(44100);
mRecorder.setAudioEncodingBitRate(192000);
}
try {
mRecorder.prepare();
mRecorder.start();
mStartingTimeMillis = System.currentTimeMillis();
//startTimer();
//startForeground(1, createNotification());
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
public void setFileNameAndPath(){
int count = 0;
File f;
do{
count++;
mFileName = getString(R.string.default_file_name)
+ "_" + (mDatabase.getCount() + count) + ".mp4";
mFilePath = Environment.getExternalStorageDirectory().getAbsolutePath();
mFilePath += "/SoundRecorder/" + mFileName;
f = new File(mFilePath);
}while (f.exists() && !f.isDirectory());
}
public void stopRecording() {
mRecorder.stop();
mElapsedMillis = (System.currentTimeMillis() - mStartingTimeMillis);
mRecorder.release();
Toast.makeText(this, getString(R.string.toast_recording_finish) + " " + mFilePath, Toast.LENGTH_LONG).show();
//remove notification
if (mIncrementTimerTask != null) {
mIncrementTimerTask.cancel();
mIncrementTimerTask = null;
}
mRecorder = null;
try {
mDatabase.addRecording(mFileName, mFilePath, mElapsedMillis);
} catch (Exception e){
Log.e(LOG_TAG, "exception", e);
}
}
private void startTimer() {
mTimer = new Timer();
mIncrementTimerTask = new TimerTask() {
@Override
public void run() {
mElapsedSeconds++;
if (onTimerChangedListener != null)
onTimerChangedListener.onTimerChanged(mElapsedSeconds);
NotificationManager mgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mgr.notify(1, createNotification());
}
};
mTimer.scheduleAtFixedRate(mIncrementTimerTask, 1000, 1000);
}
//TODO:
private Notification createNotification() {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_mic_white_36dp)
.setContentTitle(getString(R.string.notification_recording))
.setContentText(mTimerFormat.format(mElapsedSeconds * 1000))
.setOngoing(true);
mBuilder.setContentIntent(PendingIntent.getActivities(getApplicationContext(), 0,
new Intent[]{new Intent(getApplicationContext(), MainActivity.class)}, 0));
return mBuilder.build();
}
}