-
Notifications
You must be signed in to change notification settings - Fork 806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixed issue 17 #97
base: master
Are you sure you want to change the base?
fixed issue 17 #97
Changes from 3 commits
75086e6
bc37781
0bb7944
0025136
3d5eae7
10a191f
0948810
e4a72b0
19a04da
f5c3ad5
c5f5583
3873a35
0de0d04
4d4ed28
797c62c
a34b3c5
a8a531a
58d6178
b85d2e2
20958ae
3fc9c3b
1322063
0150800
f4d8045
493b93f
b6d531d
d430d42
40d7aa6
b61e037
791c6f2
2c8ca10
e90a613
93f6bad
aac4b31
3fc2639
fe216d3
9d660be
6461c11
7894e2b
7e01bef
48aa374
4e5e388
3c8aecb
5ebc9c6
20d674f
02bd993
04576fb
2cfdc6c
3378832
697c55c
63c9a9e
fca6032
45f644f
1a3cfc2
8dd7db5
f7f3d6b
7d2f59e
c14c0c7
ddb3ad0
6c7c4a6
1e0aa20
30428d6
1056828
7839c0e
3e0ab56
6daac58
307b805
83b8b31
60ad3de
80598ad
3de6e79
e23b87d
7b54057
624e714
d0bdfad
c3b6190
31f1b9d
c016c86
21dc22b
b8e5b58
84716a1
5ecb514
649592f
dcd437e
590ac3c
1c8c880
8ef8adf
58f18e0
759170c
d44fda9
54ca03f
d85f97d
46a125d
b65a5b6
06f41e4
6e19084
49c7af9
ebc2a6d
90cf343
f87b092
40aac82
ca3e6a3
a6aa8b5
b63f9ce
52c4249
a36b9f6
ee3799d
2ce25ed
e818e86
ff0022b
375177b
307b38c
6f7f7b7
4751f00
fb02d14
7c40e3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,20 +6,31 @@ | |
import android.app.Service; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.SharedPreferences; | ||
import android.media.MediaRecorder; | ||
import android.os.Binder; | ||
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.coremedia.iso.boxes.Container; | ||
import com.danielkim.soundrecorder.activities.MainActivity; | ||
import com.googlecode.mp4parser.FileDataSourceImpl; | ||
import com.googlecode.mp4parser.authoring.Movie; | ||
import com.googlecode.mp4parser.authoring.Track; | ||
import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder; | ||
import com.googlecode.mp4parser.authoring.container.mp4.MovieCreator; | ||
import com.googlecode.mp4parser.authoring.tracks.AppendTrack; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.channels.FileChannel; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
@@ -47,9 +58,20 @@ public class RecordingService extends Service { | |
private Timer mTimer = null; | ||
private TimerTask mIncrementTimerTask = null; | ||
|
||
private boolean isFilePathTemp = true; | ||
private boolean isPaused; | ||
private int tempFileCount = 0; | ||
|
||
private ArrayList<String> filesPaused = new ArrayList<>(); | ||
private ArrayList<Long> pauseDurations= new ArrayList<>(); | ||
|
||
// Binder given to clients | ||
private final IBinder mBinder = new LocalBinder(); | ||
|
||
|
||
@Override | ||
public IBinder onBind(Intent intent) { | ||
return null; | ||
return mBinder; | ||
} | ||
|
||
public interface OnTimerChangedListener { | ||
|
@@ -77,7 +99,32 @@ public void onDestroy() { | |
super.onDestroy(); | ||
} | ||
|
||
public void setFileNameAndPath() { | ||
if (isFilePathTemp) { | ||
mFileName = getString(R.string.default_file_name) + (++tempFileCount )+ "_" + ".tmp"; | ||
mFilePath = Environment.getExternalStorageDirectory().getAbsolutePath(); | ||
mFilePath += "/SoundRecorder/" + mFileName; | ||
} else { | ||
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 startRecording() { | ||
isPaused = false; | ||
isFilePathTemp=true; | ||
setFileNameAndPath(); | ||
|
||
mRecorder = new MediaRecorder(); | ||
|
@@ -104,35 +151,50 @@ public void startRecording() { | |
} | ||
} | ||
|
||
public void setFileNameAndPath(){ | ||
int count = 0; | ||
File f; | ||
|
||
do{ | ||
count++; | ||
public void pauseRecording(){ | ||
isPaused = true; | ||
mRecorder.stop(); | ||
mElapsedMillis = (System.currentTimeMillis() - mStartingTimeMillis); | ||
pauseDurations.add(mElapsedMillis); | ||
Toast.makeText(this, getString(R.string.toast_recording_paused), Toast.LENGTH_LONG).show(); | ||
|
||
mFileName = getString(R.string.default_file_name) | ||
+ "_" + (mDatabase.getCount() + count) + ".mp4"; | ||
mFilePath = Environment.getExternalStorageDirectory().getAbsolutePath(); | ||
mFilePath += "/SoundRecorder/" + mFileName; | ||
//remove notification | ||
if (mIncrementTimerTask != null) { | ||
mIncrementTimerTask.cancel(); | ||
mIncrementTimerTask = null; | ||
} | ||
filesPaused.add(mFilePath); | ||
|
||
f = new File(mFilePath); | ||
}while (f.exists() && !f.isDirectory()); | ||
} | ||
|
||
public void stopRecording() { | ||
mRecorder.stop(); | ||
mElapsedMillis = (System.currentTimeMillis() - mStartingTimeMillis); | ||
if(!isPaused) | ||
filesPaused.add(mFilePath); | ||
|
||
isFilePathTemp =false; | ||
setFileNameAndPath(); | ||
|
||
if (!isPaused) { | ||
mRecorder.stop(); | ||
mElapsedMillis = (System.currentTimeMillis() - mStartingTimeMillis); | ||
} | ||
mRecorder.release(); | ||
Toast.makeText(this, getString(R.string.toast_recording_finish) + " " + mFilePath, Toast.LENGTH_LONG).show(); | ||
|
||
isPaused = false; | ||
//remove notification | ||
if (mIncrementTimerTask != null) { | ||
mIncrementTimerTask.cancel(); | ||
mIncrementTimerTask = null; | ||
} | ||
|
||
mRecorder = null; | ||
if (filesPaused != null && !filesPaused.isEmpty()) { | ||
if (makeSingleFile(filesPaused)) { | ||
for (long duration : pauseDurations) | ||
mElapsedMillis += duration; | ||
} | ||
} | ||
|
||
try { | ||
mDatabase.addRecording(mFileName, mFilePath, mElapsedMillis); | ||
|
@@ -142,6 +204,60 @@ public void stopRecording() { | |
} | ||
} | ||
|
||
/** | ||
* collect temp generated files because of pause to one target file | ||
* @param filesPaused contains all temp files due to pause | ||
*/ | ||
private boolean makeSingleFile(ArrayList<String> filesPaused) { | ||
ArrayList<Track> tracks =new ArrayList<>(); | ||
Movie finalMovie =new Movie(); | ||
for (String filePath : filesPaused) { | ||
try { | ||
Movie movie = MovieCreator.build(new FileDataSourceImpl(filePath)); | ||
List<Track> movieTracks = movie.getTracks(); | ||
tracks.addAll(movieTracks); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
} | ||
|
||
if (tracks.size() > 0) { | ||
try { | ||
finalMovie.addTrack(new AppendTrack(tracks.toArray(new Track[tracks.size()]))); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
// try { | ||
// finalMovie.addTrack(new AppendTrack((Track) tracks)); | ||
// } catch (IOException e) { | ||
// e.printStackTrace(); | ||
// } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove "dead code" |
||
Container mp4file = new DefaultMp4Builder().build(finalMovie); | ||
FileChannel fc = null; | ||
try { | ||
fc = new FileOutputStream(new File(mFilePath)).getChannel(); | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
try { | ||
mp4file.writeContainer(fc); | ||
fc.close(); | ||
return true; | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
|
||
} | ||
|
||
public void resumeRecording(){ | ||
isPaused=false; | ||
startRecording(); | ||
} | ||
|
||
private void startTimer() { | ||
mTimer = new Timer(); | ||
mIncrementTimerTask = new TimerTask() { | ||
|
@@ -171,4 +287,17 @@ private Notification createNotification() { | |
|
||
return mBuilder.build(); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Class used for the client Binder. Because we know this service always | ||
* runs in the same process as its clients, we don't need to deal with IPC. | ||
*/ | ||
public class LocalBinder extends Binder { | ||
public RecordingService getService() { | ||
// Return this instance of LocalService so clients can call public methods | ||
return RecordingService.this; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These seem like spurious changes?