zoukankan      html  css  js  c++  java
  • Java知多少(102)多媒体基础

    本节介绍 Java程序播放幻灯片和动画,播放声音和视频的方法。

    播放幻灯片和动画

    用实例说明播放幻灯片和动画的方法。

    【例 12-7】小应用程序先将幻灯片读入数组在存储,单击鼠标变换幻灯片,逐张显示。

     1 import java.applet.*import java.awt.*;
     2 import java.awt.event.*;
     3 public class Example7_7 extends Applet implements MouseListener{
     4     final int number = 50; //假定幻灯片有50张
     5     int count = 0;
     6     Image[] card = new Image[number];
     7     public void init(){
     8         addMouseListener(this);
     9         for (int i = 0; i < number; i++){
    10             card[i] = getImage(getCodeBase(), "DSC0033" + i + ".jpg");
    11         }
    12     }
    13     public void paint(Graphics g){
    14         if ((card[count]) != null)
    15             g.drawImage(card[count], 10, 10, card[count].getWidth(this),card[count].getHeitht(this), this);
    16     }
    17     public void mousePressed(MouseEvent e){
    18         count = (count + 1) % number; //循环逐张显示
    19         repaint();
    20     }
    21     public void mouseRelease(MouseEvent e){}
    22     public void mouseEntered(MouseEvent e){}
    23     public void mouseExited(Mouse Event e){}
    24     public void mouseClicked(MouseEvent e){}
    25 }

    【例 12-8】小应用程序说明播放动画的方法,要求播放的图片和小程序放在相同的目录中,程序通过快速显示一组图片造成显示动画的效果。小应用程序利用线程控制动画图片的逐显示。

     1 import java.applet.*;
     2 import java.awt.*;
     3 import java.awt.event.*;
     4 public class Example7_8 extends Applet implements Runnable{
     5     final int number = 50;
     6     int count = 0;
     7     Thread mythread;
     8     Image[] pic = new Image[number];
     9     public void init(){
    10         setSize(300, 200);
    11         for (int i = 0; i <= number; i++){
    12             //载入动画图片
    13             pic[i - 1] = getImage(getCodeBase(), "DSC0033" + i + ".jpg");
    14         }
    15     }
    16     public void start(){
    17         mythread = new Thread(this); //创建一个线程
    18         mythread.start(); //启动线程执行
    19     }
    20     public void stop(){
    21         mythread = null;
    22     }
    23     public void run(){
    24         //线程的执行代码
    25         while (true){
    26             repaint();
    27             count = (count + 1) % number; //改变显示的图片号
    28             try{
    29                 mhythread.sleep(200);
    30             }
    31             catch (InterruptedExeception e){}
    32         }
    33     }
    34     public void paint(Graphics g){
    35         if ((pic[count] != null)
    36             g.drawImage(pic[count], 10, 10, pic[count].getwidth(this), pic[count].getHeight(this), this);
    37     }
    38 }

    播放声音

    Java语言老根据地的音频格式有多种:au、aiff、wav、midi、rfm等。小程序要播放音频文件,可使用类AudioClip,该类在java.applet.AudioClip类库中定义。小程序先创建AudioClip对象,并用getAudioClip()方法为其初始化。代码形式如下:
        AudioClip audioClip = getAudioClip(getCodeBase(),”myAudioClipFile.au”);
    如果要从网上获得音频文件,可用方法getAudioClip(URL url, String name),根据url地址及音频文件name获得可播放的音频对象。

    控制声音的播放有3个方法:play()播放声音,loop()循环播放和stop()停止播放。

    【例 12-9】能播放声音的小应用程序。

     1 import java.applet.*;
     2 import java.awt.*;
     3 import java.awt.event.*;
     4 public class Example7_9 extends Applet implements ActionListener{
     5     AudioClip clip; //声明一个音频对象
     6     Button buttonPlay, buttonLoop, buttonStop;
     7     public void init(){
     8         clip = getAudioClip(getCodeBase(), "2.wav");
     9         //根据程序所在地址处声音文件2.wav创建音频对象,
    10         //Applet类的getCodeBase()方法可以获得小程序所在的html页面的URL地址。
    11         buttonPlay = new Button("开始播放");
    12         buttonLoop = new Button("循环播放");
    13         buttonStop = new Button("停止播放");
    14         buttonPlay.addActionListener(this);
    15         buttonStop.addActionListener(this);
    16         buttonLoop.addActionListener(this);
    17         add(buttonPlay);
    18         add(buttonLoop);
    19         add(buttonStop);
    20     }
    21     public void stop(){
    22         clip.stop(); //当离开此页面时停止播放
    23     }
    24     public void actionPerformed(ActionEvent e){
    25         if (e.getSource() == buttonPlay){
    26             clip.play();
    27         }
    28         else if (e.getSource() == buttonLoob){
    29             clip.loop();
    30         }
    31         else if (e.getSource() == buttonStop){
    32             clip.stop();
    33         }
    34     }
    35 }

    【例 12-10】如果声音文件较大或网络速度慢会影响小程序的初始化工作。这可用多线程技术解决。在一个级别较低的线程中完成音频对象的创建,即由后台载入声音文件,前台播放。

     1 import java.applet.*;
     2 import java.awt.*;
     3 import java.awt.event.*;
     4 public class Hanoi extends applet implements Runnable, ActionListener{
     5     AudioClip clip; //声明一个音频对象
     6     textField text;
     7     Thread thread;
     8     Button buttonPlay, buttonLoop, buttonStop;
     9     public void init(){
    10         thread = new Thread(this); //创建新线程
    11         thread .setPriority(Thread.MIN_PRIORITY);
    12         buttonPlay = new Button("开始播放");
    13         buttonLoop = new Button( "循环播放");
    14         buttonStop = new Button("停止播放");
    15         text = new textField(12);
    16         buttonPlay.addActionListener(this);
    17         buttonStop.addActionListener(this);
    18         buttonLoop.addActionListener(this);
    19         add(buttonPlay);
    20         add(buttonLoop);
    21         add(buttonStop);
    22         add(text);
    23     }
    24     public void start(){
    25         thread.start();
    26     }
    27     public void stop(){
    28         clip.stop();
    29     }
    30     public void actionPerformed(ActionEvent e){
    31         if (e.getSource() == buttonPlay(){
    32             clip.play();
    33         }
    34         else if (e.getSource() == buttonLoop(){
    35             clip.loop();
    36         }
    37         else if (e.getSource() == buttonStop(){
    38             clip.stop();
    39         }
    40     }
    41     public void run(){
    42         //在线程thread 中创建音频对象
    43         clip = getAudioclip(getCodeBase(), "2.wav");
    44         text.setText("请稍等"); 
    45         if(clip ! = null){
    46             buttonPlay.setBackground(Color.red); buttonLoop.setBackground(Color.green); text.setText("您可以播放了");
    47         } //获得音频对象后通知可以播放
    48     }
    49 }

    系列文章:

  • 相关阅读:
    January 25th, 2018 Week 04th Thursday
    January 24th, 2018 Week 04th Wednesday
    January 23rd, 2018 Week 04th Tuesday
    January 22nd, 2018 Week 04th Monday
    January 21st, 2018 Week 3rd Sunday
    January 20th, 2018 Week 3rd Saturday
    January 19th, 2018 Week 3rd Friday
    January 18th, 2018 Week 03rd Thursday
    January 17th, 2018 Week 03rd Wednesday
    January 16th, 2018 Week 03rd Tuesday
  • 原文地址:https://www.cnblogs.com/Coda/p/4598579.html
Copyright © 2011-2022 走看看