FileDocCategorySizeDatePackage
QSoundABBToneSequencePlayer.javaAPI DocphoneME MR2 API (J2ME)8194Wed May 02 18:00:46 BST 2007com.sun.mmedia

QSoundABBToneSequencePlayer

public class QSoundABBToneSequencePlayer extends com.sun.mmedia.ABBBasicPlayer implements Runnable

Fields Summary
private QSoundABBMIDIPlayControl
qsmc
private Object
playLock
private Thread
playThread
private boolean
stopped
private QSoundABBToneCtrl
tctrl
private final int
bufferSize
Constructors Summary
public QSoundABBToneSequencePlayer()

    
     
    
        qsmc = new QSoundABBMIDIPlayControl(this);
        tctrl = new QSoundABBToneCtrl(this);
    
Methods Summary
protected voiddoClose()
Subclasses need to implement this to close the Player.

        if(state != Player.UNREALIZED)
        {   
            if(!stopped) 
                try{ doStop(); } catch (MediaException me) {}; // Make sure the we were stopped.
            
            qsmc.close();
        
            qsmc = null;
        }  
    
protected voiddoDeallocate()
Subclasses need to implement this to deallocate the Player.

   

    
voiddoFinishLoopIteration()

 
protected ControldoGetControl(java.lang.String controlType)
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 r = null;
        
        if ((getState() != UNREALIZED) && controlType.startsWith(ABBBasicPlayer.pkgName)) {
            
            controlType = controlType.substring(ABBBasicPlayer.pkgName.length());
            
            if (controlType.equals(ABBBasicPlayer.tocName)) {
                r = tctrl;
            } else 
            {
                r = qsmc.getControl(controlType);
            }
        }
        
        return r;                 
    
protected longdoGetDuration()
Subclasses need to implement this to get the duration of the Player.

return
Description of the Return Value

        return qsmc.getDuration();
    
protected longdoGetMediaTime()
Subclasses need to implement this to get the media time of the Player

return
Description of the Return Value

        return qsmc.getMediaTime();
    
voiddoNextLoopIteration()

 
protected voiddoPrefetch()
Subclasses need to implement this to prefetch the Player.

exception
MediaException Description of the Exception

     
    
protected voiddoRealize()
Subclasses need to implement this to realize the Player.

exception
MediaException Description of the Exception

        qsmc.open();
        
        stopped = true;
        
        if(source != null)
        {
            int count;
            byte[] b = new byte[bufferSize];

            // make use of BAOS, since it takes care of growing buffer 
            ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferSize);        

            try {
                while ((count = source.read(b, 0, bufferSize)) > 0)
                    baos.write(b, 0, count);

                boolean r = qsmc.fillBuffer(baos.toByteArray());

                baos.close();

                if(!r) throw new MediaException("Bad Tone Format");
            }
            catch (IOException ioe)
            {
                throw new MediaException("Failure occured with read stream");
            }
            
            baos = null;
        }
    
protected voiddoSetLoopCount(int count)

        qsmc.setLoopCount(count);
    
protected longdoSetMediaTime(long now)
Subclasses need to implement this to set the media time of the Player.

param
now Description of the Parameter
return
Description of the Return Value
exception
MediaException Description of the Exception

        return qsmc.setMediaTime(now);
    
protected booleandoStart()
Subclasses need to implement this start the Player.

return
Description of the Return Value

        /* 
         * TBD: wait until previous thread finishes before starting new one ...
         * Does it make sense ? Or doStop() makes code below redundant ?
        synchronized (playLock) {
            if (playThread != null && playThread.isAlive()) {
                //request thread to stop ex. stopped=true or doStop() ?
                try { playThread.join(); } catch (InterruptedException ie) {};
            }
        }
         */
        
        if(!stopped) 
            try{ doStop(); } catch (MediaException me) {}; // Make sure the we were stopped.
        
        stopped = false;

        synchronized(playLock)
        {
            playThread = new Thread(this);
            playThread.start();
            try { playLock.wait(); } catch (InterruptedException ie) {};
        }
        
        return true;
    
protected voiddoStop()
Subclasses need to implement this to realize the Player.

exception
MediaException Description of the Exception

        qsmc.stop();
        stopped = true;
        synchronized(playLock)
        {
            try { playThread.join(); } catch (InterruptedException ie) {};
        }
    
public java.lang.StringgetContentType()

        chkClosed(true);
        return DefaultConfiguration.MIME_AUDIO_TONE;
    
public voidrun()

        qsmc.start();
 
        synchronized(playLock) { playLock.notify(); }
        
        boolean done = false;
        int numLoopComplete = 0;
        
        while(!stopped)
        {
            try { Thread.sleep(500); } catch (InterruptedException ie) {};
            
            done = qsmc.isDone();
            numLoopComplete = qsmc.numLoopComplete();
            
            if(!done && (numLoopComplete > 0))
            {
                Long medt = new Long(qsmc.getMediaTime());
                while(numLoopComplete-- > 0)
                    sendEvent(PlayerListener.END_OF_MEDIA, medt);

            }
            if (done)
                stopped = true;
        }
            
        if(done) {
            // updateTimeBase(false);
            state = Player.PREFETCHED;
            sendEvent(PlayerListener.END_OF_MEDIA, new Long(qsmc.getMediaTime()));
        }
    
booleansetSequence(byte[] seq)

        
        if(state == UNREALIZED)
            qsmc.open();
        
        return qsmc.fillBuffer(seq);