You are on page 1of 23

Pht trin ng

dng
Smartphone
Ti liu lu hnh ni b
y l ti liu tham kho s dng trong mn hc Lp trnh ng dng Smartphone Android
c tng hp, bin son t nhiu ngun bi cc thnh vin ca Nhm nghin cu v ng dng cng
ngh A106-i hc Hoa Sen.
Nhm A106-http://profiles. hoasen. edu. vn/groups/584/
Pht trin ng dng Smartphone Android



Pht trin ng
dng Smartphone

Phn 07: Media
L c Huy
Email: leduchuy89vn@gmail.com
Pht trin ng dng Smartphone Android

Mc lc
1 Chi tp tin m thanh ............................................................................................. 3
1.1 S dng phng thc setDataSource() .......................................................... 7
1.2 Chi tp tin m thanh t trong th nh SD .................................................... 8
2 Chi tp tin Video ................................................................................................. 12
2.1 Chi tp tin video t th SD .......................................................................... 13
3 Mt s lu khi lm vic vi MediaPlayer v ViewView ...................................... 13
4 Ghi m thanh ........................................................................................................ 14
5 Ghi video ............................................................................................................... 18


Pht trin ng dng Smartphone Android

Android h tr hm chi audio v video trong gi android.media. Phn chnh ca gi ny l lp
android.media.MediaPlayer. Lp ny c s dng trong phn nghe audio v video, c th c c
ni dung t cc ngun sau: web, tp tin media nh km trong tp .apk file, th SD. MediaPlayer c th
c c cc tp c phn m rng sau: 3GPP (.3gp), MP3 (.mp3), MIDI (.mid and others),
PCM/WAVE (.wav), and MPEG-4(.mp4).
To mi mt project vi cc thng s sau:
Project name: AudioExample
Build target: Android 2.3.3.
Application name: Audio Example
Package name: niit.android
Create Activity: main
1 Chi tp tin m thanh
u tin, ta xy dng ng dng n gin chi tp tin MP3 t Web. Sau ta s s dng hm
setDataSource() chy ni dung t file .apk hoc t th nh.
Xy dng mt Activity dng chi mt tp tin m thanh vi giao din nh sau:

Tham kho ni dung tp tin xml nh du giao din ca activity dng chi nhc trn:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
Pht trin ng dng Smartphone Android

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<Button
android:id="@+id/btnStartPlayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Playing Audio" />
<Button
android:id="@+id/btnResumePlayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Resume Player" />
<Button
android:id="@+id/btnPausePlayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pause Player" />
</LinearLayout>
Dng mt AudioExampleActivity vi ni dung nh sau:
package niit.android;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AudioExampleActivity extends Activity {
static final String AUDIO_PATH =
"http://www.androidbook.com/akc/filestorage/android/documentfile
s/3389/play.mp3";

private MediaPlayer mediaPlayer;

private int playbackPosition = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_example);
Button btnStartPlayer =
(Button)findViewById(R.id.btnStartPlayer);
Button btnPausePlayer =
(Button)findViewById(R.id.btnPausePlayer);
Pht trin ng dng Smartphone Android

Button btnResumePlayer =
(Button)findViewById(R.id.btnResumePlayer);
btnStartPlayer.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View view) {
try {
playAudio(AUDIO_PATH);
} catch (Exception e) {
e.printStackTrace();
}
}
});
btnPausePlayer.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer != null) {
playbackPosition =
mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
}
}
});
btnResumePlayer.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer != null &&
!mediaPlayer.isPlaying()) {
mediaPlayer.seekTo(playbackPosition);
mediaPlayer.start();
}
}
});
}

private void playAudio(String url) throws Exception {
killMediaPlayer();
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();
}

