FileDocCategorySizeDatePackage
AndroidClip.javaAPI DocAndroid 1.5 API7096Wed May 06 22:41:02 BST 2009com.android.internal.sound.sampled

AndroidClip

public class AndroidClip extends Object implements Clip
Implements an audio Clip for Android. Since Android's MediaPlayer is somewhat limited, we only support sample playback, but not recording or the querying of sample information. Many of the methods hence throw {@link java.lang.UnsupportedOperationException} or return dummy results.

Fields Summary
private android.media.MediaPlayer
player
Holds the Android MediaPlayer we use.
private AndroidAudioInputStream
stream
Holds the AndroidAudioInputStream we want to play.
Constructors Summary
Methods Summary
public voidaddLineListener(javax.sound.sampled.LineListener listener)

        throw new UnsupportedOperationException();
    
public intavailable()

        throw new UnsupportedOperationException();
    
public voidclose()

        if (isOpen()) {
            stop();
            player = null;
        }
    
public voiddrain()

    
public voidflush()

    
public intgetBufferSize()

        throw new UnsupportedOperationException();
    
public javax.sound.sampled.ControlgetControl(javax.sound.sampled.Control.Type control)

        throw new IllegalArgumentException("No controls available");
    
public javax.sound.sampled.Control[]getControls()

        return new Control[0];
    
public javax.sound.sampled.AudioFormatgetFormat()

        throw new UnsupportedOperationException();
    
public intgetFrameLength()

        throw new UnsupportedOperationException();
    
public intgetFramePosition()

        throw new UnsupportedOperationException();
    
public floatgetLevel()

        throw new UnsupportedOperationException();
    
public javax.sound.sampled.Line$InfogetLineInfo()

        return new Line.Info(this.getClass());
    
public longgetLongFramePosition()

        throw new UnsupportedOperationException();
    
public longgetMicrosecondLength()

        throw new UnsupportedOperationException();
    
public longgetMicrosecondPosition()

        if (isOpen()) {
            return player.getCurrentPosition() * 1000;
        } else {
            return 0;
        }
    
public booleanisActive()

        return false;
    
public booleanisControlSupported(javax.sound.sampled.Control.Type control)

        return false;
    
public booleanisOpen()

        return player != null;
    
public booleanisRunning()

        return player != null && player.isPlaying();
    
public voidloop(int count)

        throw new UnsupportedOperationException();
    
public voidopen()

        try {
            player = new MediaPlayer();
        } catch (Exception ex) {
            throw new LineUnavailableException(ex.toString());
        }
    
public voidopen(javax.sound.sampled.AudioFormat format, byte[] data, int offset, int bufferSize)

        InputStream stream = new ByteArrayInputStream(data, offset, bufferSize);

        open();
        
        try {
            this.stream = (AndroidAudioInputStream)(new AndroidAudioFileReader().getAudioInputStream(stream));
        } catch (Exception ex) {
            throw new LineUnavailableException(ex.toString());
        }
    
public voidopen(javax.sound.sampled.AudioInputStream stream)

        open();
        
        if (!(stream instanceof AndroidAudioInputStream)) {
            try {
                stream = new AndroidAudioFileReader().getAudioInputStream(stream);
            } catch (Exception ex) {
                throw new LineUnavailableException(ex.toString());
            }
        }
        
        this.stream = (AndroidAudioInputStream)stream;
    
public voidremoveLineListener(javax.sound.sampled.LineListener listener)

        throw new UnsupportedOperationException();
    
public voidsetFramePosition(int frames)

        throw new UnsupportedOperationException();
    
public voidsetLoopPoints(int start, int end)

        throw new UnsupportedOperationException();
    
public voidsetMicrosecondPosition(long microseconds)

        if (!isOpen()) {
            throw new IllegalStateException("Clip must be open");
        }
        
        player.seekTo((int)(microseconds / 1000));
    
public voidstart()

        if (!isOpen()) {
            throw new IllegalStateException("Clip must be open");
        }
        
        if (stream == null) {
            throw new IllegalStateException("Need an AudioInputStream to play");
        }

        if (!isRunning()) {
            /*
             * This is ugly, but there is no way around it: The javax.sound API
             * doesn't expect to throw an exception at this point for illegal
             * MIDI sequences. Since we don't really construct the MIDI sequence
             * from the original binary data, but only refer to its URL, the
             * MediaPlayer can actually bail out at this point. We wrap the
             * exception into a RuntimeException, to at least keep the API
             * contract.
             */
            try {
                String s = this.stream.getURL().toExternalForm();
                
                /*
                 * TODO Workaround for 1107794: MediaPlayer doesn't handle
                 * "file:" URLs. Get rid of this.
                 */
                if (s.startsWith("file:")) {
                    s = s.substring(5);
                }
                
                player.setDataSource(s);
                player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                player.prepare();
            } catch (IOException ex) {
                throw new RuntimeException(ex.toString());
            }
            
            player.start();
        }
    
public voidstop()

        if (!isOpen()) {
            throw new IllegalStateException("Clip must be open");
        }
        
        if (isRunning()) {
            player.stop();
        }