服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - Java常用的一些多媒体文件基本操作方法简介

Java常用的一些多媒体文件基本操作方法简介

2020-01-09 14:11goldensun JAVA教程

这篇文章主要介绍了Java常用的一些多媒体文件基本操作方法,包括对音频视频以及幻灯片的播放,需要的朋友可以参考下

播放幻灯片和动画

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.applet.*import java.awt.*;
import java.awt.event.*;
public class Example7_7 extends Applet implements MouseListener{
  final int number = 50; //假定幻灯片有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], 10, 10, card[count].getWidth(this),card[count].getHeitht(this), this);
  }
  public void mousePressed(MouseEvent e){
    count = (count + 1) % number; //循环逐张显示
    repaint();
  }
  public void mouseRelease(MouseEvent e){}
  public void mouseEntered(MouseEvent e){}
  public void mouseExited(Mouse Event e){}
  public void mouseClicked(MouseEvent e){}
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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++){
      //载入动画图片
      pic[i - 1] = getImage(getCodeBase(), "DSC0033" + i + ".jpg");
    }
  }
  public void start(){
    mythread = new Thread(this); //创建一个线程
    mythread.start(); //启动线程执行
  }
  public void stop(){
    mythread = null;
  }
  public void run(){
    //线程的执行代码
    while (true){
      repaint();
      count = (count + 1) % number; //改变显示的图片号
      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);
  }
}

播放声音

Java语言老根据地的音频格式有多种:au、aiff、wav、midi、rfm等。小程序要播放音频文件,可使用类AudioClip,该类在java.applet.AudioClip类库中定义。小程序先创建AudioClip对象,并用getAudioClip()方法为其初始化。代码形式如下:

?
1
AudioClip audioClip = getAudioClip(getCodeBase(),”myAudioClipFile.au”);


如果要从网上获得音频文件,可用方法getAudioClip(URL url, String name),根据url地址及音频文件name获得可播放的音频对象。

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

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

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

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

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

延伸 · 阅读

精彩推荐