private void killMediaPlayer() {
if (mediaPlayer != null) {
Pht trin ng dng Smartphone Android

try {
mediaPlayer.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}

@Override
protected void onDestroy() {
super.onDestroy();
killMediaPlayer();
}
}
Trong trng hp ny ta chy mt tp Mp3 file t a ch web. V vy ta cn thm khai bo sau
<uses-permission android:name="android.permission.INTERNET" />
vo file manifest. Bn c th thy trong hm onCreate(), ta setClickListener cho c 03
button.
Khi ngi dng nhn vo button btnStartPlayer, phng thc playAudio() s c gi. Trong
phng thc playAudio(), mt th hin ca MediaPlayer c to ra, d liu ca player c gn l
mt ng dn n mt tp tin MP3. MediaPlayer bt u chi tp tin.
Khi ngi dng nhn vo button bntPausePlayer, ta ly c v tr hin ti ca player bng hm
getCurrentPosition() v gn vo bin playbackPosition nh ngha trn. Sau ta dng vit chi
nhc li bng phng thc pause().
Khi ngi dng nhn vo button btnResumePlayer, phng thc seedTo() c gi vi
parameter l playbackPosition c truyn vo, khi ta gi hm start() mediaPlayer s bt u chi ti v
tr playbackPosition thay v t u.
Lp MediaPlayer cn cha hm stop(). Ch rng nu bn stop player bng hm ny. Bn cn
gi hm prepare() trc khi gi start() mt ln na. nhng nu bn gi pause(), bn c th gi start
ngay m khng cn gi prepare().
Ngoi ra, hy chc chn l bn s gi hm release() 1 ln khi bn s dng xong i tng
MediaPlayer.Trong v d trn, ta lm vic ny trong hm killMediaPlayer().
Trong phn trn ta chy mt tp tin audio t web. Lp MediaPlayer cn h tr chy media t
file .apk. thc hin vic ny, ta thm mt th mc tn raw vo trong th mc /res nu project ca
bn cha c. Sau , ta copy mt tp tin Mp3 vo /res/raw vi tn l music_file.mp3.
B sung thm phng thc sau :
private void playLocalAudio()throws Exception
{
Pht trin ng dng Smartphone Android

mediaPlayer = MediaPlayer.create(this,
R.raw.music_file);
mediaPlayer.start();
}
y ta gi lm static MediaPlayer.create() to mt th hin ca MediaPlayer cho resource
c truyn vo v sau gn n cho th hin mediaPlayer ca chng ta v cui cng l start() n.
1.1 S dng phng thc setDataSource()
trn chng ta gi hm create load file audio t mt raw resource. Vi cch ny, ta khng
cn phi gi setDataSource(). Nhng trong trng hp th hin MediaPlayer ca ta s dng constructor
mc nh hoc ni dung media khng th truy cp c thong qua resource ID hoc URL nh trn th
ta s s dng phng thc setDataSource().
Phng thc setDataSource c overload thnh nhiu version v vy bn c th s dng
ty chnh datasource cho nhng g bn cn.
Tin hnh b sung thm phng thc sau:
private void playLocalAudio_UsingDescriptor() throws Exception {
AssetFileDescriptor fileDesc =
getResources().openRawResourceFd(R.raw.music_file);
if (fileDesc != null) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(fileDesc.getFileDescriptor()
, fileDesc.getStartOffset(), fileDesc.getLength());
fileDesc.close();
mediaPlayer.prepare();
mediaPlayer.start();
}
}
Phng thc getResources() s tr v i tng kiu Resources, i tng ny s c dng
truy xut n cc ti nguyn nh km trong tp tin ci t ng dng (Hnh nh, m thanh). Sau
s dng phng thc openRawResourceFd() ly v mt i tng kiu AssetFileDescriptor, y l
i tng cha cc thng tin m t v tp tin audio cha trong th mc res/raw. Cui cng dng
phng thc setDataSource() vi tham s truyn vo l i tng AssetFileDescriptor cng vi cc
thng s im bt u chi tp tin m thanh l u tp v im kt thc l cui tp. Nu bn mun chi
ton b tp tin th c th s dng mt phng thc n gin hn l setDataSource(FileDescriptor desc).
S dng mt trong cc phng thc overload li phng thc setDataSource() vi mt tham s
truyn vo l FileDescriptor chi cc tp tin media t trong th mc data ca ng dng. V l do
bo mt nn MediaPlayer khng c php truy xut th mc data ca ng dng. Tuy nhin, ng dng
ca bn c th m mt tp tin v s dng n trong phng thc setDataSource() thng qua i tng
FileDescriptior. Gi s bn c mt s tp tin v th mc cha tp tin media t ti a ch
Pht trin ng dng Smartphone Android

data/data/APP_PACKAGE_NAME/, bn c th gi mt s phng thc t Activity n ly a ch th
mc thay v dng a ch c nh bng code java.
VD: Dng phng thc getFilesDir() ly ng dn n tp th mc
data/data/APP_PACKAGE_NAME, phng thc getCacheDir() ly th mc cha cc tp tin cache
ca ng dng.
Bng cch ny th ng dng ca bn s c quyn truy xut, chnh sa cc tp tin t trong th
mc data ca ng dng. Tham kho thm phn Lu tr d liu bit cch c ghi tp tin.
C s khc bit gia cc tp tin media t trong th mc res/raw v th mc data ca ng dng.
Th mc res/raw l mt phn vt l ca tp tin ci t c phn m rng .apk, cc tp tin t trong y
l tp tin tnh, khng th thay i sau khi ng dng c xut thnh tp tin ci t c phn m rng
.apk. Ni dung ca th mc data ca ng dng th c th thay i c.
1.2 Chi tp tin m thanh t trong th nh SD
chi mt tp tin media t trong th nh SD ca vic u tin l chp tp tin m thanh ln
th nh my o:
Bc 01: M File Explorer chp tp tin m thanh ln th nh ca my o:
Chn menu Windows>Show View>Orther

Chn Android>File Explorer:
Pht trin ng dng Smartphone Android


Click chn Open Perspective

Chn DDMS
Pht trin ng dng Smartphone Android


Chn my o lit k tab Devices v dng File Explorer m th mc sdcard nh trong
hnh:

Bc 02: Push mt tp tin media vo th mc sdcard ca my o
Pht trin ng dng Smartphone Android


Ch ng dn n tp tin media a tp tin v th sd ca my o:

Pht trin ng dng Smartphone Android

chi tp tin audio k trn ta ch cn thay i gi tr ca bin AUDIO_PATH ca phng
thc playAudio() thnh:
static final String AUDIO_PATH = "/sdcard/music_file.mp3";
2 Chi tp tin Video
To tp tin giao din video_example.xml c ni dung nh sau:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal">
<VideoView
android:id="@+id/videoView"
android:layout_width="200px"
android:layout_height="200px" />
</LinearLayout>
To mi mt activity vi ni dung nh sau:
package niit.android;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoExampleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.video_example);
VideoView videoView =
(VideoView)this.findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);

videoView.setVideoURI(Uri.parse("http://www.androidbook.com
/akc/filestorage/android/documentfiles/3389/movie.mp4"));
videoView.requestFocus();
videoView.start();
}
}
Pht trin ng dng Smartphone Android

Trong on activity trn, ta s dng VideoView hin th video. Nhng thay v t to cc nt
nhn iu khin video , ta to mediaControler cung cp cc nt nhn cho chng ta. Trong
on code, ta gn MediaControler cho videoView bng cch gi setMediaControler() cho php play,
pause, and seek-to. Nu bn mun iu khin bng cc nt nhn ca mnh, bn c th gi hm
videoView.start(), videoView.pause(), videoView.stopPlayback(), v videoView.seekTo().
Ngoi ra bn c th play video trc tip bng MediaPlayer. Nu bn tr li vd v play
audio trn v sa ng dn AUDIO_PATH n 1 video, bn vn s nghe c video ny
nhng bn khng th thy c hnh nh.
2.1 Chi tp tin video t th SD
Trong khi MediaPlayer s dng hm setDataSource() th VideoView s dng hm
setVideoPath() hoc setVideoURI(). Gi s bn t mt tp tin video vo th nh SD nh cch trnh
by pha trn v thay i code trn bng cch thay hm setVideoURI() thnh setVideoPath(). V b
ng dn n tp tin media vo hm setVideoPath() nh mt tham s. Khi bn chy li ng dng, bn
s nghe v nhn thy video trn VideoView.
videoView.setVideoPath("/sdcard/movie.mp4");
Tuy nhin, bn cng c th s dng setVideoURI() ly d liu t SDcard v cho kt qu
tng t nh setVideoPath:
videoView.setVideoURI(Uri.parse("file:///sdcard/movie.mp4"));
3 Mt s lu khi lm vic vi MediaPlayer v ViewView
Mt khi gn data source cho mt i tng MediaPlayer th ta khng d dng thay i n
vi mt data source khc. Thay vo tt nht bn nn to mt MediaPlayer mi hoc gi phng
thc MediaPlayer.reset() khi to li i tng MediaPlayer.
Sau khi gi phng thc MediaPlayer.prepare() bn c th gi phng thc
getCurrentPosition(), getDuration() v isPlaying() kim tra trng thi ca MediaPlayer. Ngoi ra bn
cn c th gi phng thc setLooping() v setVolume() yu cu MediaPlayer chi lp i lp li tp
tin media ca bn cng nh thay i m lng.
Sau khi gi phng thc Media.start() bn c th gi cc phng thc MediaPlayer.pause(),
MediaPlayer.stop() v MediaPlayer.seekTo().
Mi MediaPlayer s t to mt lung mi chi tp tin media bn ch nh, chnh v vy cn
phi gi phng thc MediaPlayer.release() khi khng cn s dng MediaPlayer. Nu bn s dng
VideoView trnh din mt tp tin video th VideoView s lm vic ny cho bn.
C th s dng MediaPlayer chi tp tin video, tt nhin l ta ch c th nghe c m thanh
ca tp tin video ny m khng xem c hnh nh.
Pht trin ng dng Smartphone Android

4 Ghi m thanh
To mi mt tp tin audio_record.xml vi ni dung nh sau:
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/record.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/bgnBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Begin Recording" />
<Button
android:id="@+id/stpBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop Recording" />

<Button
android:id="@+id/playRecordingBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play Recording" />

<Button
android:id="@+id/stpPlayingRecordingBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop Playing Recording" />

</LinearLayout>
To mi mt Activity vi ni dung:
package niit.android;

import java.io.File;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
Pht trin ng dng Smartphone Android


public class AudioRecorderActivity extends Activity {
private MediaPlayer mediaPlayer;

private MediaRecorder recorder;

private static final String OUTPUT_FILE =
"/sdcard/recordaudio.3gpp";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio_record);
Button startBtn = (Button)findViewById(R.id.bgnBtn);
Button endBtn = (Button)findViewById(R.id.stpBtn);
Button playRecordingBtn =
(Button)findViewById(R.id.playRecordingBtn);
Button stpPlayingRecordingBtn =
(Button)findViewById(R.id.stpPlayingRecordingBtn);
startBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
beginRecording();
} catch (Exception e) {
e.printStackTrace();
}
}
});
endBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
stopRecording();
} catch (Exception e) {
e.printStackTrace();
}
}
});
playRecordingBtn.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View view) {
try {
playRecording();
} catch (Exception e) {
e.printStackTrace();
Pht trin ng dng Smartphone Android

}
}
});
stpPlayingRecordingBtn.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View view) {
try {
stopPlayingRecording();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

private void beginRecording() throws Exception {
killMediaRecorder();
File outFile = new File(OUTPUT_FILE);
if (outFile.exists()) {
outFile.delete();
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_G
PP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
;
recorder.setOutputFile(OUTPUT_FILE);
recorder.prepare();
recorder.start();
}

private void stopRecording() throws Exception {
if (recorder != null) {
recorder.stop();
}
}

private void killMediaRecorder() {
if (recorder != null) {
recorder.release();
}
}

private void killMediaPlayer() {
if (mediaPlayer != null) {
Pht trin ng dng Smartphone Android

try {
mediaPlayer.release();
} catch (Exception e) {
e.printStackTrace();
}
}
}

private void playRecording() throws Exception {
killMediaPlayer();
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(OUTPUT_FILE);
mediaPlayer.prepare();
mediaPlayer.start();
}

private void stopPlayingRecording() throws Exception {
if (mediaPlayer != null) {
mediaPlayer.stop();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
killMediaRecorder();
killMediaPlayer();
}
}
Vic ghi m thanh trong Android s thng qua mt i tng MediaRecorder. Tuy nhin trc
tin ta cn b xung quyn ghi m thanh, v quyn ghi tp tin xung th nh cho ng dng bng cch b
xung tp AndroidManifest.xml:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Phng thc beginRecording() c cc lnh sau:
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(OUTPUT_FILE);
recorder.prepare();
recorder.start();
Pht trin ng dng Smartphone Android

Trc tin l to mi mt i tng MediaRecorder. Sau gi phng thc setAudioSource()
gn ngun m cn thu. Ngun m ny c th l t mic, t cuc gi n, t tp tin nhc ang pht
Phng thc setOutputFormat() thit lp nh dng m thanh v phng thc setAudioEncoder()
thit lp cch m ha m thanh v setOutPutFile() gn ng dn lu tp tin m thanh xung.
Ngoi ra cn cc phng thc hu dng l:
- setMaxDuration(): Dng gn thi gian ti a ca tp tin m thanh ghi thnh cng.
- setMaxFileSize(): Gn kch thc ti a ca tp tin m thanh ghi thnh cng.
5 Ghi video
To mi mt tp tin video_record.xml vi ni dung nh sau:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<Button android:id="@+id/bgnBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Begin
Recording"
android:enabled="false" />

<Button android:id="@+id/stpBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Stop
Recording" />

<Button android:id="@+id/playRecordingBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Play
Recording" />

<Button android:id="@+id/stpPlayingRecordingBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop Playing Recording" />

<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">

Pht trin ng dng Smartphone Android

<VideoView android:id="@+id/videoView"
android:layout_width="176px"
android:layout_height="144px" />

</RelativeLayout>
</LinearLayout>
To mi mt Activity vi ni dung nh sau:
package niit.android;

import java.io.File;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoRecorderActivity extends Activity implements
SurfaceHolder.Callback {
private MediaRecorder recorder = null;

private static final String OUTPUT_FILE =
"/sdcard/videooutput.mp4";

private static final String TAG = "RecordVideo";

private VideoView videoView = null;

private Button startBtn = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startBtn = (Button)findViewById(R.id.bgnBtn);
Button endBtn = (Button)findViewById(R.id.stpBtn);
Button playRecordingBtn =
(Button)findViewById(R.id.playRecordingBtn);
Button stpPlayingRecordingBtn =
(Button)findViewById(R.id.stpPlayingRecordingBtn);
videoView =
(VideoView)this.findViewById(R.id.videoView);
Pht trin ng dng Smartphone Android

final SurfaceHolder holder = videoView.getHolder();
holder.addCallback(this);

holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
startBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
beginRecording(holder);
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
}
});
endBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
stopRecording();
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
}
});
playRecordingBtn.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View view) {
try {
playRecording();
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
}
});
stpPlayingRecordingBtn.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View view) {
try {
stopPlayingRecording();
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
Pht trin ng dng Smartphone Android

}
}
});
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
startBtn.setEnabled(true);
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}

@Override
public void surfaceChanged(SurfaceHolder holder, int
format, int width, int height) {
Log.v(TAG, "Width x Height = " + width + "x" +
height);
}

private void playRecording() {
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
videoView.setVideoPath(OUTPUT_FILE);
videoView.start();
}

private void stopPlayingRecording() {
videoView.stopPlayback();
}

private void stopRecording() throws Exception {
if (recorder != null) {
recorder.stop();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if (recorder != null) {
recorder.release();
}
}

Pht trin ng dng Smartphone Android

private void beginRecording(SurfaceHolder holder) throws
Exception {
if (recorder != null) {
recorder.stop();
recorder.release();
}
File outFile = new File(OUTPUT_FILE);
if (outFile.exists()) {
outFile.delete();
}
try {
recorder = new MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource
.CAMERA);
recorder.setAudioSource(MediaRecorder.AudioSource
.MIC);
recorder.setOutputFormat(MediaRecorder.OutputForm
at.MPEG_4);
recorder.setVideoSize(176, 144);
recorder.setVideoFrameRate(15);
recorder.setVideoEncoder(MediaRecorder.VideoEncod
er.MPEG_4_SP);
recorder.setAudioEncoder(MediaRecorder.AudioEncod
er.AMR_NB);
recorder.setMaxDuration(5000); // limit to 5
seconds
recorder.setPreviewDisplay(holder.getSurface());
recorder.setOutputFile(OUTPUT_FILE);
recorder.prepare();
recorder.start();
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
}
}
Ta cng ln lt gi phng thc setVideoSource() thit lp ngun video cn thu
(Camera), setAudioSource() thit lp ngun m thanh cn thu, setOutputFormat() thit lp nh
dng xut ra (MP4), setVideoSize() gn kch thc tp video cn ghi, setVideoFrameRate() thit
lp s khung hnh trn giy, setAudioEncoder() thit lp cch m ha m thanh, setMaxDuration()
thit lp s giy ti a ghi li, phng thc setPreviewDisplay() xem trc phn video ang
quay.

You might also like