5
5
import android .net .Uri ;
6
6
import android .os .Environment ;
7
7
import android .text .TextUtils ;
8
+ import android .util .Log ;
8
9
9
10
import com .coremedia .iso .boxes .Container ;
10
11
import com .googlecode .mp4parser .FileDataSourceImpl ;
@@ -32,29 +33,25 @@ class StreamRecorder {
32
33
private final AtomicReference <String > recordingFilename = new AtomicReference <>();
33
34
34
35
private final File mediaRootDir ;
35
- private final MediaScannerConnection mediaScanner ;
36
+ private final Context context ;
37
+
38
+ private final MediaScannerConnection .OnScanCompletedListener scanCompletedListener = new MediaScannerConnection .OnScanCompletedListener () {
39
+ @ Override
40
+ public void onScanCompleted (String path , Uri uri ) {
41
+ Timber .i ("Media file %s was scanned successfully: %s" , path , uri );
42
+ }
43
+ };
36
44
37
45
private ExecutorService asyncExecutor ;
38
46
39
47
private BufferedOutputStream h264Writer ;
40
48
41
49
StreamRecorder (Context context ) {
50
+ this .context = context ;
42
51
this .mediaRootDir = new File (context .getExternalFilesDir (Environment .DIRECTORY_MOVIES ), "stream" );
43
52
if (!this .mediaRootDir .exists ()) {
44
53
this .mediaRootDir .mkdirs ();
45
54
}
46
-
47
- this .mediaScanner = new MediaScannerConnection (context , new MediaScannerConnection .MediaScannerConnectionClient () {
48
- @ Override
49
- public void onMediaScannerConnected () {
50
-
51
- }
52
-
53
- @ Override
54
- public void onScanCompleted (String path , Uri uri ) {
55
- Timber .i ("Media file %s was scanned successfully: %s" , path , uri );
56
- }
57
- });
58
55
}
59
56
60
57
String getRecordingFilename (){
@@ -135,22 +132,21 @@ void onNaluChunkUpdated(byte[] payload, int index, int payloadLength) {
135
132
}
136
133
137
134
void convertToMp4 (final String filename ) {
135
+ if (TextUtils .isEmpty (filename )) {
136
+ Timber .w ("Invalid media filename." );
137
+ return ;
138
+ }
139
+
140
+ final File rawMedia = new File (mediaRootDir , filename );
141
+ if (!rawMedia .exists ()) {
142
+ Timber .w ("Media file doesn't exists." );
143
+ return ;
144
+ }
145
+
138
146
asyncExecutor .execute (new Runnable () {
139
147
@ Override
140
148
public void run () {
141
- Timber .i ("Starting h264 conversion process." );
142
-
143
- Timber .i ("Converting media file %s" , filename );
144
- if (TextUtils .isEmpty (filename )) {
145
- Timber .w ("Invalid media filename." );
146
- return ;
147
- }
148
-
149
- File rawMedia = new File (mediaRootDir , filename );
150
- if (!rawMedia .exists ()) {
151
- Timber .w ("Media file doesn't exists." );
152
- return ;
153
- }
149
+ Timber .i ("Starting h264 conversion process for media file %s." , filename );
154
150
155
151
try {
156
152
H264TrackImpl h264Track = new H264TrackImpl (new FileDataSourceImpl (rawMedia ));
@@ -171,7 +167,8 @@ public void run() {
171
167
172
168
//Add the generated file to the mediastore
173
169
Timber .i ("Adding the generated mp4 file to the media store." );
174
- mediaScanner .scanFile (mp4Media .getAbsolutePath (), null );
170
+ MediaScannerConnection .scanFile (context ,
171
+ new String []{mp4Media .getAbsolutePath ()}, null , scanCompletedListener );
175
172
176
173
} catch (IOException e ) {
177
174
Timber .e (e , e .getMessage ());
0 commit comments