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

Source: Internet
Author: User

Play Slides and animations

Use examples to illustrate how slides and animations are played.

The example applet first reads the slides into the array at the store, clicks the mouse to transform the slides, and displays them per sheet.

Import Java.applet.*import java.awt.*;
Import java.awt.event.*;
public class Example7_7 extends Applet implements mouselistener{
  Final int number = 50;//Assuming the slide has 50
  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], ten, Card[count]. GetWidth (This), card[count].getheitht (this), this);
  public void mousepressed (MouseEvent e) {
    count = (count + 1)%;//loop-by-sheet
    display repaint ();
  }
  The public void Mouserelease (MouseEvent e) {} is public
  void mouseentered (MouseEvent e) {} public
  void mouseexited ( Mouse Event e) {} public
  void mouseclicked (MouseEvent e) {}
}

The example applet explains how to play the animation, requiring that the pictures and applet be played in the same directory, and that the program displays the animation by displaying a set of pictures quickly. Small applications use threads to control the display of animated pictures.

 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++) {//load animated picture Pic[i-1] = GetImage (GetCodeBase (), "DSC0033" + i + ". jpg");  The public void Start () {mythread = new thread (this);//Create a thread mythread.start ();/start thread execution} public void
  Stop () {mythread = null;
      The execution code while (true) of the public void run () {//thread) {repaint (); Count = (count + 1)%;
      Change the displayed picture number try{mhythread.sleep (200); The catch (Interruptedexeception e) {}} is public void paint (Graphics g) {if ((Pic[count)!= null) G.
  DrawImage (Pic[count], ten, pic[count].getwidth (this), Pic[count].getheight (this), this); }
}

Play sound

The Java language Logen has a variety of audio formats: AU, AIFF, WAV, MIDI, RFM, and so on. Applet to play audio files, you can use the class AudioClip, which is defined in the Java.applet.AudioClip class library. The applet first 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 obtain audio files from the Internet, you can use the method getaudioclip (URL url, String name) to obtain a playable audio object based on the URL address and audio file name.

There are 3 ways to control sound playback: Play () plays sound, loop () loops and stop () stops playing.

"Example" a small application that can play a 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");
    The GetCodeBase () method of the//applet class can obtain the URL address of the HTML page where the applet resides, based on the audio file 2.wav created by the program's address.
    Buttonplay = New button ("Start playing");
    Buttonloop = New button ("Loop play");
    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.getsour
    CE () = = Buttonplay) {clip.play ();
    else if (e.getsource () = = Buttonloob) {clip.loop ();
    else if (e.getsource () = = Buttonstop) {clip.stop (); }
  }
}

Example if the sound file is larger or the network speed is slow, the initialization of the applet is affected. This can be solved with multithreading technology. Completes the creation of an audio object in a lower-level thread, that is, a sound file is loaded from the background and the foreground plays.

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);/////////////////(thread.min_priority);
    Buttonplay = New button ("Start playing");
    Buttonloop = New button ("Loop play");
    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 ();
    Create an audio object in public void run () {//thread 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 Can Play"
    ); ///Get audio object after notification can play}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.