[英]What is the correct way to play a sound file using java.sound.sampled.Clip
[英]Correct location for .au sound clip file for Eclipse Java applet
这个问题应该很简单,尽管显然我很想念它。 我有一个带有图像和声音剪辑的Java小程序。 所有图像均正确显示,并且位于/ bin文件中。
但是,声音剪辑也位于/ bin文件中,并且根本没有播放。
问题是:1)声音片段的此代码片段格式正确吗?
mySound = getAudioClip(getDocumentBase(),“ OnPoint.au”);
和2)声音剪辑应该放在哪里,不是/ bin文件夹吗?
下面是代码,请指教,谢谢!
import java.net.*;
import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.applet.Applet;
import java.applet.AudioClip;
导入java.util。*; //计时器在此库中
public class bTrainAnimation extends Applet
{
private Image trainAndBush[]; // "Image" is inherited
private int totalImages = 17, // total number of images in array
currentImage = 0, // set current image array subscript to 0
sleepTime = 500; // milliseconds to sleep
MediaTracker myImageTracker; // "MediaTracker" is inherited
Timer myTimer; // "Timer" is inherited
private AudioClip mySound; // "AudioClip" is a inherited
public void init()
{
// load the images when the applet begins executing
setSize(400,400);
mySound = getAudioClip(getDocumentBase(), "OnPoint.au");
myTimer = new Timer(true);
myTimer.schedule(
new TimerTask()
{
public void run() // "run" is action performed by timer task
{
repaint();
}
}
,0,sleepTime); // global VAR "sleepTime" = 500 milliseconds
// create array of all the images in slide show
trainAndBush = new Image[ totalImages ]; // global VAR "totalImages" = 17 images
myImageTracker = new MediaTracker( this );
for( int i = 0; i < trainAndBush.length; i++ )
{
trainAndBush[i] = getImage(getDocumentBase(), // load an image in an applet
"Hill" + (i + 1 ) + ".jpg" );
// track loading image
myImageTracker.addImage(trainAndBush[i], i);
}
try
{
catch ( InterruptedException e ){}
mySound.play();
}
public void start(Graphics g)
{
g.drawImage(trainAndBush[0],50,50,300,300, this );
currentImage = 1;
public void paint(Graphics g)
{
{
if (myImageTracker.checkID(currentImage, true))
{
g.drawImage(trainAndBush[ currentImage ],50,50,300,300, this );
if (currentImage == 0 )
trainAndBush[ totalImages -1].flush();
else trainAndBush [ currentImage -1].flush();
currentImage = ++currentImage % totalImages;
}
else
postEvent( new Event( this, Event.MOUSE_ENTER, ""));
}
}
// override update to eliminate flicker
public void update(Graphics g)
{
paint( g );
}
}
格式很难阅读您的代码,但我会尽力回答。 (如果您更新/编辑您的问题,我将尝试更新/编辑我的答案):
我不确定您从何处了解“ bin”目录。 如果这适用于图像,则说明正在进行其他操作。
现在,距离我这样做已经有一段时间了,但是我对文档的解释是:
getDocumentBase()为您提供包含页面的URL,因此,如果您的小程序包含在http://example.com/folder/hello.html中 ,那么这就是getDocumentBase()所提供的。
如果您的音频位于
http://example.com/folder/audio/OnPoint.au
那么您的代码将如下所示:
mySound = getAudioClip(getDocumentBase()。parent()。child(“ audio”),“ OnPoint.au”)
请注意,Java的URL类不具有parent()和child()函数(这太容易了!),我只是为了弥补这一点而将其包括在内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.