这个问题应该很简单,尽管我显然很想念它。我有一个带有图像和声音剪辑的 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 );
}
}