Tải bản đầy đủ (.pdf) (43 trang)

Bài giảng Android nâng cao: Bài 4 - Trương Xuân Nam

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.12 MB, 43 trang )

MobiPro

ANDROID NÂNG CAO
BÀI 4: Media Services (continue) +
Location Base Services


MobiPro

N à
1. Media Services (continue)
– Video
– TTS
– Camera

2. Location Base Services
– Global Positioning Services
– Geocoding Locations
– Mapping Locations

TR

NGàXUÂNàNáM

2


MobiPro

P


à1.1

Video

TR

NGàXUÂNàNáM

3


MobiPro

Video playback
 á

–ì à
–ì à

 C

àOìà à à

à

à

à à

à


à

à
à

à à
à

à

àVideoView à
à àMediaController
àMediaPlayer và SurfaceView

à à
à
à
à
à
à à
à à
à à
à
à à
 P
à
à
à à à
à à à

à à
à
hàm setPreviewCallback
à à
à
à
à
à
TR

à

à

NGàXUÂNàNáM

à

à

à à
à à à

à
à

à

à
à

à

à

à

à

à
à

4


MobiPro

VideoView + MediaController
 V
 V

V
V

à à
à

à

à à
à à à

à
à à à
à à
à
à
à
à
à
àstart, pause, suspend, resume,
stopPlayback, seekTo(millis)
 M
C
à à
à
à à à
à
à
à
à à
à
à à
à à à
à
à
à
à
à
à à ànext và prev
 V
V à àM

C
à
à
à à à
à
à à
à à
à
à
à à
à
à
à à
à
à à à à à
à
TR

NGàXUÂNàNáM

5


MobiPro

VideoView + MediaController
xmlns:android=" />xmlns:tools=" />android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >

android:id="@+id/videoView1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
TR

NGàXUÂNàNáM

6


MobiPro

VideoView + MediaController
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
videoView.setMediaController(new MediaController(this));
videoView.setVideoURI(Uri.parse("android.resource://" +
getPackageName() + "/" + R.raw.teamwork));
videoView.start();
}

TR


NGàXUÂNàNáM

7


MobiPro

VideoView + MediaController
 M

C

à

à

à

à à

à

à

à

à

à

à à
à
 MediaController dùng hàm setAnchorView à à à
à à à
à à à
à
à à
à
à
à
à
à
à àV
V à à à
à
 M
C
à
à à à à à
à
à à à
à
à à
à à
à
à
à à à
à
à à
àM

C
 V
V à
à
à à
à à à
à
à à
à à à
à
à
à à à à
à à à
à
à
à
à àV
V
TR

NGàXUÂNàNáM

8


MobiPro

MediaPlayer + SurfaceView
 M
 ì


 ì
–T
–ì

P

à à à à
à
à
à
à à à
à
à à
à
à
à
à
V à à
à
à
à à à
à
à à à
à à
à
à à à à à à
à
à à
à

à
à à
à à
à à
à à
à
à à
à
V à
à à à à à
à à à
à
à
à à
à
à à
à
à

à

à à à
à
V à
à à

à

à


à
à

à

à
TR

à

à

àì

à

NGàXUÂNàNáM

V

à

à à

à

à

à


à

à à
9


MobiPro

MediaPlayer + SurfaceView
public class MainActivity extends Activity
implements SurfaceHolder.Callback, OnPreparedListener {
private MediaPlayer mediaPlayer;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SurfaceView vidSurface = new SurfaceView(this);
vidSurface.getHolder().addCallback(this);
setContentView(vidSurface);
}
public void surfaceChanged(SurfaceHolder s, int a, int b, int c) {
}
public void surfaceDestroyed(SurfaceHolder arg0) {
}
TR

NGàXUÂNàNáM

10



MobiPro

MediaPlayer + SurfaceView
public void surfaceCreated(SurfaceHolder arg0) {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(arg0);
mediaPlayer.setDataSource(this,
Uri.parse("android.resource://" +
getPackageName() + "/" + R.raw.teamwork));
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (Exception e) { }
}
public void onPrepared(MediaPlayer mp) { mediaPlayer.start(); }
}
TR

NGàXUÂNàNáM

11


MobiPro

P

à


Text to speech

TR

NGàXUÂNàNáM

12


