0 Bewertungen0% fanden dieses Dokument nützlich (0 Abstimmungen)
14 Ansichten8 Seiten
The next class is the MediaPlayer, which controls the playback of audio and video streams. The VideoView class can load video content from different sources. The release method releases the resources used by the current media player.
The next class is the MediaPlayer, which controls the playback of audio and video streams. The VideoView class can load video content from different sources. The release method releases the resources used by the current media player.
The next class is the MediaPlayer, which controls the playback of audio and video streams. The VideoView class can load video content from different sources. The release method releases the resources used by the current media player.
programming mobile applications for Android handheld systems. The next class is the MediaPlayer. MediaPlayer controls the playback of audio and video streams and files. And this allows you to incorporate audio and video into your applications, and to let the applications and users control that playback. This class operates according to a complex state machine, which I won't go over here in this lesson, so please take a look at the following website for more information. Some of the methods that you're likely to use when you use the MediaPlayer include SetDataSource, which tells the media player which streams to play. Prepare, which initializes the media player and loads the necessary streams. The prepare method is synchronous, and you'll normally use it when the media content is stored in a file on the device. And there's also a asynchronous version of this method. Which can be used, for example, when the media is streamed from the internet. There's also a start method, to start or resume playback. A pause method, to stop playing temporarily. A seekTo method, to move to a particular position in the stream. A stop method, to stop playing the media. And the release method, which releases the resources used by the current media player. Another class that can be used to view video content is the VideoView class. And this class is a sub-class of SurfaceView and internally makes use of the media player we just talked about. This class can load video content from different sources, and it includes a number of methods and controls to make it easier to view video content. Our next example application is called AudioVideoVideoPlay, and this application displays a simple view with video playback controls, and allows the user to play a video file. In this case, the film is a clip from the 1902 film, A Trip to the Moon, by Georges Mlis. Let's take a look. So here's my device. And now I'll start up the AudioVideoVideoPlay application. If I now touch the display, you can see that a set of playback controls appear. And now I'll hit the single triangle and the video will begin playing. Here we go. [BLANK_AUDIO] Let's take a look at the source code for this application. [BLANK_AUDIO] Here's the AudioVideoVideoPlay application open in the IDE. And now I'll open the main activity. In OnCreate, the code first gets a reference to a video view that's in this activity's layout. Next, it creates a media controller, which is a view that contains controls for controlling the media player. The code continues by disabling the media controls, and then by attaching this media controller to the video view, with a call to the video view's setMediaController method. Next, the code identifies the media file to play, passing in a URI that points to a file stored in the res/raw directory. After that, the code sets an OnPreparedListener on the video view. This code will be called, once the media is loaded and ready to play. And when that happens, the code will enable the media controller, so the user can start the film playing. And finally, down in the onPause method, the code shuts down the video view. The next class we'll discuss is the MediaRecorder. Now this class can be used to record both audio and video. The class operates in accordance with a state machine, which you can read more about, at this URL. [BLANK_AUDIO] Now some of the media recorder methods that you'll likely use include setAudioSource, and setVideoSource. Which set the source of the input, such as the microphone for audio, or a camera for video. SetOutputFormat, which sets the output format for the recording. For instance, mp4. Prepare, which readies the recorder to begin capturing and encoding data. Start, which starts the actual recording process. Stop, which stops the recording process. And release, which releases the resources held by this MediaRecorder. Our next example application is AudioVideoAudioRecording. Now this application records audio from the user and can play the recorded audio back to the user. Let's use this application to capture my voice. So here's my device. [BLANK_AUDIO] And now I'll start up the AudioVideoAudioRecording application. This application displays two toggle buttons, one labeled Start Recording and one labeled Start Playback. When I press the Start Recording button the application will begin recording. The button's label will change to Stop Recording, and the play back button will be disabled. [BLANK_AUDIO] When I press the start recording button again, the recording will stop. The button's label will change back, and the playback button will be enabled again. Let's try it out. Now I'll press the Start Recording button. [BLANK_AUDIO] Testing, testing, one, two, three, testing. And now that I've pressed the button again, the recording is finished, and saved, and the Start Playback button is now enabled. Let me press that one now. Testing, testing, one, two, three, testing. And now I'll press that button again. And we're back to where we started. Let's look at the source code for this application. Here's the AudioVideoAudioRecording application open in the IDE. Now I'll open the main activity. In onCreate the code first gets references to the two toggle buttons. Next it sets up an onCheckChangeListener, on each of the toggle buttons. This code is called when the check state of a toggle button changes. Let's look at the first toggle button which is the recording button. [BLANK_AUDIO] When this button's checked state changes, say from off to on, this code will first disable the play button, and then will call the onRecordPressed method. The playback button does something similar. It first changes the enabled state of the recording button, disabling it if the user wants to start playback. Or enabling it, if the user wants to stop playback. After that, it then calls the onPlayPressed method. Let's look at the onRecordPressed method first. As you can see, this method takes a Boolean as a parameter called shouldStartRecording. If shouldStartRecording is true, then the code calls the startRecording method. Otherwise, it calls the stopRecording method. The start recording method first creates a new media recorder and then sets its source as the microphone. Then it sets the output format. And then the output file where the recording will be saved. And then it sets the encoder for the audio file. Now continuing on, the code calls prepare to get the recorder ready, and then finally it calls the start method to begin recording. [BLANK_AUDIO] The stop recording method instead, stops the media recorder and then releases its resources. [BLANK_AUDIO] If the user instead had pressed the playback button, then onPlayPressed would have been called. If the button was checked then the parameter shouldStartPlaying would be true. If so, the start playing method is called, otherwise the stop playing method is called. The start playing method starts by creating a media player. And then follows up by setting its data source, then by calling prepare on the media player, and then by calling the start method. The stop playing method will stop the media player, and then release the media player's resources. [BLANK_AUDIO] The last class we'll talk about, in this lesson, is the camera class. This class allows applications to access the camera service. The low level code that manages the actual camera hardware on your device. Now, through this class your application can manage settings for capturing images. Start and stop a preview function, which allows you to use the devices display as a kind of camera view finder. And most importantly, it allows you to take pictures and video. To use the camera features you'll need to set some permissions, and features. You'll need at least the camera permission, and you'll probably want to include a uses-feature tag in your Android manifest .xml file that specifies the need for a camera. And you may want to specify that your application requires other sub-features, such as autofocus or a flash. Although you can easily use the built in camera application to take pictures, you might want to add some features to a traditional camera application. Or, you might want to use the camera for other purposes. In that case, you can follow the following steps. First, you get a camera instance. Next, you can set any camera parameters that you might need. After that, you will want to setup the preview display, so the user can see what the camera sees. Next, you'll start the preview, and you'll keep it running, until the user takes a picture. And once the user takes a picture, your application will receive and process the picture image. And then eventually, your application will release the camera so that other applications can have access to it. The last example application for this lesson is called AudioVideoCamera. This application takes still photos and uses the device's display as the camera's viewfinder. Let's give it a try. So here's my device. And now, I'll start up the AudioVideoCamera application. As you can see, the application displays the image currently visible through the camera's lens. And if you move the camera, the image changes. If the user is satisfied with the image, then he or she can simply touch the screen to take a picture. And when he or she does so, the camera will take the picture, and then freeze the preview window for about two seconds. So the user can see the picture they just snapped. Let me do that. I'll touch the display, to snap the picture, and now the preview freezes for about two seconds. And now the camera is ready to take another photo. Let's look at the source code for this application. Here's the AudioVideoCamera application open in the IDE. Now, I'll open the main activity. And let's scroll down to the onCreate method. [BLANK_AUDIO] And one of the things we see here is the code calls the getCamera method, to get a reference to the camera object. Let's scroll down to that method. [BLANK_AUDIO] This method calls the camera classes open method. Which returns a reference, to the first back facing camera on this device. If your device has several cameras, you can use other versions of the open method to get particular cameras. Now scrolling back up to onCreate, the code now sets up a touch listener on the main view. And when the user touches the screen, this listener's onTouch method will be called. And this method will call the camera's takePicture method to take a picture. Now we'll come back to this method in a few seconds. Next, the code sets up a surface view that is used to display the preview, which shows the user what the camera is currently seeing. And these steps are just what we talked about in our previous lesson on graphics. First, the code gets the surface holder for the surface view, and then it adds a callback object to the surface holder. And that callback object is defined below. Let's scroll down to it. [BLANK_AUDIO] Now as you remember, the SurfaceHolder.Callback interface defines three methods. SurfaceCreated, surfaceChanged, and surfaceDestroyed. The surfaceCreated method starts by setting the surface holder on which the camera will show its preview. And after that, the code starts the camera's preview. When the surface changes its structure or format, the surfaceChanged method is called. And this method disables touches on the layout, and then stops the camera preview. Next, the code changes the camera parameters. And in this case, the code finds an appropriate size for the camera preview. And then sets the preview size, and then passes the updated parameters object back to the camera. Now that the parameters are set, the code restarts the preview, by calling the startPreview method. Then, finally, the code re-enables touches on the layout. So now that we've gone over setting up and managing the preview display, let's go back and look at taking an actual picture. So scrolling back up to the onTouchListener, When the user touches the display, the takePicture method gets called. In that method, the code here passes in two CameraCallback objects. One is the ShutterCallback, and the other is the CameraCallback. The ShutterCallback is called around the time that the user takes the picture, basically to let the user know that the camera is taking a picture. The CameraCallback used here, is called after the picture has been taken and when the compressed image is available. When this happens, the CameraCallback's onPictureTaken method is called. In this example, the code simply sleeps for two seconds, and then restarts the preview. And you might notice that this particular application doesn't actually save the image. But of course, you'd normally would want to do that, and if so, you'd typically do it right here in this method. [BLANK_AUDIO] The last method I'll talk about is onPause. Here the code disables touches on the display, shuts down the preview, and then releases the camera so that other applications can use it. So that's all for our lesson on multimedia. Please join me next time, when we'll talk about sensors. Thanks.