Recording and playback of video files utilizes the MediaPlayer/MediaRecorder framework in a similar way to the audio examples discussed previously. For completeness, the steps are included here. First, if video is recorded, set the permission in the AndroidMani-fest XML file:

<uses-permission android:name="android.permission.RECORD_VIDEO" />

Then, the specification has different choices from the audio examples (note, default can also be used and is the same as the first choice in these lists):

■ MediaRecorder.VideoSource:

■ MediaRecorder.OutputFormat:

■ three_GPP—3GPP media file format

■ MediaRecorder.VideoEncoder:

■ MPEG_4_SP-MPEG4 Simple Profile

The steps to record video are

1. Create an instance of the MediaRecorder: MediaRecorder m_Recorder = new MediaRecorder();

2. Specify the source of media, which currently is only the camera: m_Recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

3. Set the output file format and encoding: m_Recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); m_Recorder.setAudioEncoder(MediaRecorder.AudioEncoder.H263);

4. Set the path for the file to be saved: m_Recorder.setOutputFile(path);

5. Prepare and start the recording: m_Recorder.prepare(); m_Recorder.start();

For playback, the steps are

1. Create an instance of the MediaPlayer: MediaPlayer m_mediaPlayer = new MediaPlayer();

2. Specify the source of media. It can be created from a raw resource: m_mediaPlayer = MediaPlayer.create(this, R.raw.my_video);

Another option is to set as a file from the filesystem (which then also needs a prepare statement):

m_mediaPlayer.setDataSource(path); m_mediaPlayer.prepare();

In any case, these statements need to be surrounded by a try-catch block because the specified resource might not exist.

3. Start playback of the video: m_mediaPlayer.start();

4. When the playback is done, stop the MediaPlayer instance and release it to free up resources:

m_mediaPlayer.stop(); m_mediaPlayer.release();

This page intentionally left blank

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment