FileDocCategorySizeDatePackage
DirectTone.javaAPI DocphoneME MR2 API (J2ME)5400Wed May 02 16:47:08 BST 2007com.sun.mmedia

DirectTone

public final class DirectTone extends DirectPlayer implements ToneControl
Java Tone Sequence Player it implements ToneControl

Fields Summary
Constructors Summary
public DirectTone()
It does not need data source

        hasDataSource = false;
    
Methods Summary
protected ControldoGetControl(java.lang.String type)
The worker method to actually obtain the control.

param
type the class name of the Control.
return
Control for the class or interface name.

        Control c = super.doGetControl(type);
        if (c != null) return c;

        if (getState() >= REALIZED) {
            if (type.equals("javax.microedition.media.control.ToneControl")) {
                return this;
            }
        }

        return null;
    
protected voiddoRealize()
the worker method to realize the player

exception
MediaException Description of the Exception


        // Get current isolate ID to support MVM
        int isolateId = MIDletSuiteUtils.getIsolateId();
        
        // Init native library
        if (this.source == null) {
            hNative = nInit(isolateId, pID, Manager.TONE_DEVICE_LOCATOR, Manager.TONE_DEVICE_LOCATOR, -1);
        } else {
            hNative = nInit(isolateId, pID, DefaultConfiguration.MIME_AUDIO_TONE, source.getLocator(), -1);
        }
        
        if (hNative == 0) {
            throw new MediaException("Unable to realize tone player");
        }

        // if no source stream, player is created from TONE_DEVICE_LOCATOR
        // simply return it.
        if (stream == null) {
            return;
        }

        // read the whole sequence from the source stream
        int chunksize = 128;
        byte[] tmpseqs = new byte[chunksize];
        byte[] seqs = null;
        // make use of BAOS, since it takes care of growing buffer
        ByteArrayOutputStream baos = new ByteArrayOutputStream(chunksize);

        try {
            int read;

            while ((read = stream.read(tmpseqs, 0, chunksize)) != -1) {
                baos.write(tmpseqs, 0, read);
            }

            seqs = baos.toByteArray();
            baos.close();
            tmpseqs = null;
            System.gc();

        } catch (IOException ex) {
            throw new MediaException("unable to realize: fail to read from source");
        }
        
        try {
            this.setSequence(seqs);
        } catch (Exception e) {
            throw new MediaException("unable to realize: " + e.getMessage());
        }
    
public java.lang.StringgetContentType()
Override getContentType from BasicPlayer Always return DefaultConfiguration.TONE content type

        chkClosed(true);
        return DefaultConfiguration.MIME_AUDIO_TONE;
    
public voidsetSequence(byte[] sequence)
Sets the tone sequence.

param
sequence The sequence to set.
exception
IllegalArgumentException Thrown if the sequence is null or invalid.
exception
IllegalStateException Thrown if the Player that this control belongs to is in the PREFETCHED or STARTED state.

        if (this.getState() >= Player.PREFETCHED)
            throw new IllegalStateException("cannot set seq after prefetched");
       
        if(sequence == null) throw new IllegalArgumentException("null sequence");
        if (sequence.length == 0) throw new IllegalArgumentException("empty sequence");

        nFlushBuffer(hNative);

        if(-1 == nBuffering(hNative, sequence, sequence.length))
            throw new IllegalArgumentException("invalid sequence");
        
        if(-1 == nBuffering(hNative, sequence, -1))
            throw new IllegalArgumentException("invalid sequence");

        hasToneSequenceSet = true;