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

所属分类: 软件编程 / java 阅读数: 40
收藏 0 赞 0 分享

播放幻灯片和动画

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

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

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){}
}

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

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()方法为其初始化。代码形式如下:

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


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

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

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

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();
    }
  }
}

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

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("您可以播放了");
    } //获得音频对象后通知可以播放
  }
}

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多