A brief introduction to the basic operation methods of some multimedia files commonly used in Java

  • 2020-04-01 04:14:56
  • OfStack

Show slides and animations

Illustrate the method of displaying slides and animations with examples.

Applets first read the slides into the array and store them, then click the mouse to transform the slides and display them one by one.


import java.applet.*import java.awt.*;
import java.awt.event.*;
public class Example7_7 extends Applet implements MouseListener{
  final int number = 50; //Let's say I have 50 slides
  int count = 0;
  Image[] card = new Image[number];
  public void init(){
    addMouseListener(this);
    for (int i = 0; i < number; i++){
      card[i] = getImage(getCodeBase(), "DSC0033" + i + ".jpg");
    }
  }
  public void paint(Graphics g){
    if ((card[count]) != null)
      g.drawImage(card[count], 10, 10, card[count].getWidth(this),card[count].getHeitht(this), this);
  }
  public void mousePressed(MouseEvent e){
    count = (count + 1) % number; //The loop is shown page by page
    repaint();
  }
  public void mouseRelease(MouseEvent e){}
  public void mouseEntered(MouseEvent e){}
  public void mouseExited(Mouse Event e){}
  public void mouseClicked(MouseEvent e){}
}

Applets show how to play animations. They require the pictures to be placed in the same directory as the applets. Applets use threads to control the sequential display of animated images.


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example7_8 extends Applet implements Runnable{
  final int number = 50;
  int count = 0;
  Thread mythread;
  Image[] pic = new Image[number];
  public void init(){
    setSize(300, 200);
    for (int i = 0; i <= number; i++){
      //Loading animated images
      pic[i - 1] = getImage(getCodeBase(), "DSC0033" + i + ".jpg");
    }
  }
  public void start(){
    mythread = new Thread(this); //Create a thread
    mythread.start(); //Start thread execution
  }
  public void stop(){
    mythread = null;
  }
  public void run(){
    //Thread execution code
    while (true){
      repaint();
      count = (count + 1) % number; //Change the number of images displayed
      try{
        mhythread.sleep(200);
      }
      catch (InterruptedExeception e){}
    }
  }
  public void paint(Graphics g){
    if ((pic[count] != null)
      g.drawImage(pic[count], 10, 10, pic[count].getwidth(this), pic[count].getHeight(this), this);
  }
}

The play

There are many audio formats in the old base of Java language: au, aiff, wav, MIDI, RFM, etc. To play an audio file, a small program can use the AudioClip class, which is defined in the java.applet.AudioClip class library. The applet creates the AudioClip object and initializes it with the getAudioClip() method. The code form is as follows:


  AudioClip audioClip = getAudioClip(getCodeBase(), " myAudioClipFile.au " );


If you want to get an audio file from the web, you can use the method getAudioClip(URL URL, String name) to get a playable audio object based on the URL address and the audio file name.

There are three ways to control the sound: play(), loop(), and stop().

Small application that plays sound.


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example7_9 extends Applet implements ActionListener{
  AudioClip clip; //Declare an audio object
  Button buttonPlay, buttonLoop, buttonStop;
  public void init(){
    clip = getAudioClip(getCodeBase(), "2.wav");
    //2. Wav creates audio objects according to the address of the program.
    //The getCodeBase() method of the Applet class gets the URL address of the HTML page on which the Applet resides.
    buttonPlay = new Button(" Start playing ");
    buttonLoop = new Button(" Loop for ");
    buttonStop = new Button(" Stop playing ");
    buttonPlay.addActionListener(this);
    buttonStop.addActionListener(this);
    buttonLoop.addActionListener(this);
    add(buttonPlay);
    add(buttonLoop);
    add(buttonStop);
  }
  public void stop(){
    clip.stop(); //Stop playing when leaving this page
  }
  public void actionPerformed(ActionEvent e){
    if (e.getSource() == buttonPlay){
      clip.play();
    }
    else if (e.getSource() == buttonLoob){
      clip.loop();
    }
    else if (e.getSource() == buttonStop){
      clip.stop();
    }
  }
}

If the sound file is large or the network is slow, the initialization of the applet will be affected. This can be solved by multi-threading. In a lower level of the thread to complete the creation of audio objects, that is, by the background load sound file, the foreground playback.


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Hanoi extends applet implements Runnable, ActionListener{
  AudioClip clip; //Declare an audio object
  textField text;
  Thread thread;
  Button buttonPlay, buttonLoop, buttonStop;
  public void init(){
    thread = new Thread(this); //Create a new thread
    thread .setPriority(Thread.MIN_PRIORITY);
    buttonPlay = new Button(" Start playing ");
    buttonLoop = new Button( " Loop for ");
    buttonStop = new Button(" Stop playing ");
    text = new textField(12);
    buttonPlay.addActionListener(this);
    buttonStop.addActionListener(this);
    buttonLoop.addActionListener(this);
    add(buttonPlay);
    add(buttonLoop);
    add(buttonStop);
    add(text);
  }
  public void start(){
    thread.start();
  }
  public void stop(){
    clip.stop();
  }
  public void actionPerformed(ActionEvent e){
    if (e.getSource() == buttonPlay(){
      clip.play();
    }
    else if (e.getSource() == buttonLoop(){
      clip.loop();
    }
    else if (e.getSource() == buttonStop(){
      clip.stop();
    }
  }
  public void run(){
    //Create an audio object in a thread
    clip = getAudioclip(getCodeBase(), "2.wav");
    text.setText(" Please wait a moment "); 
    if(clip ! = null){
      buttonPlay.setBackground(Color.red); buttonLoop.setBackground(Color.green); text.setText(" You are ready to play ");
    } //The notification can be played after obtaining the audio object
  }
}


Related articles: