10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.util.concurrent.TimeUnit; 25 26 import javax.sound.sampled.AudioFormat; 27 import javax.sound.sampled.AudioSystem; 28 import javax.sound.sampled.Clip; 29 import javax.sound.sampled.DataLine; 30 31 /* 32 * @test 33 * @bug 5070081 34 * @summary Tests that javax.sound.sampled.Clip does not loses position through 35 * stop/start 36 * @key headful 37 */ 38 public class bug5070081 { 39 40 static AudioFormat format = new AudioFormat(22050, 8, 1, false, false); 41 // create a 3-second file 42 static byte[] soundData = new byte[(int) (format.getFrameRate() * format.getFrameSize() * 3)]; 43 44 static final int LOOP_COUNT = 5; 45 46 static boolean test() throws Exception { 47 DataLine.Info info = new DataLine.Info(Clip.class, format); 48 Clip clip = (Clip)AudioSystem.getLine(info); 49 clip.open(format, soundData, 0, soundData.length); 50 51 boolean bSuccess = true; 52 53 long nLengthMS = clip.getMicrosecondLength()/1000; 54 55 System.out.println(" Clip length:"); 56 System.out.println(" frames: " + clip.getFrameLength()); 57 System.out.println(" seconds: " + nLengthMS/1000.0); 58 59 clip.start(); // start playing 60 Thread.sleep(1000); // wait a sec 61 long time1 = currentTimeMillis(); 62 long pos1 = clip.getFramePosition(); // store the position 63 clip.stop(); // and then stop 64 long pos2 = clip.getFramePosition(); // 2nd try 65 long time2 = currentTimeMillis(); 66 67 System.out.println(" Position before stop: " + pos1); 68 System.out.println(" Position after stop: " + pos2); 69 70 long timeDiff = Math.abs(time2 - time1); 71 // sample rate is 22050 per second, so 22.05 per ms | 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.util.concurrent.TimeUnit; 25 26 import javax.sound.sampled.AudioFormat; 27 import javax.sound.sampled.AudioSystem; 28 import javax.sound.sampled.Clip; 29 import javax.sound.sampled.DataLine; 30 import javax.sound.sampled.LineUnavailableException; 31 32 /* 33 * @test 34 * @bug 5070081 35 * @summary Tests that javax.sound.sampled.Clip does not loses position through 36 * stop/start 37 */ 38 public class bug5070081 { 39 40 static AudioFormat format = new AudioFormat(22050, 8, 1, false, false); 41 // create a 3-second file 42 static byte[] soundData = new byte[(int) (format.getFrameRate() * format.getFrameSize() * 3)]; 43 44 static final int LOOP_COUNT = 5; 45 46 static boolean test() throws Exception { 47 DataLine.Info info = new DataLine.Info(Clip.class, format); 48 Clip clip = null; 49 boolean bSuccess = true; 50 try { 51 clip = (Clip) AudioSystem.getLine(info); 52 clip.open(format, soundData, 0, soundData.length); 53 } catch (LineUnavailableException | IllegalArgumentException ignored) { 54 // the test is not applicable 55 return bSuccess; 56 } 57 58 long nLengthMS = clip.getMicrosecondLength()/1000; 59 60 System.out.println(" Clip length:"); 61 System.out.println(" frames: " + clip.getFrameLength()); 62 System.out.println(" seconds: " + nLengthMS/1000.0); 63 64 clip.start(); // start playing 65 Thread.sleep(1000); // wait a sec 66 long time1 = currentTimeMillis(); 67 long pos1 = clip.getFramePosition(); // store the position 68 clip.stop(); // and then stop 69 long pos2 = clip.getFramePosition(); // 2nd try 70 long time2 = currentTimeMillis(); 71 72 System.out.println(" Position before stop: " + pos1); 73 System.out.println(" Position after stop: " + pos2); 74 75 long timeDiff = Math.abs(time2 - time1); 76 // sample rate is 22050 per second, so 22.05 per ms |