1. Demand Analysis
In Android under the timing of the video, the first can be implemented in the Network Camera Settings page, at some point in the video, but this situation is not the case that the video is not kept in the camera local or uploaded to the camera manufacturers to provide the server is not adapted to all the needs.
Second, the video stream of the camera can be saved under Android with FFmpeg, and the timer function can be implemented in the Android program.
2. Concrete Realization
(1) Timing
Timed recordings are recorded at some point in the day or at some point in time, which can be achieved with the help of an Android alarm clock mechanism.
The Android alarm clock implementation is based on the Alarmmanager class, which has two main methods:
Alarmmanager manager = (Alarmmanager) getsystemservice (Alarm_service);
public void Set (int type, long triggeratmillis, pendingintent operation) { try { manager.set (type, Triggeratmillis, operation); } catch (RemoteException ex) { } }
<span style= "FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:14PX;" > Public void setrepeating (int type, long triggeratmillis, long intervalmillis, pendingintent operation) { c2/>try { manager.setrepeating (type, Triggeratmillis, Intervalmillis, operation); } catch (RemoteException ex ) { } } </span>
the first method is to simply register an alarm, and the second is to set a repeating alarm, which is a time interval.
The second method is used here, which is implemented as follows:
<span style= "White-space:pre" ></span>intent Intent = new Intent (Alarmsetingactivity.this, Alarmreceiver.class); Pendingintent sender = Pendingintent.getbroadcast (alarmsetingactivity.this, 0, intent, 0); Long firsttime = Systemclock.elapsedrealtime ();//After boot to the current run time (including sleep time) long systemTime = System.currenttimemill Is (); <span style= "White-space:pre" ></span>calendar Calendar = Calendar.getinstance (); Calendar.settimeinmillis (System.currenttimemillis ()); Calendar.settimezone (Timezone.gettimezone ("gmt+8")); Here time zone needs to set up, otherwise there will be a 8-hour difference calendar.set (Calendar.minute, Mminute); Calendar.set (Calendar.hour_of_day, Mhour); Calendar.set (Calendar.second, 0); Calendar.set (Calendar.millisecond, 0); Select a daily timing time long selecttime = Calendar.gettimeinmillis (); If the current time is greater than the set time, start if (SystemTime > Selecttime) {toast.maketext (Alarmsetingactivity.this, "set time is less than current") from the next day's set time Toast.length_short). Show (); Calendar.add (Calendar.day_of_month, 1); Selecttime = Calendar.gettimeinmillis (); }//Calculates the time difference between now and the set time long = Selecttime-systemtime; Firsttime + = time; Make an alarm registration Alarmmanager manager = (Alarmmanager) getsystemservice (Alarm_service); Manager.setrepeating (Alarmmanager.elapsed_realtime_wakeup, Firsttime, 6*60*60*1000, sender); LOG.I (TAG, "time = =" + Time + ", selecttime =====" + Selecttime + ", SystemTime = =" + SYSTEMTI Me + ", firsttime = = =" + Firsttime); Toast.maketext (Alarmsetingactivity.this, "Set timed successfully!", Toast.length_long). Show (); Explain the first parameter of Setrepeat:
ALARMMANAGER.RTC, the hardware alarm clock, does not wake up the phone (or other devices) hibernate, when the phone does not fire alarm when hibernating.
alarmmanager.rtc_wakeup, hardware alarm clock, when the alarm clock hair 躰 wake up the phone sleep;
Alarmmanager.elapsed_realtime, the real time passes the alarm clock, does not wake the phone to sleep, when the phone sleeps, does not emit the alarm clock.
Alarmmanager.elapsed_realtime_wakeup, real time elapsed alarm clock, when the alarm clock hair 躰 wake up the phone sleep;
the biggest difference between RTC alarms and Elapsed_realtime is that the former can trigger alarm events by modifying the phone's time, which passes through the real time, even in the dormant state, and the time is calculated
When the alarm is registered, the broadcast is sent and we also accept the broadcast:
<span style= "WHITE-SPACE:PRE;" ></span>public class Alarmreceiver extends Broadcastreceiver {<span style= "white-space:pre;" ></span> @Override <span style= "WHITE-SPACE:PRE;" ></span>public void OnReceive (context context, Intent Intent) {<span style= "white-space:pre;" ></span>toast.maketext (Context, "~ ~ Start recording Video ~ ~", Toast.length_long). Show (); new MyThread (). Start (); Class MyThread extends Thread {public void run () {try {@SuppressWarnings ("unused") Process proc = Runtime.getruntime (). exe C ("su-c./data/misc/command_record.sh"); <span style= "WHITE-SPACE:PRE;" ></span>} catch (IOException e) {<span style= "white-space:pre;" ></span>//TODO auto-generated catch Block<span style= "WHITE-SPACE:PRE;" ></span>e.printstacktrace (); <span style= "WHITE-SPACE:PRE;" ></span>}<span style= "WHITE-SPACE:PRE;" ></span>}<span style= "WHITE-SPACE:PRE;" ></span>} <span style= "White-space:pre;" ></span>} Also need to be inAndroidmanifest.xml Registration:
<span style= "WHITE-SPACE:PRE;" ></span><receiver android:name= "com.wr.alarmrecord.AlarmReceiver" android:process= ": Remote "> </receiver>
android:process= ": remote", represented in the application, when the service is needed, a new process is automatically created. And if it is android:process= "remote", there is no ":" semicolon, then the global process is created, and the process is shared by different applications.
(2) Video
The implementation of the video is implemented in the Android downgrade with FFmpeg execution command line command, the idea is:
After the timer starts, every time calls the script executes the recording command, saves the recording to the SD card, but because the SD card capacity is limited, so when the number of saved reaches a certain amount to delete the file, the video name is by the camera names plus the date. The script is as follows:
<span style= "White-space:pre" ></span>#!/system/bin/sh<span style= "White-space:pre" ></span >file= ". Count.rec" <span style= "White-space:pre" ></span>count=0<span style= "White-space:pre" ></span>da= ' Date ' +%y-%m-%d-%h-%m-%s "' <span style=" white-space:pre "></span>camera=" Hai Kang wei View " <span style= "White-space:pre" ></span>if [-f $file]; Then <span style= "White-space:pre" ></span> Read count < $file fi<span style= "WHITE-SPACE:PR E "></span>if [$ ($count + 1)-ne]<span style=" White-space:pre "></span>then <span style=" W Hite-space:pre "></span> Echo $ (($count + 1)) echo $ (($count + 1)) > $file <span style=" white-s Pace:pre "></span>else Echo $ (($count + 1)) echo 0 > $file cd/mnt/sdcard/rm-r admin/< Span style= "White-space:pre" ></span>fi cd/data/misc/ffmpeg/./ffmpeg-y-I Rtsp://admiN:[email protected]-vcodec copy-acodec copy-t 0:1:0-f flv/mnt/sdcard/admin/$camera-$da. flv
3.
4. Source code Download:
http://download.csdn.net/detail/xdwyyan/8691263
5. Reference
< Span style= "White-space:pre" >http://jakend.iteye.com/blog/1980842
http://blog.csdn.net/liuhaomatou/article/details/22445755
Android uses FFmpeg to implement timed recording