Code example for MediaPlayer: Playing audio and video

Methods: isPlayingpreparesetAudioStreamTypesetDataSourcestart

0
    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 = 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 void stop() { 
        if (!isOpen()) { 
Contextual code suggestions in your IDE  Get Codota for Java