Y
yingjian.ma1955
Hi,
I have a program to play a .wav music. But it plays a broken music.
When I play it using Real Player, it is OK. How can I fix it and why
does it play a broken music? You can use your .wav file to try it.
Thanks a lot.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class Music extends JFrame {
public Music() {
super("Music");
setSize(190, 80);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
FlowLayout flo = new FlowLayout();
content.setLayout(flo);
AButton p = new AButton();
content.add(p);
setContentPane(content);
setVisible(true);
}
public static void main(String[] arguments) {
Music m = new Music();
}
}
class AButton extends JButton implements Runnable, ActionListener {
AudioClip[] a = new AudioClip[1];
Thread runner;
AButton() {
super("Play");
addActionListener(this);
try {
URL u = new URL("file:ladieu.wav");
a[0] = JApplet.newAudioClip(u);
} catch (MalformedURLException e) { }
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command == "Play")
startMusic();
if (command == "Stop")
stopMusic();
}
void startMusic() {
if (runner == null) {
runner = new Thread(this);
runner.start();
setText("Stop");
}
}
void stopMusic() {
a[0].stop();
runner = null;
setText("Play");
}
public void run() {
a[0].loop();
}}
I have a program to play a .wav music. But it plays a broken music.
When I play it using Real Player, it is OK. How can I fix it and why
does it play a broken music? You can use your .wav file to try it.
Thanks a lot.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class Music extends JFrame {
public Music() {
super("Music");
setSize(190, 80);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
FlowLayout flo = new FlowLayout();
content.setLayout(flo);
AButton p = new AButton();
content.add(p);
setContentPane(content);
setVisible(true);
}
public static void main(String[] arguments) {
Music m = new Music();
}
}
class AButton extends JButton implements Runnable, ActionListener {
AudioClip[] a = new AudioClip[1];
Thread runner;
AButton() {
super("Play");
addActionListener(this);
try {
URL u = new URL("file:ladieu.wav");
a[0] = JApplet.newAudioClip(u);
} catch (MalformedURLException e) { }
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command == "Play")
startMusic();
if (command == "Stop")
stopMusic();
}
void startMusic() {
if (runner == null) {
runner = new Thread(this);
runner.start();
setText("Stop");
}
}
void stopMusic() {
a[0].stop();
runner = null;
setText("Play");
}
public void run() {
a[0].loop();
}}