Recap Android Components Application Set of Android Components

  • Slides: 36
Download presentation
Recap: Android Components Application= Set of Android Components Activity • UI Component typically corresponding

Recap: Android Components Application= Set of Android Components Activity • UI Component typically corresponding to one screen. • Responds to notifications or status changes. Can wake up your process. Service • Faceless task that runs in the background. Content. Provider Intent Broadcast. Receiver • Enable applications to share data.

Android Programming Lecture 10 Multimedia

Android Programming Lecture 10 Multimedia

3

3

Audio • • • MP 3 MIDI PCM/WAVE AAC LC etc… Video • •

Audio • • • MP 3 MIDI PCM/WAVE AAC LC etc… Video • • H. 263 H. 264 AVC MPEG-4 etc… The Android multimedia framework includes support for playing variety of common media types, so that you can easily integrate audio, video and images into your applications. 4

Media Playback • Android multimedia framework includes support for playing variety of common media

Media Playback • Android multimedia framework includes support for playing variety of common media types, so that you can easily integrate audio, video and images into your applications. • You can play audio or video from media files stored in your application’s resources (raw resources), from standalone files in the file system, or from a data stream arriving over a network connection, all using Media. Player class

Media Playback Guide • http: //developer. android. com/guide/topics/mediapl ayer. html

Media Playback Guide • http: //developer. android. com/guide/topics/mediapl ayer. html

Media Player • One of the most important components of the media framework is

Media Player • One of the most important components of the media framework is the Media. Player class. An object of this class can fetch, decode, and play both audio and video with minimal setup. It supports several different media sources such as: o Local resources o Internal URIs, such as one you might obtain from a Content Resolver o External URIs (streaming) • Supported Media Formats are: o o o RTSP (RTP, SDP) HTTP/HTTPS progressive streaming HTTP/HTTPS live streaming 3 GPP MPEG-4 MP 3

Step 1: Create the raw folder in the project and put the media file

Step 1: Create the raw folder in the project and put the media file into the folder Step 2: Configure the media player object to start the media playback // Create an instance of Media. Player and load the music Media. Player media. Player = Media. Player. create(this, R. raw. music); // Start the media playback media. Player. start(); • To replay the media, call reset() and prepare() • To pause, call pause() • To stop, call stop() Media. Player class reference: http: //developer. android. com/reference/android/media/Media Player. html 8

Media Player • Whit this example you can play an audio file as a

Media Player • Whit this example you can play an audio file as a local raw resource from res/raw/ directory: Media. Player media. Player = Media. Player. create(context, R. raw. sound_file_1); media. Player. start(); // no need to call prepare(); create() does that for you • You can also load a local content through an URI Object Uri my. Uri =. . ; // initialize Uri here Media. Player media. Player = new Media. Player(); media. Player. set. Audio. Stream. Type(Audio. Manager. STREAM_MUSIC); media. Player. set. Data. Source(get. Application. Context(), my. Uri); media. Player. prepare(); media. Player. start(); • You can also play a content from a remote URL via HTTP streaming. String url = "http: //. . . . "; // your URL here Media. Player media. Player = new Media. Player(); media. Player. set. Audio. Stream. Type(Audio. Manager. STREAM_MUSIC); media. Player. set. Data. Source(url); media. Player. prepare(); // might take long! (for buffering, etc) media. Player. start();

Read Media Information

Read Media Information

Callback Functions set. On. Buffering. Update. Listener This method can be called in any

Callback Functions set. On. Buffering. Update. Listener This method can be called in any state and calling it does not change the object state. set. On. Completion. Listener This method can be called in any state and calling it does not change the object state. set. On. Error. Listener This method can be called in any state and calling it does not change the object state. set. On. Prepared. Listener This method can be called in any state and calling it does not change the object state. set. On. Seek. Complete. Listener This method can be called in any state and calling it does not change the object state.

Task at home Follow this tutorial to make a cool sound equalizer: http: //www.

Task at home Follow this tutorial to make a cool sound equalizer: http: //www. 101 apps. co. za/articles/perfect-sound-usingthe-equalizer-effect-a-tutorial. html 12

Audio Recording 13

Audio Recording 13

Audio Capture • You can record audio using the Media. Recorder APIs if supported

Audio Capture • You can record audio using the Media. Recorder APIs if supported by the device hardware • Emulator does not have the capability to record audio and video Media. Recorder class reference: http: //developer. android. com/reference/android/media/Media. Recorder. html

Media Recorder 1. Create a new instance of android. media. Media. Recorder. 2. Set