MobiPro

Text to speech
 C
 C

à
à
à

 C à
 C à
 H à

à
à

à

à
à


à
à à à

à à
à

à
à

à

à

à

à
à

à

à

à

à
à
à
à à


à
à

à

à
à

à

à
à

à

– TextToSpeech: phát âm
– TextToSpeechService: customize engine
TR

NGàXUÂNàNáM

13


MobiPro

Text to speech
g i activity m c đ nh đ ki m tra có d li u cho TTS ch a
Intent intent = new Intent(Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(intent, CODE);


x l d li u do activity tr v
protected void onActivityResult(int req, int result, Intent data) {
if (req == CODE)
if (result != Engine.CHECK_VOICE_DATA_PASS)
startActivity(new Intent(Engine.ACTION_INSTALL_TTS_DATA));
else {
// d li u đã có t o đ i t ng TTS m c đ nh
}
}
TR

NGàXUÂNàNáM

14


MobiPro

Text to speech
TextToSpeech talker = new TextToSpeech(this, new OnInitListener() {
public void onInit(int status) {
talker speak Hi There
TextToSpeech QUEUE FLUSH null
}
}
TextToSpeech tts = new TextToSpeech(this, new OnInitListener() {
public void onInit(int status) {
if (status != TextToSpeech.SUCCESS) return;
tts.setLanguage(Locale.ENGLISH);

tts.setPitch(0.8f);
tts.setSpeechRate(1.0f);
}
});
TR

NGàXUÂNàNáM

15


MobiPro

P

à

Camera

TR

NGàXUÂNàNáM

16


MobiPro

C


à

à

à

à

1. Previewing

2. After shutter is pressed

3. Image transferred from
CAMERA to the application
TR

NGàXUÂNàNáM

17


MobiPro

C

à

à

à


à

public class CameraDemo extends Activity {
ImageView mImageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImageView = (ImageView) findViewById(R.id.mImageView);
Intent mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(mIntent, 101);
}
private void showToast(Context context, String text) {
Toast.makeText(context, text, 1).show();
}
TR

NGàXUÂNàNáM

18


MobiPro

C

à

à


à

à

protected void onActivityResult(int req, int res, Intent intent) {
super.onActivityResult(req, res, intent);
if (res == RESULT_CANCELED) return;
if (req == 101) {
Bundle b = intent.getExtras();
Bitmap bm = (Bitmap) b.get("data");
mImageView.setImageBitmap(bm);
}
}
}

TR

NGàXUÂNàNáM

19


MobiPro

Camera
 L

à
à


àC
à

à

à
à

à

à
à

à

à

à
à

 D
à
à
à à
 C à
à
à
àá
à à
à

à à
 X à
à
à
à

à à
à à
à
à à
à
à
à
à à
à à
àM
à à
àC
à
à à
à

à
à

à

à
à à


àC
à

à

à

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
TR

NGàXUÂNàNáM

20


MobiPro

The takePicture Method

TR

NGàXUÂNàNáM

21


MobiPro


P

à2.1

Global Positioning Services

TR

NGàXUÂNàNáM

22


MobiPro

Global Positioning Services
 C à

à

à

à

à à à

à

à


–C à
à
à à à
à à
à
à
AndroidManifest.xml)
–K à à à
àL
M
à
à
àgetSystemService à
à à
à
LOCATION_SERVICE
–L à
à
à
à
à
à à
getAllProviders à
àgetBestProvider()
à
– Implement interface LocationListener à
à à à à
à
à à
–G à

à
àrequestLocationUpdates à
à à
à
TR

NGàXUÂNàNáM

à
à

à à

à

à

à

à

à
à

à

à à
à

à

à

23


MobiPro

Global Positioning Services
<uses-feature android:name="android.hardware.location" />
<uses-feature android:name="android.hardware.location.gps" />

TR

NGàXUÂNàNáM

24


MobiPro

AndroidManifest.xml
"android.permission.ACCESS_GPS" />
"android.permission.ACCESS_LOCATION" />
"android.permission.ACCESS_FINE_LOCATION" />
"android.permission.ACCESS_COARSE_LOCATION" />

"android.permission.INTERNET" />
"android.hardware.location" />
"android.hardware.location.gps" />
TR

NGàXUÂNàNáM

25


×