Java by API/java.applet/AudioClip — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:22, 31 мая 2010

AudioClip: loop()

// This example is from the book _Java AWT Reference_ by John Zukowski.
// Written by John Zukowski.  Copyright (c) 1997 O"Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Event;
import java.awt.Graphics;
import java.net.MalformedURLException;
import java.net.URL;
public class MainClass extends Applet {
  AudioClip audio1, audio2, audio3;
  public void init() {
    audio1 = getAudioClip(getCodeBase(), "audio/bong.au");
    audio2 = getAudioClip(getCodeBase(), "audio/joy.au");
    try {
      audio3 = getAudioClip(new URL(getCodeBase(), "audio/return.au"));
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
  }
  public boolean mouseDown(Event e, int x, int y) {
    if (audio1 != null)
      audio1.play();
    return true;
  }
  public void start() {
    if (audio2 != null)
      audio2.loop();
  }
  public void paint(Graphics g) {
    if (audio3 != null)
      audio3.play();
  }
  public void stop() {
    if (audio1 != null)
      audio1.stop();
    if (audio2 != null)
      audio2.stop();
    if (audio3 != null)
      audio3.stop();
  }
}





AudioClip: play()

import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
public class MainClass {
  public static void main(String[] args) {
    try {
      URL url = new URL("file:youraudiofile.wav" );
      AudioClip ac = Applet.newAudioClip(url);
      ac.play();
      System.out.println("Press any key to exit.");
      System.in.read();
      ac.stop();
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}





AudioClip: stop()

import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
public class MainClass {
  public static void main(String[] args) {
    try {
      URL url = new URL("file:youraudiofile.wav" );
      AudioClip ac = Applet.newAudioClip(url);
      ac.play();
      System.out.println("Press any key to exit.");
      System.in.read();
      ac.stop();
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}