473,513 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Playing sounds in applications

Hi all,

Here 's the trouble I've been dealing with for several days.
I'm building a small game with JAVA and I would like to play sounds inside.
Here's my code :
try {
testUrl = new URL("file:" + new File(".").getCanonicalPath() + "/" +
filename);
testClip=Applet.newAudioClip(testUrl);
testClip.play();
}catch(Exception e){
System.out.println(testUrl);
}

It looks good, but it doesn't work !!!
I hear nothing, although I'm sure there is a file at the position pointed to
by testUrl.

I insist that it is a JAVA application (not an applet).
I use JBuilder X.

Any suggestion ?

Thanks in advance




Jul 17 '05 #1
2 2269
beve wrote:
Hi all,

Here 's the trouble I've been dealing with for several days.
I'm building a small game with JAVA and I would like to play sounds inside.
Here's my code :
try {
testUrl = new URL("file:" + new File(".").getCanonicalPath() + "/" +
filename);
testClip=Applet.newAudioClip(testUrl);
testClip.play();
}catch(Exception e){
System.out.println(testUrl);
}

It looks good, but it doesn't work !!!
I hear nothing, although I'm sure there is a file at the position pointed to
by testUrl.

I insist that it is a JAVA application (not an applet).
I use JBuilder X.

Any suggestion ?


The APIs you have used in the code above only work within applets. Your
first hint should have been the use of the Applet class. :)

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2
> The APIs you have used in the code above only work within applets. Your
first hint should have been the use of the Applet class. :)


Not so. newAudioClip is a static method, so you don't have to have an applet
going.

Here's code that works for me. This is in a class named SongPlayer that
extends JFrame, not an applet. The music1.mid file is in the same directory
as SongPlayer.java.

URL url = SongPlayer.class.getResource("music1.mid");
System.out.println(url.toString());
AudioClip clip = Applet.newAudioClip(url);
clip.play();

Good luck,

--Doug
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9265
by: Kiteman \(Canada\) | last post by:
Instead of a beep sound playing as an alarm or reminder I would like to be able to play a more interesting sound. On the web, I found and tried a sound routine that uses the SOUND.DRV but it does...
1
2814
by: Peter Schmitz | last post by:
Hi, I'm just trying to play a *.wav file, making use of the unmanaged function "sndPlaySoundA" in winmm.dll, but I still got one question: How can I avoid, that the sound file itself has to be...
0
1146
by: Adrian | last post by:
Is there a way in C# to play various sounds (WAV files) simultaneously? I also need to jump to the middle of the WAV files and start playing from there, not just from the beginning. Thanks.
8
2516
by: shlomi | last post by:
Hello, It's been long time that I'm investigating on how to play Avi file from memory, with no success. What I'm trying to do is to get a scrambled Avi file to memory, fix same data and...
1
1486
by: Tom W | last post by:
hi I've found some code for playing sounds and it kinda works. This is what I have \\\\\Public Class SoundClas Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name As String, ByVal hmod As...
1
3734
by: Terry Olsen | last post by:
Using the following routine to play embedded wav files: Private Sub PlaySound(ByVal Sound As Int32) Select Case Sound Case 1 : My.Computer.Audio.Play(My.Resources.Sounds.GoOnline,...
5
5698
by: Torquemada | last post by:
Hello I want to know why my applications stops responding for approx. 1 sec when I play sounds rapidly. This is the method I use private void playSound(int index) { //sound.stop();...
1
2161
by: poo22 | last post by:
Please help, i've looked everywhere on the internet for the answer, but nothing works! here is my problem: I have a Pong game, and would like to have a background .wav file playing. I also have...
1
1342
by: sandeepk84 | last post by:
Hi all... I wanted to add a functionality to my software for recording and playing sounds. Does anyone know how it can be done in java? How Sound API can be used? Even if it supports only some...
0
7264
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7166
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7543
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7534
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3236
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
459
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.