6 - 7 - Multimedia - Part 2 (18-45)

8

Click here to load reader

Transcript of 6 - 7 - Multimedia - Part 2 (18-45)

Page 1: 6 - 7 - Multimedia - Part 2 (18-45)

[BLANK_AUDIO]Hi, I'm Adam Porter, and this isprogramming mobile applications forAndroid handheld systems.The next class is the MediaPlayer.MediaPlayer controls the playback of audioand video streams and files.And this allows you to incorporate audioand video into yourapplications, and to let the applicationsand users control that playback.This class operates according to a complexstate machine,which I won't go over here in this lesson,so please take a look at the followingwebsite for more information.Some of the methods that you're likely touse when you use the MediaPlayer includeSetDataSource, which tells the mediaplayer which streams to play.Prepare, which initializes the mediaplayer and loads the necessary streams.The prepare method is synchronous, andyou'll normally use it whenthe media content is stored in a file onthe device.And there's also a asynchronous version ofthis method.Which can be used, for example, when themedia is streamed from the internet.There's also a start method, to start orresume playback.A pause method, to stop playingtemporarily.A seekTo method, to move to a particularposition in the stream.A stop method, to stop playing the media.And the release method, which releases theresources used by the current mediaplayer.Another class that can be used to viewvideo content is the VideoView class.And this class is a sub-class ofSurfaceView and internallymakes use of the media player we justtalked about.This class can load video content fromdifferent sources, and it includes anumber of methods and controls to make iteasier to view video content.Our next example application is calledAudioVideoVideoPlay, and this applicationdisplays a simpleview with video playback controls, andallows the user to play a video file.In this case, the film is a clip from the1902 film, A Trip to the Moon, by GeorgesMéliès.Let's take a look.So here's my device.

Page 2: 6 - 7 - Multimedia - Part 2 (18-45)

And now I'll start up theAudioVideoVideoPlay application.If I now touch the display, you can seethat a set of playback controls appear.And now I'll hit the single triangle andthe video will begin playing.Here we go.[BLANK_AUDIO]Let's take a look at the source code forthis application.[BLANK_AUDIO]Here's the AudioVideoVideoPlay applicationopen in the IDE.And now I'll open the main activity.In OnCreate, the code first gets areference to a video view that's in thisactivity's layout.Next, it creates a media controller,which is a view that contains controls forcontrolling the media player.The code continues by disabling the mediacontrols, andthen by attaching this media controller tothe video view, witha call to the video view'ssetMediaController method.Next, the code identifies the media fileto play, passing in a URIthat points to a file stored in theres/raw directory.After that, the code sets anOnPreparedListener on the video view.This code will be called, once the mediais loaded and ready to play.And when that happens, the code willenable themedia controller, so the user can startthe film playing.And finally, down in the onPause method,the code shuts down the video view.The next class we'll discuss is theMediaRecorder.Now this class can be used to record bothaudio and video.The class operates in accordance with astate machine,which you can read more about, at thisURL.[BLANK_AUDIO]Now some of the media recorder methodsthat you'll likely use includesetAudioSource, and setVideoSource.Which set the source of the input, such asthe microphone for audio, or a camera forvideo.SetOutputFormat, which sets the outputformat for the recording.For instance, mp4.Prepare,which readies the recorder to begin

Page 3: 6 - 7 - Multimedia - Part 2 (18-45)

capturing and encoding data.Start, which starts the actual recordingprocess.Stop, which stops the recording process.And release, which releases the resourcesheld by this MediaRecorder.Our next example application isAudioVideoAudioRecording.Now this application records audio fromthe user andcan play the recorded audio back to theuser.Let's use this application to capture myvoice.So here's my device.[BLANK_AUDIO]And now I'll start up theAudioVideoAudioRecording application.This application displays two togglebuttons, onelabeled Start Recording and one labeledStart Playback.When I press the Start Recording buttonthe application will begin recording.The button's label will change to StopRecording,and the play back button will be disabled.[BLANK_AUDIO]When I press the start recording buttonagain, the recording will stop.The button's label will change back, andthe 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 buttonagain, the recording is finished, andsaved, and the Start Playback button isnow enabled.Let me press that one now.Testing, testing, one, two, three,testing.Andnow I'll press that button again.And we're back to where we started.Let's look at the source code for thisapplication.Here's the AudioVideoAudioRecordingapplication open in the IDE.Now I'll open the main activity.In onCreate the code first gets referencesto the two toggle buttons.Next it sets up an onCheckChangeListener,on each of thetoggle buttons.This code is called when the check stateof a toggle button changes.Let's look at the first toggle button

Page 4: 6 - 7 - Multimedia - Part 2 (18-45)

which is the recording button.[BLANK_AUDIO]When this button's checked state changes,say from off to on, this code willfirst disable the play button, and thenwill call the onRecordPressed method.The playback button does somethingsimilar.It first changes the enabled state of therecordingbutton, disabling it if the user wants tostart playback.Or enablingit, if the user wants to stop playback.After that, it then calls theonPlayPressed method.Let's look at the onRecordPressed methodfirst.As you can see, this method takesa Boolean as a parameter calledshouldStartRecording.If shouldStartRecording is true, then thecode calls the startRecording method.Otherwise, it calls the stopRecordingmethod.The start recording method first creates anew media recorder and then setsits source as the microphone.Then it sets the output format.And then the output file where therecording will be saved.And then it sets the encoder for the audiofile.Now continuing on, the code calls prepareto get the recorder ready,and then finally it calls the start methodto begin recording.[BLANK_AUDIO]The stop recording method instead, stopsthemedia recorder and then releases itsresources.[BLANK_AUDIO]If the user instead had pressed theplayback button, then onPlayPressed wouldhave been called.If the button was checked thenthe parameter shouldStartPlaying would betrue.If so, the start playing method iscalled, otherwise the stop playing methodis called.The start playing method starts bycreating a media player.And then follows up by setting its datasource, then by callingprepare on the media player, and then bycalling the start method.The stop playing method will stop themedia

