< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/JavaSoundAudioClip.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -28,10 +28,12 @@
 import java.applet.AudioClip;
 import java.io.BufferedInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
 
 import javax.sound.midi.InvalidMidiDataException;
 import javax.sound.midi.MetaEventListener;
 import javax.sound.midi.MetaMessage;
 import javax.sound.midi.MidiFileFormat;

@@ -74,10 +76,11 @@
     private DataPusher datapusher = null;
 
     private Sequencer sequencer = null;
     private Sequence sequence = null;
     private boolean sequencerloop = false;
+    private volatile boolean success;
 
     /**
      * used for determining how many samples is the
      * threshhold between playing as a Clip and streaming
      * from the file.

@@ -89,16 +92,35 @@
      */
     private static final long CLIP_THRESHOLD = 1048576;
     //private final static long CLIP_THRESHOLD = 1;
     private static final int STREAM_BUFFER_SIZE = 1024;
 
-    public JavaSoundAudioClip(InputStream in) throws IOException {
+    public static JavaSoundAudioClip create(final URLConnection uc) {
+        JavaSoundAudioClip clip = new JavaSoundAudioClip();
+        try {
+            clip.init(uc.getInputStream());
+        } catch (final Exception ignored) {
+            // AudioClip will be no-op if some exception will occurred
+        }
+        return clip;
+    }
+
+    public static JavaSoundAudioClip create(final URL url) {
+        JavaSoundAudioClip clip = new JavaSoundAudioClip();
+        try {
+            clip.init(url.openStream());
+        } catch (final Exception ignored) {
+            // AudioClip will be no-op if some exception will occurred
+        }
+        return clip;
+    }
+
+    private void init(InputStream in) throws IOException {
         if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.<init>");
 
         BufferedInputStream bis = new BufferedInputStream(in, STREAM_BUFFER_SIZE);
         bis.mark(STREAM_BUFFER_SIZE);
-        boolean success = false;
         try {
             AudioInputStream as = AudioSystem.getAudioInputStream(bis);
             // load the stream data into memory
             success = loadAudioData(as);
 

@@ -118,22 +140,25 @@
                 success = createSequencer(bis);
             } catch (InvalidMidiDataException e1) {
                 success = false;
             }
         }
-        if (!success) {
-            throw new IOException("Unable to create AudioClip from input stream");
-        }
     }
 
     @Override
     public synchronized void play() {
+        if (!success) {
+            return;
+        }
         startImpl(false);
     }
 
     @Override
     public synchronized void loop() {
+        if (!success) {
+            return;
+        }
         startImpl(true);
     }
 
     private synchronized void startImpl(boolean loop) {
         // hack for some applets that call the start method very rapidly...

@@ -204,10 +229,13 @@
         }
     }
 
     @Override
     public synchronized void stop() {
+        if (!success) {
+            return;
+        }
 
         if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip->stop()");
         lastPlayCall = 0;
 
         if (clip != null) {
< prev index next >