java播放音乐的问题 急!!!!!!!!!
我建立了两个线程,用来播放两个不同的音乐“m1","m2";第一个音乐能播放,可是只有当我点击按钮时(停止第一个音乐,播放第二个音乐),就报错了,,,Exceptioni...
我建立了两个线程,用来播放两个不同的音乐“m1","m2";第一个音乐能播放,可是只有当我点击按钮时(停止第一个音乐,播放第二个音乐),就报错了,,,
Exception in thread "Thread-3" java.lang.OutOfMemoryError: Java heap space
at com.sun.media.sound.JavaSoundAudioClip.readStream(Unknown Source)
at com.sun.media.sound.JavaSoundAudioClip.loadAudioData(Unknown Source)
at com.sun.media.sound.JavaSoundAudioClip.<init>(Unknown Source)
at sun.applet.AppletAudioClip.createAppletAudioClip(Unknown Source)
at sun.applet.AppletAudioClip.<init>(Unknown Source)
at java.applet.Applet.newAudioClip(Unknown Source)
at EscWindow.Base_Frame.getMusic2(Base_Frame.java:54)
at EscWindow.Base_Frame$2.run(Base_Frame.java:36)
at java.lang.Thread.run(Unknown Source)
该怎么怎么实现呢????求高手帮忙解决下,,,很急,,,!!!!!!!!!!
package EscWindow;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Base_Frame extends JFrame
{
private JButton button=new JButton("Start");
private AudioClip music1,music2;
private URL url,url1;
private Thread t,t1;
public Base_Frame()
{
super.setSize(400, 499);
super.setBackground(null);
this.getContentPane().add(button);
button.setBounds(100, 100, 60, 60);
t=new Thread(new Runnable()
{
public void run()
{
getMusic1();
}
});
t1=new Thread(new Runnable()
{
public void run()
{
getMusic2();
}
});
t.start();
AL();
super.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void getMusic1()
{
url=Base_Frame.class.getResource("m1.au");
music1=Applet.newAudioClip(url);
music1.play();
}
public void getMusic2()
{
url1=Base_Frame.class.getResource("m2.au");
music2=Applet.newAudioClip(url1);
music2.play();
}
public void AL()
{
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t.interrupt();
music1.stop();
//url=null;
//new window();
t1.start();
}
});
}
public static void main(String args[])
{
new Base_Frame();
}
} 展开
Exception in thread "Thread-3" java.lang.OutOfMemoryError: Java heap space
at com.sun.media.sound.JavaSoundAudioClip.readStream(Unknown Source)
at com.sun.media.sound.JavaSoundAudioClip.loadAudioData(Unknown Source)
at com.sun.media.sound.JavaSoundAudioClip.<init>(Unknown Source)
at sun.applet.AppletAudioClip.createAppletAudioClip(Unknown Source)
at sun.applet.AppletAudioClip.<init>(Unknown Source)
at java.applet.Applet.newAudioClip(Unknown Source)
at EscWindow.Base_Frame.getMusic2(Base_Frame.java:54)
at EscWindow.Base_Frame$2.run(Base_Frame.java:36)
at java.lang.Thread.run(Unknown Source)
该怎么怎么实现呢????求高手帮忙解决下,,,很急,,,!!!!!!!!!!
package EscWindow;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Base_Frame extends JFrame
{
private JButton button=new JButton("Start");
private AudioClip music1,music2;
private URL url,url1;
private Thread t,t1;
public Base_Frame()
{
super.setSize(400, 499);
super.setBackground(null);
this.getContentPane().add(button);
button.setBounds(100, 100, 60, 60);
t=new Thread(new Runnable()
{
public void run()
{
getMusic1();
}
});
t1=new Thread(new Runnable()
{
public void run()
{
getMusic2();
}
});
t.start();
AL();
super.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void getMusic1()
{
url=Base_Frame.class.getResource("m1.au");
music1=Applet.newAudioClip(url);
music1.play();
}
public void getMusic2()
{
url1=Base_Frame.class.getResource("m2.au");
music2=Applet.newAudioClip(url1);
music2.play();
}
public void AL()
{
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t.interrupt();
music1.stop();
//url=null;
//new window();
t1.start();
}
});
}
public static void main(String args[])
{
new Base_Frame();
}
} 展开
展开全部
刚学不久也来琢磨琢磨,个人理解是监听程序内部的线程上的问题~
个人理解:本来调用interrupt是可以终止,但是他还是会运行一个内部程序后才停止,也就是MUSIC1运行结束后,个人觉得配合Thread.sleep()(放在music1里面)用个异常测试下,捕获异常让异常运行第二个音乐不知道可行不~~
关注中~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
个人理解:本来调用interrupt是可以终止,但是他还是会运行一个内部程序后才停止,也就是MUSIC1运行结束后,个人觉得配合Thread.sleep()(放在music1里面)用个异常测试下,捕获异常让异常运行第二个音乐不知道可行不~~
关注中~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java的语言我还没有精通。无能为力...
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一定是哪里有死循环
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询