Page 5: 6 - 7 - Multimedia - Part 2 (18-45)

player, and then release the mediaplayer's resources.[BLANK_AUDIO]The last class we'll talk about, in thislesson, is the camera class.This class allows applications to accessthe camera service.The low level code that manages the actualcamera hardware on your device.Now, through this class your applicationcan manage settings for capturing images.Start and stop a preview function, whichallowsyou to use the devices display as a kindof camera view finder.And most importantly, it allows you totake pictures and video.To use the camera features you'll need toset some permissions, and features.You'll need at least the camerapermission, and you'll probably want toinclude a uses-feature tag in your Androidmanifest .xml file that specifiesthe need for a camera.And you may want to specify that yourapplication requires other sub-features,such as autofocus or a flash.Although you can easily use the built incamera application to takepictures, you might want to addsome features to a traditional cameraapplication.Or, you might want to use the camera forother purposes.In that case, you can follow the followingsteps.First, you get a camera instance.Next, you can set any camera parametersthat you might need.After that, you will want to setupthe preview display, so the user can seewhat the camera sees.Next, you'll start the preview, and you'llkeepit running, until the user takes apicture.And once the user takes a picture, yourapplication will receive and process thepicture image.And then eventually, your application willrelease the cameraso that other applications can have accessto it.The last example application for thislesson is called AudioVideoCamera.This application takes still photos andusesthe device's display as the camera'sviewfinder.Let's give it a try.

Page 6: 6 - 7 - Multimedia - Part 2 (18-45)

Sohere's my device.And now, I'll start up theAudioVideoCamera application.As you can see, the application displaysthe image currently visible through thecamera's lens.And if you move the camera, the imagechanges.If the user is satisfied with the image,then heor she can simply touch the screen to takea picture.And when he or she does so, the camerawill takethe picture, and then freeze the previewwindow for about two seconds.So the user can see the picture they justsnapped.Let me do that.I'll touch the display, to snap thepicture,and now the preview freezes for about twoseconds.And now the camera is ready to takeanother photo.Let's look at thesource code for this application.Here's the AudioVideoCamera applicationopen in the IDE.Now, I'll open the main activity.And let's scroll down to the onCreatemethod.[BLANK_AUDIO]And one of the things we see here is thecodecalls the getCamera method, to get areference to the camera object.Let's scroll down to that method.[BLANK_AUDIO]This method calls the camera classes openmethod.Which returns a reference, to the firstback facing camera on this device.If your device has several cameras, youcan useother versions of the open method to getparticular cameras.Now scrolling back up to onCreate, thecode now sets up a touch listener on themain view.And when the user touches the screen, thislistener's onTouch method will be called.And this method will call the camera'stakePicture method to take a picture.Now we'll come back to this method in afew seconds.Next, the code sets up a surface view thatis used to display the preview,which shows the user what the camera is

Page 7: 6 - 7 - Multimedia - Part 2 (18-45)

currently seeing.And these steps are just what we talkedabout in our previous lesson on graphics.First, the code gets the surface holderfor the surface view,and then it adds a callback object to thesurface holder.And that callback object is defined below.Let's scroll down to it.[BLANK_AUDIO]Now as you remember, theSurfaceHolder.Callback interface definesthree methods.SurfaceCreated, surfaceChanged, andsurfaceDestroyed.The surfaceCreated method starts bysetting the surfaceholder on which the camera will show itspreview.And after that, the code starts thecamera's preview.When the surface changes its structureor format, the surfaceChanged method iscalled.And this method disables touches on thelayout, and then stops the camera preview.Next, the code changes the cameraparameters.And in this case, the code finds anappropriate size for the camera preview.And then sets the preview size,and then passes the updated parametersobject back to the camera.Now that the parameters are set, the coderestarts the preview, by calling thestartPreview method.Then, finally, the code re-enables toucheson the layout.So now that we've gone over setting up andmanaging thepreview display, let's go back and look attaking an actual picture.So scrolling back up to theonTouchListener,When the user touches the display, thetakePicture method gets called.In that method, the code here passes intwo CameraCallback objects.One is the ShutterCallback, and the otheris the CameraCallback.The ShutterCallback is called around thetime that the user takes the picture,basically to let the user know that thecamera is taking a picture.The CameraCallback used here, is calledafter the picturehas been taken and when the compressedimage is available.When this happens, the CameraCallback'sonPictureTaken method is called.

Page 8: 6 - 7 - Multimedia - Part 2 (18-45)

In this example, the code simply sleepsfor two seconds, and then restarts thepreview.And you might notice that this particularapplication doesn't actually save theimage.But of course, you'd normally would wantto do that, andif so, you'd typically do it right here inthis method.[BLANK_AUDIO]The last method I'll talk about isonPause.Here the code disables touches on thedisplay, shuts down the preview,and then releases the camera so that otherapplications can use it.So that's all for our lesson onmultimedia.Please join me next time, when we'll talkabout sensors.Thanks.