利用Java Applet播放声音文件

利用Java Applet播放声音文件,第1张

使用Applet播放声音时需首先定义AudioClip对象 GetAudioClip方法能把声音赋予AudioClip对象 如果仅想把声音播放一遍 应调用AudioClip类的play方法 如果想循环把声音剪辑 应选用AudioClip类的loop方法

图像格式各种各样 如BMP GIF和JPEG等 声音文件也一样 WAV和AU是最常用的两种声音文件 目前Java仅支持AU文件 但Windows环境下常用的却是WAV文件 所以最好能有一个可把WAV文件转换为AU文件的工具

播放声音的AudioClip类

AudioClip类用来在Java Applet内播放声音 该类在java Applet包中有定义

下面演示了如何利用AudioClip类播放声音

装入一个名为Sample Au的声音文件并播放(SoundDemo java)

//源程序清单 import java awt ; import java applet public class SoundDemo extends Applet {public void paint(Graphics g){ AudioClip audioClip=getAudioClip(getCodeBase() Sample AU ); //创建AudioClip对象并用//getAudioClip方法将其初始化 g drawstring( Sound Demo! ); audioClip loop() //使用AudioClip类的loop方法循环播放} }

需把如下的HTML语句放入SoundDemo HTML文件 为运行该Applet做准备

<HTML> <TITLE>SoundDemo Applet</TITLE> <APPLET CODE= SoundDemo class WIDTH= HEIGHT= > </APPLET> </HTML>

lishixinzhi/Article/program/Java/hx/201311/27027

package AOMLIN;

import javaapplet;

import javaawt;

import javanet;

import javaawtevent;

import javaio;

import javaxmedia;

public class bofangqi extends Applet implements ControllerListener,

Runnable, ItemListener {

Player player;

String str;

Thread mythread;

Choice choice;

Component visualComponent, controlComponent, progressBar;

String mediaFile;

URL mediaURL, codeBase;

Frame frame;

public void init() {

str = "Music01MPG";

mythread = new Thread(this);

choice = new Choice();

choiceadd("Music01MPG");

choiceadd("Music02MPG");

choiceadd("Music03MPG");

choiceaddItemListener(this);

codeBase = getDocumentBase();

frame = new Frame("视频系统");

framesetSize(660, 580);

frameaddWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

if (player != null) {

playerstop();

playerdeallocate();

}

framesetVisible(false);

Systemexit(0);

}

});

add(choice);

}

public void stop() {

if (player != null) {

playerstop();

}

}

public synchronized void controllerUpdate(ControllerEvent event) {

playergetDuration();

if (event instanceof RealizeCompleteEvent) {

if ((visibleComponent = playergetVisualComponent() != null))

frameadd("Center", visualComponent);

if ((controlComponent = playergetControlPanelComponent()) != null)

if (visualComponent != null)

frameadd("South", controlComponent);

else

frameadd("Center", controlComponent);

framevalidate();

framepack();

} else if (event instanceof PrefetchComponentEvent) {

playerstart();

}

}

public void itemStateChanged(ItemEvent e) {

str = choicegetSelectedItem();

if (player == null) {

} else {

playerstop();

playerdeallocate();

}

frameremoveAll();

framesetVisible(true);

framesetBounds(300, 100, 150, 100);

framevalidate();

if (!(mythreadisAlive())) {

mythread = new Thread(this);

}

try {

mythreadstart();

} catch (Exception ee) {

}

}

public synchronized void run() {

try {

mediaURL = new URL(codeBase, str);

player = ManagercreatePlayer(mediaURL);

playergetDuration();

if (player != null) {

playeraddControllerListener(this);

} else

Systemoutprintln("failed to create player for" + mediaURL);

} catch (MalformedURLException e) {

Systemoutprintln("URL for" + mediaFile + "is invalid");

} catch (NoPlayerException e) {

Systemoutprintln("canot find a player for" + mediaURL);

}

if (player != null) {

playerprefetch();

}

}

}

package login;

import javaioFile;

import javaioIOException;

import javaxsoundsampledAudioFormat;

import javaxsoundsampledAudioInputStream;

import javaxsoundsampledAudioSystem;

import javaxsoundsampledDataLine;

import javaxsoundsampledFloatControl;

import javaxsoundsampledLineUnavailableException;

import javaxsoundsampledSourceDataLine;

import javaxsoundsampledUnsupportedAudioFileException;

public class testviode extends Thread {

private String filename;

private Position curPosition;

private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb

enum Position {

LEFT, RIGHT, NORMAL

};

public testviode(String wavfile) {

filename = wavfile;

curPosition = PositionNORMAL;

}

public testviode(String wavfile, Position p) {

filename = wavfile;

curPosition = p;

}

public void run() {

File soundFile = new File(filename);

if (!soundFileexists()) {

Systemerrprintln("Wave file not found: " + filename);

return;

}

AudioInputStream audioInputStream = null;

try {

audioInputStream = AudioSystemgetAudioInputStream(soundFile);

} catch (UnsupportedAudioFileException e1) {

e1printStackTrace();

return;

} catch (IOException e1) {

e1printStackTrace();

return;

}

AudioFormat format = audioInputStreamgetFormat();

SourceDataLine auline = null;

DataLineInfo info = new DataLineInfo(SourceDataLineclass, format);

try {

auline = (SourceDataLine) AudioSystemgetLine(info);

aulineopen(format);

} catch (LineUnavailableException e) {

eprintStackTrace();

return;

} catch (Exception e) {

eprintStackTrace();

return;

}

if (aulineisControlSupported(FloatControlTypePAN)) {

FloatControl pan = (FloatControl) auline

getControl(FloatControlTypePAN);

if (curPosition == PositionRIGHT)

pansetValue(10f);

else if (curPosition == PositionLEFT)

pansetValue(-10f);

}

aulinestart();

int nBytesRead = 0;

byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

try {

while (nBytesRead != -1) {

nBytesRead = audioInputStreamread(abData, 0, abDatalength);

if (nBytesRead >= 0)

aulinewrite(abData, 0, nBytesRead);

}

} catch (IOException e) {

eprintStackTrace();

return;

} finally {

aulinedrain();

aulineclose();

}

}}

以上就是关于利用Java Applet播放声音文件全部的内容,包括:利用Java Applet播放声音文件、java程序中如何加入声音 java程序中的图片如何实现自动播放!~急、java程序如何播放声音等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10169719.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存