Code example for MediaPlayer: Playing audio and video

Methods: isPlayingpreparesetAudioStreamTypesetDataSourcestart

    public boolean isActive() { 
        return false; 
    public boolean isRunning() { 
        return player != null && player.isPlaying();
    public void start() { 
        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 =;
                 * TODO Workaround for 1107794: MediaPlayer doesn't handle 
                 * "file:" URLs. Get rid of this. 
                if (s.startsWith("file:")) {
                    s = s.substring(5);
            } catch (IOException ex) {
                throw new RuntimeException(ex.toString());
    public void stop() { 
        if (!isOpen()) { 
Connect your IDE to all the code out there  Get Codota for Java