Programming Java 2 Micro Edition for Symbian OS 2004 (779882), страница 36
Текст из файла (страница 36)
The names are then used toinitialize the InitialView. The VideoCanvas and VideoPlayerinstances are then created.178MIDP 2.0 AND THE JTWIAll the other methods in MIDletController are essentially thesame as their Audio Player namesakes.The VideoCanvas ClassWe will briefly take a look at the (very simple) VideoCanvas class:import javax.microedition.lcdui.*;public class VideoCanvas extends Canvas{public VideoCanvas(MIDletController controller){setCommandListener(controller);}// Paints background colorpublic void paint(Graphics g){g.setColor(128, 128, 128);g.fillRect(0, 0, getWidth(), getHeight());}}The important point to note is that the paint() method plays no partin rendering the video. This is performed directly by the VideoControl.The full source code and JAR and JAD files for the Video PlayerMIDlet can be downloaded from the Symbian website at www.symbian.com/books.3.4.1.6 Capturing ImagesAnother use of VideoControl is to capture images from a camera.
In thiscase, rather than specifying a file (and MIME type) as the data source,we specify capture://video. Other than that, the setting up of thevideo player and control proceeds pretty much as in the Video PlayerMIDlet above.The Picture Puzzle MIDlet, included as a case study in Chapter 5,illustrates image capture.
The following code which performs the necessary initialization of a video player and a control is reproduced from theCapturer class in that example.// Creates a VideoPlayer and gets an associated VideoControlpublic void createPlayer() throws ApplicationException {try {player = Manager.createPlayer("capture://video");player.realize();// Sets VideoControl to the current display.videoControl =(VideoControl)(player.getControl("VideoControl"));if (videoControl == null) {discardPlayer();} else {OPTIONAL J2ME APIS IN THE JTWI179videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO,canvas);int cWidth = canvas.getWidth();int cHeight = canvas.getHeight();int dWidth = 160;int dHeight = 120;videoControl.setDisplaySize(dWidth, dHeight);videoControl.setDisplayLocation((cWidth - dWidth)/2,(cHeight - dHeight)/2);}By setting the Canvas to be the current one in the Display, we canuse it as a ‘‘viewfinder’’ for the camera.
When we are ready to take apicture, we simply call getSnapshot(null) on the VideoControl,as shown in the following code from the Picture Puzzle MIDlet:public byte[] takeSnapshot() throws ApplicationException {byte[] pngImage = null;if (videoControl == null) {throw new ApplicationException("Unable to capture photo:VideoControl null");}try {pngImage = videoControl.getSnapshot(null);}catch(MediaException me) {throw new ApplicationException("Unable to capture photo",me);}return pngImage;}It should be noted that, if a security policy is in operation, userpermission may be requested through an intermediate dialog, which mayinterfere with the photography!3.4.1.7 Generating TonesMMAPI also supports tone generation. Generating a single tone is simplyachieved using the following method of the Manager class:public static void playTone(int note, int duration, int volume)throws MediaExceptionThe note is passed as an integer value in the range 0–127.
ToneControl.C4 = 60 represents middle C. Adding or subtracting 1 increasesor lowers the pitch by a semitone. The duration is specified in millisecondsand the volume is an integer value on the scale 0–100.To play a sequence of tones it is more appropriate to create a Playerand use it to obtain a ToneControl.180MIDP 2.0 AND THE JTWIbyte[] toneSequence = { ToneControl.C4, ToneControl.C4 + 2,ToneControl.c4 +4, ...};try{Player player = Manager.createPlayer(Manager.TONE_DEVICE_LOCATOR);player.realize();ToneControl control = (ToneControl)player.getControl("ToneControl");control.setSequence(toneSequence);player.start();} catch (IOException ioe) {} catch (MediaException me) { //handle }A tone sequence is specified as a list of tone–duration pairs and userdefined sequence blocks, using Augmented Backus–Naur form (ABNF)syntax (refer to the MMAPI specification for more detail).
The list ispackaged as a byte array and passed to the ToneControl using thesetSequence() method. To play the sequence we simply invoke thestart() method of the Player.A more sophisticated example can be found in the documentation ofToneControl in the MMAPI specification.3.4.2 MMAPI on Symbian OS PhonesWe next look at the important question of which media capabilities aresupported in practice on the various Symbian OS phones on the market.It is important to understand that when we talk about MMAPI on SymbianOS we are not talking about a single version but three, based on twodistinct implementations. These are:• Symbian MIDP 2.0 Audio subset (on Symbian OS Version 7.0)• Series 60 Developer Platform 1.0 (on Symbian OS Version 6.1)• Series 60 Developer Platform 2.0 (on Symbian OS Version 7.0s).These MMAPI implementations will be discussed in turn.MMAPI was first implemented on Symbian OS not by Symbian butby Nokia, for their Series 60 Developer Platform 1.0 (as embodied inthe Series 60 MIDP SDK 1.2.1 for Symbian OS, Nokia edition, based onSymbian OS Version 6.1).
This is available on all phones based on thisplatform, with the exception of the Nokia 7650 which was technicallybased on a precursor to the Series 60 Developer Platform 1.0, andprovided multimedia capabilities only through custom Nokia APIs.This implementation was extended by Nokia for the Series 60 Developer Platform 2.0 and Series 90 Developer Platform 1.0, both based onSymbian OS Version 7.0s. At the time of writing, the only announcedphones based on these platforms are the Nokia 6600 and the Nokia 7700,based on Series 60 and Series 90 respectively.OPTIONAL J2ME APIS IN THE JTWI181As the number of phones based on these platforms continues to grow,the reader is referred to www.symbian.com/phones to ascertain the current list.
Note that Nokia licenses its platforms to other mobile phonemanufacturers, so the list is not restricted to Nokia phones.At the same time, Symbian has separately implemented the audiosubset (‘‘building block’’) of MMAPI defined by MIDP 2.0, which becameavailable with the release of Symbian OS Version 7.0s. Consequently, itis not available as standard on phones based on Symbian OS Version 7.0.However, the whole of MIDP 2.0 has been ‘‘backported’’ from SymbianOS Version 7.0s to Symbian OS Version 7.0 as part of the upgrade of theUIQ platform from UIQ 2.0 to UIQ 2.1.
As a result, phones based on UIQ2.1 (the first of which to be announced are the Sony Ericsson P900/P908and the BenQ P30) support the audio subset.Symbian is releasing a fully featured MMAPI implementation in theforthcoming Symbian OS Version 8.0, which will be available forall Symbian OS phones (see www.symbian.com/technology/standardjava.html). This will certainly mean a closer match of the MMAPIcapabilities of Symbian OS phones based on different UIs than atpresent.3.4.2.1 Symbian MIDP 2.0 Audio SubsetThe audio subset of MIDP 2.0 is described in the MIDP 2.0 specificationdocument under javax.microedition.media and javax.microedition.media.control.
Notably there is no javax.microedition.media.protocol package, since custom DataSourcesare not supported. The associated overridden version of Manager.createPlayer() is not, as a result, supported either. Only two controls are available, VolumeControl and ToneControl, both of whichare fully supported by Symbian OS. There is no support for mediarecording or capture.The following are the audio formats supported:FormatFile extensionMIME typesAU audioWave audioMP3Tone sequence.au.wav.mp3n/aaudio/basicaudio/wav, audio/x-wavaudio/mp3audio/x-tone-seqThese can all, with the exception of tone sequences, be played via thevarious mechanisms described in Section 3.4.1.2. Tone sequences differin that there is no file extension associated with them; they can only becreated in a programmatic manner, in the context of a ToneControl.182MIDP 2.0 AND THE JTWIThe P900 adds additional support for the following audio types:FormatFile extensionMIME typesRMF (Beatnik)iMelodyMIDI.rmf.imy.midaudio/rmftext/x-imelody, audio/x-imelodyaudio/midi, audio/x-midi3.4.2.2 Series 60 Developer Platform 1.0Nokia’s implementation of MMAPI in Series 60 Developer Platform 1.0supports playing of the following types of media:FormatFile extension MIME typesWave audioAMR audioNokia ring toneTone sequenceMIDIScalable Polyphonic MIDI3GPP videoNIM video.wav.amr.rngn/a.mid.mid.3gp.nimaudio/x-wavaudio/amraudio/x-nokia-rngaudio/x-tone-seqaudio/midiaudio/sp-midivideo/3gppvideo/vnd.nokia.interleavedmultimediaThese all support VolumeControl and StopTimeControl (whichallows you to specify in advance a stop time, rather than letting themedia play to its end).
In addition, tone sequences necessarily supportToneControl and videos support VideoControl. No other controlsare supported.It is worth noting that Nokia’s MIDP implementation supports fullscreen display of a Canvas, through a custom FullCanvas class. It ispossible to play video full-screen using such a class as follows:FullCanvas canvasVideoControl videoControl;Player player;...videoControl = (VideoControl)player.getControl(“VideoControl”);videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);There are no recording capabilities. Nor is there support for audiocapture.
However, still images can be captured from the camera, usingOPTIONAL J2ME APIS IN THE JTWI183the protocol described in Section 3.4.1.2. The default encoding for thecaptured image on all Nokia phones is PNG. Alternatively you can specifyone of the three supported formats:• Portable Network Graphics (PNG)• Bitmap (BMP)• JPEG (JPG).You do this by passing one of the strings encoding=png, encoding=bmp or encoding=jpeg as an argument to the getSnapshot()method of VideoControl. You can set the width and height in thesame way.
The default is 160 × 120 pixels. Be aware that if you changethe aspect ratio in your specification, the image will be stretched ratherthan clipped. VideoControl is the only control which can be used in thecontext of video capture. Further details about the use of VideoControlcan be found in Camera MIDlet: A Mobile Media API Example on ForumNokia (http://ncsp.forum.nokia.com/csp).Other points worth noting about Nokia’s implementation are that:• ‘‘mixing’’, in the sense of simultaneous playback by multiple players,is not supported; although the TimeBase concept is supported, itwould not appear to be usable for its intended purpose of playbacksynchronization• RTP streaming is not supported: the protocol itself is unsupported• HTTP streaming is not supported; media data will be downloadedcompletely (during the ‘‘realization’’ phase) before ‘‘prefetching’’ andplaying can begin (see Figure 3.23).3.4.2.3 Series 60 Developer Platform 2.0A number of modifications have been made to Nokia’s MMAPI implementation for the Series 60 Developer Platform 2.0.
The comments belowcan be expected to apply equally to the Series 90 Developer Platform 1.0which is closely related.The main differences are that support has been added for audiocapture and recording, and there are changes to the set of supportedcontent types. In particular, support for the proprietary Nokia audio andvideo file formats has been removed and support has been added forAU, Raw and AMR wideband audio formats and MP4 and Real Mediavideo formats.184MIDP 2.0 AND THE JTWIThe following is the list of supported content types:FormatFile extension MIME typesWave audioAMR audioAMR wideband audioRaw audioAU audioTone sequenceMIDIScalable Polyphonic MIDI3GPP videoMP4 videoReal Media video.wav.amr.awb.raw.aun/a.mid.mid.3gp.mp4.rmaudio/wav, audio/x-wavaudio/amraudio/amr-wbaudio/basicaudio/au, audio/x-auaudio/x-tone-seqaudio/midiaudio/sp-midivideo/3gppvideo/mp4application/vnd.rn-realmediaIt might be observed here that a number of the file extensions haveassociated with them more than one MIME type.