Media Recorder 1. Create a new instance of android. media. Media. Recorder. 2. Set the audio source using Media. Recorder. set. Audio. Source(). You will probably want to use microphone with Media. Recorder. Audio. Source. MIC 3. Set output file format using Media. Recorder. set. Output. Format(). 4. Set output file name using Media. Recorder. set. Output. File(). 5. Set the audio encoder using Media. Recorder. set. Audio. Encoder(). 6. Call Media. Recorder. prepare() on the Media. Recorder instance. 7. To start audio capture, call Media. Recorder. start(). 8. To stop audio capture, call Media. Recorder. stop(). 9. When you are done with the Media. Recorder instance, call Media. Recorder. release() on it to release the resource. http: //developer. android. com/guide/topics/media/audio-capture. html

private void start. Recording() { // 1. Create a new instance of android. media.

private void start. Recording() { // 1. Create a new instance of android. media. Media. Recorder m. Recorder = new Media. Recorder(); // 2. Set the audio source using Media. Recorder. set. Audio. Source() // In this example, use microphone to get audio m. Recorder. set. Audio. Source(Media. Recorder. Audio. Source. MIC); // 3. Set output file format using Media. Recorder. set. Output. Format(). m. Recorder. set. Output. Format(Media. Recorder. Output. Format. THREE_GPP); // 4. Set output file name using Media. Recorder. set. Output. File(). m. Recorder. set. Output. File(“/sdcard/myaudio. 3 gp”); // 5. Set the audio encoder using Media. Recorder. set. Audio. Encoder(). m. Recorder. set. Audio. Encoder(Media. Recorder. Audio. Encoder. AMR_NB); // 6. Call Media. Recorder. prepare() on the Media. Recorder instance. try { m. Recorder. prepare(); } catch (IOException e) { Log. e(LOG_TAG, "prepare() failed"); } // 7. To start audio capture, call Media. Recorder. start(). m. Recorder. start(); } private void stop. Recording() { // 8. To stop audio capture, call Media. Recorder. stop(). m. Recorder. stop(); // 9. call Media. Recorder. release() on it to release the resource. m. Recorder. release(); m. Recorder = null; } 16

Things to Remember: Manifest The application needs to have the permission to write to

Things to Remember: Manifest The application needs to have the permission to write to external storage if the output file is written to the external storage, and also the permission to record audio. These permissions must be set in the application's Android. Manifest. xml file, with something like: <manifest. . . >. . . <uses-permission android: name="android. permission. RECORD_AUDIO"/> <uses-permission android: name="android. permission. WRITE_EXTERNAL_STORAGE" />. . . </manifest> 17

Sound. Pool • Multiple sounds can be played from raw or compressed source o

Sound. Pool • Multiple sounds can be played from raw or compressed source o A Sound. Pool is a collection of samples that can be loaded into memory from a resource inside the APK or from a file in the file system o More efficient than media player in terms of CPU load, latency • Adjustable playback frequency • Each sound can be assigned a priority • Support for repeat mode Sound. Pool class reference: http: //developer. android. com/reference/android/media/Sound. Pool. html

Creating Sound Effects Sound. Pool Media. Player • Sound. Pool is designed for short

Creating Sound Effects Sound. Pool Media. Player • Sound. Pool is designed for short files which can be kept in memory decompressed for quick access, this is best suited for sound effects in apps and games • Media Player is designed for longer sound files or streams, this is best suited for music files or larger files. The files will be loaded disk each time created is called, this will save on memory space but introduce a small delay (not really noticeable) Refer to the video for a comparison between the two classes https: //www. youtube. com/watch? v=bc. YX 5 fa_j. Fc

Constructs a Sound. Pool with the maximum number of simultaneous streams and Audio stream

Constructs a Sound. Pool with the maximum number of simultaneous streams and Audio stream type. Sound. Pool sound. Pool = new Sound. Pool(4, Audio. Manager. STREAM_MUSIC, 0); int sound. Id = m. Sound. Pool. load(this, R. raw. b 1, 1); sound. Pool. play(sound. Id, 1 f, 1, 0, 1 f); Load the sound from the specified resource. Play the music clip • sound. ID – Sound ID returned by the load() function • left. Volume – Left volume value (0. 0 ~ 1. 0) • right. Volume – right volume value (0. 0 ~ 1. 0) • priority – stream priority (0 = lowest priority) • loop – loop mode (0 = no loop, -1 = loop forever) • rate – playback rate (0. 5 ~ 2. 0, 1. 0 = normal playback) Sound. Pool class reference: http: //developer. android. com/r eference/android/media/Soun d. Pool. html 20

Task at home In assignment 1 game, ask the player to record the short

Task at home In assignment 1 game, ask the player to record the short sound, and use the sound in the game, like when the ball hits the paddle and bounces 21

 Video 22

Video 22

Video. View • Video. View is a View that has video playback capabilities and

Video. View • Video. View is a View that has video playback capabilities and can be used directly in a layout o It is a View working with media player o The Video. View class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting • We can then add controls (play, pause, forward, back, etc) with the Media. Controller class.

Video View main_layout. xml Main. Activity. java ● Add Video. View component on layout

Video View main_layout. xml Main. Activity. java ● Add Video. View component on layout file ● Set content on Video. View o set. Video. URI(. . . ) o set. Video. Path(. . . ) ● Use start(), stop(), etc. to control video.

Callback Functions ● set. On. Prepared. Listener -> on. Prepared() o Called when Video.

Callback Functions ● set. On. Prepared. Listener -> on. Prepared() o Called when Video. View is prepared for play. o Use when waiting contents on online to be prepared. ● set. On. Completion. Listener -> on. Completion() o Called when Video. View playback is completed

Using Native App • Invoke the video playback by using the common intent Intent

Using Native App • Invoke the video playback by using the common intent Intent intent = new Intent(Intent. ACTION_VIEW, Uri. parse(src. Path)); intent. set. Data. And. Type(Uri. parse(src. Path), "video/mp 4"); start. Activity(intent); • Remember, your app is now in the background.

Camera 27

Camera 27

Camera • The Android framework includes support for various cameras and camera features available

Camera • The Android framework includes support for various cameras and camera features available on devices, allowing you to capture pictures and videos in your applications. • The Android framework supports capturing images and video through the Camera API or camera Intent.

Camera • Camera o This class is the primary API for controlling device cameras.

Camera • Camera o This class is the primary API for controlling device cameras. This class is used to take pictures or videos when you are building a camera application. • Media. Recorder o This class is used to record video from the camera. • Intent o An intent action type of Media. Store. ACTION_IMAGE_CAPTURE or Media. Store. ACTION_VIDEO_CAPTURE can be used to capture images or videos without directly using the Camera object.

Image Capture Intent private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; private Uri file. Uri;

Image Capture Intent private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; private Uri file. Uri; @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); // create Intent to take a picture and return control to the calling application Intent intent = new Intent(Media. Store. ACTION_IMAGE_CAPTURE); file. Uri = get. Output. Media. File. Uri(MEDIA_TYPE_IMAGE); // create a file to save the image intent. put. Extra(Media. Store. EXTRA_OUTPUT, file. Uri); // set the image file name // start the image capture Intent start. Activity. For. Result(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); } • Capturing images using a camera intent is quick way to enable your application to take pictures with minimal coding. An image capture intent can include the following extra information http: //developer. android. com/guide/topics/media/camera. html

Image Capture Intent private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; private Uri file. Uri;

Image Capture Intent private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; private Uri file. Uri; @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); // create Intent to take a picture and return control to the calling application Intent intent = new Intent(Media. Store. ACTION_IMAGE_CAPTURE); file. Uri = get. Output. Media. File. Uri(MEDIA_TYPE_IMAGE); // create a file to save the image intent. put. Extra(Media. Store. EXTRA_OUTPUT, file. Uri); // set the image file name // start the image capture Intent start. Activity. For. Result(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); } • Media. Store. EXTRA_OUTPUT- This setting requires a Uri object specifying a path and file name where you'd like to save the picture. This setting is optional but strongly recommended. If you do not specify this value, the camera application saves the requested picture in the default location with a default name, specified in the returned intent's Intent. get. Data() field.

Retrieve Results @Override protected void on. Activity. Result(int request. Code, int result. Code, Intent

Retrieve Results @Override protected void on. Activity. Result(int request. Code, int result. Code, Intent data) { if (request. Code == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { if (result. Code == RESULT_OK) { // Image captured and saved to file. Uri specified in the Intent Bitmap thumbnail = data. get. Parcelable. Extra("data"); Toast. make. Text(this, "Image saved to: n" + data. get. Data(), Toast. LENGTH_LONG). show(); } else if (result. Code == RESULT_CANCELED) { // User cancelled the image capture } else { // Image capture failed, advise user } Receive the result with the file system location of the new Image } if (request. Code == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) { if (result. Code == RESULT_OK) { // Video captured and saved to file. Uri specified in the Intent Toast. make. Text(this, "Video saved to: n" + data. get. Data(), Toast. LENGTH_LONG). show(); } else if (result. Code == RESULT_CANCELED) { Receive the result // User cancelled the video capture } else { // Video capture failed, advise user } } } with the file system location of the new Video. 32

33

33

Media Router • As users connect their televisions, home theatre systems and music players

Media Router • As users connect their televisions, home theatre systems and music players with wireless technologies, they want to be able to play content from Android apps on these larger, louder devices. Enabling this kind of playback can turn your one-device, one-user app into a shared experience that delights and inspires multiple users.

Media Router https: //www. youtube. com/watch? v=_NGB 10 u. N 6 OI 35

Media Router https: //www. youtube. com/watch? v=_NGB 10 u. N 6 OI 35

Recommended Reading • Android Developer Site: Media and Camera o http: //developer. android. com/guide/topics/media/index.

Recommended Reading • Android Developer Site: Media and Camera o http: //developer. android. com/guide/topics/media/index. html • Vogella Tutorial: Handling Media with Android o http: //www. vogella. com/tutorials/Android. Media/article. html