1. Trang chủ
  2. » Công Nghệ Thông Tin

Android SDK (phần 9) pps

50 300 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

Playing Audio and Video ❘ 367 Playing Video Using the Video View The simplest way to play back video is to use the VideoView control. The Video View includes a Surface on which the video is displayed and encapsulates and manages a Media Player to manage the video playback. The Video View supports the playback of local or streaming video as supported by the Media Player component. Video Views conveniently encapsulate the initialization of the Media Player. To assign a video to play, simply call setVideoPath or setVideoUri to specify the path to a local file, or the URI of a Content Provider or remote video stream: streamingVideoView.setVideoUri("http://www.mysite.com/videos/myvideo.3gp"); localVideoView.setVideoPath("/sdcard/test2.3gp"); Once initialized, you can control playback using the start , stopPlayback , pause ,and seekTo methods. The Video View also includes the setKeepScreenOn method to apply a screen Wake Lock that will prevent the screen from being dimmed while playback is in progress. Listing 11-3 shows the simple skeleton code used to assign a video to a Video View and control play- back. LISTING 11-3: Video playback using a Video View VideoView videoView = (VideoView)findViewById(R.id.surface); videoView.setKeepScreenOn(true); videoView.setVideoPath("/sdcard/test2.3gp"); if (videoView.canSeekForward()) videoView.seekTo(videoView.getDuration()/2); videoView.start(); [ do something ] videoView.stopPlayback(); Setting up a Surface for Video Playback The first step to using the Media Player to view video content is to prepare a Surface onto which the video will be displayed. The Media Player requires a SurfaceHolder object for displaying video content, assigned using the setDisplay method. If you do not assign a Surface Holder for your Media Player the video component will not be shown. To include a Surface Holder in your UI layout you use the SurfaceView control as shown in the sample layout XML in Listing 11-4. 368 ❘ CHAPTER 11 AUDIO, VIDEO, AND USING THE CAMERA LISTING 11-4: Sample layout including a Surface View <?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"> <SurfaceView android:id="@+id/surface" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> </SurfaceView> </LinearLayout> The Surface View is a wrapper around the Surface Holder object, which in turn is a wrapper around the Surface that is used to support visual updates from background threads. The Surface View will be examined in more detail in Chapter 15, but Listing 11-5 shows the skeleton code used to initialize a Surface View within your Activity, and assign it as a display target for your Media Player. Note that you must implement the SurfaceHolder.Callback interface. Surface Holders are created asynchronously, so you must wait until the surfaceCreated handler has been fired before assigning the returned Surface Holder object to the Media Player. LISTING 11-5: Initializing and assigning a Surface View to a Media Player public class MyActivity extends Activity implements SurfaceHolder.Callback { private MediaPlayer mediaPlayer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mediaPlayer = new MediaPlayer(); SurfaceView surface = (SurfaceView)findViewById(R.id.surface); SurfaceHolder holder = surface.getHolder(); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); holder.setFixedSize(400, 300); } public void surfaceCreated(SurfaceHolder holder) { try { mediaPlayer.setDisplay(holder); } catch (IllegalArgumentException e) { Log.d("MEDIA_PLAYER", e.getMessage()); Playing Audio and Video ❘ 369 } catch (IllegalStateException e) { Log.d("MEDIA_PLAYER", e.getMessage()); } catch (IOException e) { Log.d("MEDIA_PLAYER", e.getMessage()); } } public void surfaceDestroyed(SurfaceHolder holder) { mediaPlayer.release(); } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } } Initializing Video Content for Playback Once you have created and assigned the Surface Holder to your Media Player, use the setDataSource method to specify the path, URL, or Content Provider URI of the video resource to play. As with audio playback, if you’re passing a URL to an online media file, the file must be capable of progressive download using the RTSP or HTTP protocols. Once you’ve selected your media source, call prepare to initialize the Media Player in preparation for playback as shown in Listing 11-6. LISTING 11-6: Initializing video for playback using the Media Player public void surfaceCreated(SurfaceHolder holder) { try { mediaPlayer.setDisplay(holder); mediaPlayer.setDataSource("/sdcard/test2.3gp"); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IllegalArgumentException e) { Log.d("MEDIA_PLAYER", e.getMessage()); } catch (IllegalStateException e) { Log.d("MEDIA_PLAYER", e.getMessage()); } catch (IOException e) { Log.d("MEDIA_PLAYER", e.getMessage()); } } Unlike audio resources, Android doesn’t yet support the playback of video resources included in the application package. Similarly, you cannot use the create static methods as shortcuts to creating your Media Player objects, nor can you use a URI to point to a local file using the file:// schema. 370 ❘ CHAPTER 11 AUDIO, VIDEO, AND USING THE CAMERA Controlling Playback Once a Media Player is prepared, call start to begin playback of the associated media: mediaPlayer.start(); Use the stop and pause methods to stop or pause playback. The Media Player also provides the getDuration method to find the length of the media being played, and getCurrentPosition to find the playback position. Use seekTo to jump to a specific position in the media as shown in Listing 11-7. LISTING 11-7: Controlling playback mediaPlayer.start(); int pos = mediaPlayer.getCurrentPosition(); int duration = mediaPlayer.getDuration(); mediaPlayer.seekTo(pos + (duration-pos)/10); [ wait for a duration ] mediaPlayer.stop(); Managing Media Playback Output The Media Player provides methods to control the volume of the output, manage the screen lock during playback, and set the looping status. It is not currently possible to play audio into a phone conversation; the Media Player always plays audio using the standard output device — the speaker or connected Bluetooth headset. Use the isLooping and setLooping methods to specify if the media being played should loop when it completes. if (!mediaPlayer.isLooping()) mediaPlayer.setLooping(true); To enable a Wake Lock that will keep the screen on during video playback use the setScreenOnWhile Playing method. This is preferred to setting manual Wake Lock as it doesn’t require an additional permission. Wake Locks are described in more detail in Chapter 15. mediaPlayer.setScreenOnWhilePlaying(true); You can control the volume for each channel during playback using the setVolume method. It takes a scalar float value between 0 and 1 for both the left and right channels (where 0 is silent and 1 is maximum volume). mediaPlayer.setVolume(1f, 0.5f); Recording Audio and Video ❘ 371 When playing video resources, you can use getFrame to take a Bitmap screen grab of video media at the specified frame. RECORDING AUDIO AND VIDEO Android offers two alternatives for recording audio and video within your application. The simplest technique is to use Intents to launch the video camera app. This option lets you specify the output location and video recording quality, while letting the native video recording application handle the user experience and error handling. In cases where you want to replace the native app, or simply need more fine-grained control over the video capture UI or recording settings, you can use the Media Recorder class. Using Intents to Record Video The easiest way to initiate video recording is using the ACTION_VIDEO_CAPTURE Media Store static con- stant in an Intent passed to startActivityForResult . startActivityForResult(new Intent(MediaStore.ACTION_VIDEO_CAPTURE), RECORD_VIDEO); This will launch the native video camera Activity, allowing users to start, stop, review, and retake their video, and preventing you from having to rewrite the entire video camera application. The video capture action supports two optional extras, available as static constants from the MediaStore class: ➤ EXTRA_OUTPUT By default, the video recorded by the video capture action will be stored in the default Media Store. If you want to record it elsewhere, you can specify an alternative URI using this extra. ➤ EXTRA_VIDEO_QUALITY The video record action allows you to specify an image quality using an integer value. There are currently two possible values: 0 for low (MMS) quality videos or 1 for high (full resolution) videos. By default, the high resolution mode will be used. Listing 11-8 shows how to use the video capture action to record a new video in high quality to either a specified URI or the default media store. LISTING 11-8: Recording video using an Intent private static int RECORD_VIDEO = 1; private static int HIGH_VIDEO_QUALITY = 1; private static int MMS_VIDEO_QUALITY = 0; continues 372 ❘ CHAPTER 11 AUDIO, VIDEO, AND USING THE CAMERA LISTING 11-8 (continued) private void recordVideo(Uri outputpath) { Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); if (outputpath != null) intent.putExtra(MediaStore.EXTRA_OUTPUT, output); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, HIGH_VIDEO_QUALITY); startActivityForResult(intent, RECORD_VIDEO); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == RECORD_VIDEO) { Uri recordedVideo = data.getData(); // TODO Do something with the recorded video } } Using the Media Recorder Multimedia recording is handled by the aptly named MediaRecorder class. You can use it to record audio and/or video files that can be used in your own applications, or added to the Media Store. To record audio or video, create a new Media Recorder object. MediaRecorder mediaRecorder = new MediaRecorder(); Before you can record any media in Android, your application needs the RECORD_AUDIO and / or RECORD_VIDEO permissions. Add uses-permission tags for each of them, as required, in your application manifest. <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.RECORD_VIDEO"/> The Media Recorder lets you specify the audio and video source, the output file format, and the audio and video encoders to use when recording your file. Much like the Media Player, the Media Recorder manages recording as a state machine. That means that the order in which you configure and manage the Media Recorder is important. In the simplest terms, the transitions through the state machine can be described as follows: ➤ Create a new Media Recorder. ➤ Assign it the input sources to record from. ➤ Define the output format. ➤ Specify the audio and video encoder, frame rate, and output size. ➤ Select an output file. ➤ Prepare for recording. Recording Audio and Video ❘ 373 ➤ Record. ➤ End recording. A more detailed and thorough description of the Media Recorder state machine is provided at the Android developer site at http://developer.android.com/reference/android/media/MediaRecorder .html Once you’ve finished recording your media, call release on your Media Recorder object to free the associated resources. mediaRecorder.release(); Configuring and Controlling Video Recording As described in the state model above, before recording you must specify the input sources, output format, audio and video encoder, and an output file — in that order. The setAudioSource and setVideoSource methods let you specify a MediaRecorder.AudioSource or MediaRecorder.VideoSource static constant that define the audio and video source, respectively. Once you’ve selected your input sources, select the output format using the setOutputFormat method to specify a MediaRecorder.OutputFormat constant. Use the set[audio/video]Encoder methods to specify an audio or video encoder constant from the MediaRecorder.[Audio/Video]Encoder class. Take this opportunity to set the frame rate or video output size if desired. Finally, assign a file to store the recorded media using the setOutputFile method before calling prepare . Listing 11-9 shows how to configure a Media Recorder to record audio and video from the microphone and camera using the default format and encoder to a file on the SD card. LISTING 11-9: Configuring the Media Recorder MediaRecorder mediaRecorder = new MediaRecorder(); // Configure the input sources mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); // Set the output format mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // Specify the audio and video encoding mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); // Specify the output file mediaRecorder.setOutputFile("/sdcard/myoutputfile.mp4"); // Prepare to record mediaRecorder.prepare(); 374 ❘ CHAPTER 11 AUDIO, VIDEO, AND USING THE CAMERA To begin recording, call the start method, as shown in this extension to Listing 11-9. mediaRecorder.start(); The setOutputFile method must be called before prepare and after setOutputFormat or it will throw an Illegal State Exception. When you’re finished, call stop to end the playback, followed by release to free the Media Recorder resources. mediaRecorder.stop(); mediaRecorder.release(); Previewing Video Recording When recording video, it’s generally consideredgood practice to display a preview of the incoming video feed in real time. Using the setPreviewDisplay method, you can assign a Surface to display the video stream in real-time. This works in much the same way as described earlier in this chapter when playing video using the Media Player. Start by creating a new Activity that includes a SurfaceView control as part of the UI, and which implements the SurfaceHolder.Callback interface. Once the Surface Holder has been created, assign it to the Media Recorder using the setPreviewDisplay method as shown in Listing 11-10. The live video preview stream will begin displaying as soon as you make a call to prepare . LISTING 11-10: Previewing video recording public class MyActivity extends Activity implements SurfaceHolder.Callback { private MediaRecorder mediaRecorder; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SurfaceView surface = (SurfaceView)findViewById(R.id.surface); SurfaceHolder holder = surface.getHolder(); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); holder.setFixedSize(400, 300); } Using the Camera and Taking Pictures ❘ 375 public void surfaceCreated(SurfaceHolder holder) { if (mediaRecorder == null) { try { mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); mediaRecorder.setOutputFile("/sdcard/myoutputfile.mp4"); mediaRecorder.setPreviewDisplay(holder.getSurface()); mediaRecorder.prepare(); } catch (IllegalArgumentException e) { Log.d("MEDIA_PLAYER", e.getMessage()); } catch (IllegalStateException e) { Log.d("MEDIA_PLAYER", e.getMessage()); } catch (IOException e) { Log.d("MEDIA_PLAYER", e.getMessage()); } } } public void surfaceDestroyed(SurfaceHolder holder) { mediaRecorder.release(); } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } } USING THE CAMERA AND TAKING PICTURES The popularity of digital cameras (particularly within phone handsets) has caused their prices to drop just as their size has shrunk dramatically. It’s now becoming difficult to even find a mobile phone without a camera, and Android devices are certainly no exception. The G1 was released in 2008 with a 3.2-megapixel camera. Today several devices feature 5-megapixel cameras, with one model sporting an 8.1-megapixel sensor. The following sections will demonstrate the mechanisms you can use to control the camera and take photos within your applications. Using Intents to Take Pictures Theeasiestwaytotakeapictureusingthedevicecameraisusingthe ACTION_IMAGE_CAPTURE Media Store static constant in an Intent passed to startActivityForResult . startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), TAKE_PICTURE); 376 ❘ CHAPTER 11 AUDIO, VIDEO, AND USING THE CAMERA This will launch the camera Activity, allowing users to modify the image settings manually, and pre- venting you from having to rewrite the entire camera application. The image capture action supports two modes, thumbnail and full image. ➤ Thumbnail By default, the picture taken by the image capture action will return a thumb- nail Bitmap in the data extra within the Intent parameter returned in onActivityResult . As shown in Listing 11-11, call getParcelableExtra specifying the extra name data on the Intent parameter to return the thumbnail as a Bitmap. ➤ Full image If you specify an output URI using a MediaStore.EXTRA_OUTPUT extra in the launch Intent, the full-size image taken by the camera will be saved to the specified location. In this case no thumbnail will be returned in the Activity result callback and the result Intent data will be null. Listing 11-11 shows how to use the image capture action to capture either a thumbnail or full image using an Intent. LISTING 11-11: Taking a picture using an Intent private static int TAKE_PICTURE = 1; private Uri outputFileUri; private void getThumbailPicture() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, TAKE_PICTURE); } private void saveFullImage() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File file = new File(Environment.getExternalStorageDirectory(), "test.jpg"); outputFileUri = Uri.fromFile(file); intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(intent, TAKE_PICTURE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == TAKE_PICTURE) { Uri imageUri = null; // Check if the result includes a thumbnail Bitmap if (data != null) { if (data.hasExtra("data")) { Bitmap thumbnail = data.getParcelableExtra("data"); // TODO Do something with the thumbnail } } else { [...]... actions The frequency, audio encoding, and channel configuration values will affect the size and quality of the recorded... Once your application has been started, it is up to you to allow users to enter or modify the number to call and initiate... Data Connections The Android telephony API lets you monitor phone state, retrieve incoming phone numbers, and observe changes to data connections, signal strength, and network connectivity In order to monitor and manage phone state, your application must specify the READ_PHONE_STATE uses-permission in its manifest using the following XML code snippet: ... data, and multimedia messages from within Android applications Using SMS and MMS in Your Application Android provides full SMS functionality from within your applications through the SMSManager Using the SMS Manager, you can replace the native SMS application to send text messages, react to incoming texts, or use SMS as a data transport layer At this time, the Android API does not include simple support... after their safety 390 ❘ CHAPTER 12 TELEPHONY AND SMS TELEPHONY The Android telephony APIs let your applications access the underlying telephone hardware stack, making it possible to create your own dialer — or integrate call handling and phone state monitoring into your applications Because of security concerns, the current Android SDK does not allow you to create your own ‘‘in call’’ Activity — the... as shown below: Sending Text Messages To send a text message, use sendTextMessage from the SMS Manager, passing in the address (phone number) of your recipient and the text message you want to send, as shown in Listing 12-15 LISTING 12-15: Sending an SMS message String sendTo = "5551234"; String myMessage = "Android supports programmatic SMS... frequency, channelConfiguration, audioEncoding, audioLength, AudioTrack.MODE_STREAM); audioTrack.play(); audioTrack.write(audio, 0, audioLength); } catch (Throwable t) {} SPEECH RECOGNITION Since Android 1.5 (API level 3), Android has supported voice input and speech recognition using the RecognizerIntent class This API lets you accept voice input into your application using the standard voice input dialog... version, and number Note that, except for the phone type, reading each of these properties requires that the READ_PHONE_STATE uses-permission be included in the application manifest Listing 12-3 shows how to extract each of these details LISTING 12-3: Reading phone details int phoneType = telephonyManager.getPhoneType(); switch (phoneType) . encoding="utf-8"?> <LinearLayout xmlns :android= "http://schemas .android. com/apk/res /android& quot; android: orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent"> <SurfaceView android: id="@+id/surface" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_gravity="center"> </SurfaceView> </LinearLayout> The. xmlns :android= "http://schemas .android. com/apk/res /android& quot; android: orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent"> <SurfaceView android: id="@+id/surface" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_gravity="center"> </SurfaceView> </LinearLayout> The. your application manifest. <uses-permission android: name=" ;android. permission.RECORD_AUDIO"/> <uses-permission android: name=" ;android. permission.RECORD_VIDEO"/> The Media Recorder

Ngày đăng: 05/07/2014, 15:20

TỪ KHÓA LIÊN QUAN