File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Playing Sound in Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Playing Sound in Applet" Watch "Playing Sound in Applet" New topic
Author

Playing Sound in Applet

Bagesh Tripathi
Greenhorn

Joined: Jul 04, 2007
Posts: 6
Hi,

Is it possible to play (online .wav file) sound in Applet using URL Like
(http://Music/demo/abc.wav)

I have tried

URL ur=new URL("file:\\C:\\Demo\\abc.wav");
AudioClip clip =Applet.newAudioClip(ur);
It's working fine
In fact i have tried getAudioClip() to run "http://Music/demo/abc.wav" but it's not working.

Actually i want to play "http://" URL file directly in applet
Please help me.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
Welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

As to your question, what does "it doesn't work" mean? Are there any exceptions? Does the browser download the file if you enter "http://Music/demo/abc.wav" in the URL field?


Ping & DNS - my free Android networking tools app
Bagesh Tripathi
Greenhorn

Joined: Jul 04, 2007
Posts: 6
Hi,

Thank you for response.Yes,the browser is downloading the file.
The code i'am using is given below:
------------------------------------
package Sound;

import java.io.IOException;
import java.net.*;
import java.applet.*;


public class Sound2{
public static void main(String a[])
{
try {
String str1="http://simplythebest.net/sounds/WAV" +
"/WAV_files/cartoon_WAV_files/aladdin_goodbye.wav";

String str2="file:\\C:\\Documents and Settings\\btripathi" +
"\\Desktop\\ChatRoom_15April_Backup\\client\\sound\\ringin.wav";


URL url=new URL(str1); // 1
AudioClip clip =Applet.newAudioClip(url);
clip.play();
}catch(java.net.MalformedURLException e)
{
e.printStackTrace();
}


}
}
---------------------------------------------
Now when i pass str1 (as shown in //1) it,s workin fine,But whenever i pass str2 or whole URL("http://....").I'am unable to hear any sound.Even i'm not getting a single error.
[ July 05, 2007: Message edited by: yoshino ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
It's a bit unclear what is now working, and what isn't.

As I understand it, this works:

but this doesn't

and neither does this

Is that correct? That the second one wouldn't work is very strange - it's the same as the first one.

Also, your display name still doesn't conform to the policy. It must consist of a first name and a last name, and not be obviously fictitious. Please take a moment to change it, which you can do right here.
Bagesh Tripathi
Greenhorn

Joined: Jul 04, 2007
Posts: 6
Sorry for creating confusion Actually

URL url=new URL("http://simplythebest.net/sounds/WAV/WAV_files/cartoon_WAV_files/aladdin_goodbye.wav");
is NOT working &
------------------------
URL url=new URL("file:\\C:\\Documents and Settings\\btripathi\\Desktop\\ChatRoom_15April_Backup\\client\\sound\\ringin.wav");
is working FINE
--------------------------

That is while accessing sound file from local system it's working fine,but while accessing file in Network/Internet it's not working.
[ July 05, 2007: Message edited by: Bagesh ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
The problem is that the application quits before the clip is loaded. Both the newAudiopClip and play methods return immediately, even if the the clip isn't loaded. If you insert a "Thread.sleep(10000)" after the call to play, you should hear the clip playing.

Also, thanks for changing your display name. Unfortunately, it's still not valid, because -as I said above- it needs to have a first name and a last name. Please fix it accordingly.
[ July 05, 2007: Message edited by: Ulf Dittmer ]
Bagesh Tripathi
Greenhorn

Joined: Jul 04, 2007
Posts: 6
Hi,

Thank you for your response.
i'm using "Thread.sleep(10000)".But still I'm not getting the desired result(no sound )
Given below is the code.Please help me where i'm going wrong

package Sound;

import java.io.IOException;
import java.net.*;
import java.applet.*;

public class Sound2 {
public static void main(String a[]) throws java.net.MalformedURLException {

String str1 = "http://simplythebest.net/sounds/WAV/WAV_files/cartoon_WAV_files/aladdin_goodbye.wav";

URL url = new URL(str1);
AudioClip clip = Applet.newAudioClip(url);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
clip.play();
}
}

Thanks
[ July 05, 2007: Message edited by: Bagesh Tripathi ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
You need to put the call to Thread.sleep after the call to clip.play.
Bagesh Tripathi
Greenhorn

Joined: Jul 04, 2007
Posts: 6
Hi,
Done this also (i.e putting Thread.sleep after the call to clip.play) no success.If possible please provide some excerpts\source code.It would be great help to me.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
This works for me:
Bagesh Tripathi
Greenhorn

Joined: Jul 04, 2007
Posts: 6
Hi,

First of all Thanks a lot for your information & the solution you provided.
Given below is my code snipplet:
-------------------------------
import java.net.*;
import java.applet.*;

public class Sound2 {
public static void main(String a[]) throws java.net.MalformedURLException, InterruptedException {
URL url = new URL("http://simplythebest.net/sounds/WAV/WAV_files/cartoon_WAV_files/aladdin_goodbye.wav");
AudioClip clip = Applet.newAudioClip(url);
clip.play();
Thread.sleep(10000);
}
}
-------------------------------------------

I don't know whats going wrong but i'm still not able to hear the sound.

Thanks
 
Consider Paul's rocket mass heater.
 
subject: Playing Sound in Applet
 
Similar Threads
mark/reset not supported on getResourceAsStream
How to play sound files from remote client in browser
Where to park my abc.wav file in the Netbeans IDE
Add music to applet?
Java Media Audio (Record sound Example)