Question about Computers & Internet

1 Answer

How to play audio clip in java - Computers & Internet

Posted by on

1 Answer

  • Level 3:

    An expert who has achieved level 3 by getting 1000 points

    All-Star:

    An expert that got 10 achievements.

    MVP:

    An expert that got 5 achievements.

    President:

    An expert whose answer got voted for 500 times.

  • Master
  • 1,952 Answers

  • Create an AudioClip object
  • Load .au sound file into AudioClip
  • Play sounds once or loop continuously
  • Stop playback


Here's how the code for these steps looks:
import java.applet.*;
AudioClip ac = getAudioClip(getCodeBase(), soundFile);
ac.play(); //play once
ac.stop(); //stop playing
ac.loop(); //play continuously


It would seem logical to use this same code to play audio clips in a Java application. Unfortunately, if you do that you will get errors from the compiler. Why? Because the AudioClip object and the getAudioClip() method are part of the java.applet package -- and are not part of applications. The good news is we can dive down and make things work ourselves.
The trick to solving this problem is to use some undocumented features that are provided by Sun in its JDK. Taking a peek inside the classes.zip file from the Sun JDK (using any of the various zipfile utilities), we find not only the standard Java packages such as java.applet but also sun.audio. (These are in the directory sun/audio.)
The sun.audio package contains everything we need to be able to play audio clips! Here's the code:
import sun.audio.*; //import the sun.audio package
import java.io.*;
//** add this into your application code as appropriate
// Open an input stream to the audio file.
InputStream in = new FileInputStream(Filename);
// Create an AudioStream object from the input stream.
AudioStream as = new AudioStream(in);
// Use the static class member "player" from class AudioPlayer to play
// clip.
AudioPlayer.player.start(as);
// Similarly, to stop the audio.
AudioPlayer.player.stop(as);


To use a URL as the audio stream source, substitute the following for the input stream and audio stream setup:
AudioStream as = new AudioStream (url.openStream());


Playing the audio stream continuously adds a bit more complexity:
// Create audio stream as discussed previously.
// Create AudioData source.
AudioData data = as.getData();
// Create ContinuousAudioDataStream.
ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);
// Play audio.
AudioPlayer.player.play (cas);
// Similarly, to stop the audio.
AudioPlayer.player.stop (cas);

Posted on Jan 22, 2009

1 Suggested Answer

6ya6ya
  • 2 Answers

SOURCE: I have freestanding Series 8 dishwasher. Lately during the filling cycle water hammer is occurring. How can this be resolved

Hi,
a 6ya expert can help you resolve that issue over the phone in a minute or two.
best thing about this new service is that you are never placed on hold and get to talk to real repairmen in the US.
the service is completely free and covers almost anything you can think of (from cars to computers, handyman, and even drones).
click here to download the app (for users in the US for now) and get all the help you need.
goodluck!

Posted on Jan 02, 2017

Add Your Answer

Uploading: 0%

my-video-file.mp4

Complete. Click "Add" to insert your video. Add

×

Loading...
Loading...

Related Questions:

1 Answer

Java has fatal errors when I play pogo. chat box covers part of game


Try updating the Java run-time on your computer. If it is out of date, your Java content may not work. You can update Java at http://www.Java.com.

Regards,

D

Sep 03, 2011 | Computers & Internet

1 Answer

Message pop up "vtdisp driver


Install JAVA from here:

http://www.java.com/en/download/index.jsp


Why download Java? Java technology allows you to work and play in a secure computing environment.
Java allows you to play online games, chat with people around the world, calculate your mortgage interest, and view images in 3D, just to name a few.
After you've downloaded Java, visit java.com to check out Java in Action in your daily life.

Try also to upgrade your graphic driver.
Download a new one from the manufactures site.

Hope this was helpfull.

Mar 12, 2011 | Computers & Internet

2 Answers

My sound is not work..i instaled the driver, at realtek manager demo i hear, but at movie, song, or youtube..not work...i have motherboad asusM2A-VM ...ty


I hate to say it, but I often run into problems with my audio dying when I'm attempting to adjust the audio properties on a flash-based video stream. The only way I can get it to reset without rebooting is to throw my PC into "Stand By" and then bringing it back out of it. This resets all of the multimedia hardware on my PC. It's crude, but it works.

Mar 02, 2011 | Computers & Internet

2 Answers

I need to get java on my computer so i can play my games please tell me how


Hello,

you can download java software from the link below. After downloading, you can install it on your computer.

http://www.java.com/en/

Click on "Free java download" one the link opens, and it will be downloaded on your computer> You can install it thereafter and your computer will be able to play games.

Good luck.

Apr 17, 2010 | Microsoft Windows Vista Home Premium with...

1 Answer

Needed to know why it won't let me play my club pogo games because every time i go to play and pick a game to play and get to go into the rooms to play it keeps popping up that i need to install Java which...


Please follow these instructions to enable Java though your Web browser:

Internet Explorer
  1. Click "Tools" --> "Internet Options"
  2. Select the Advanced Tab, and scroll down to "Java (Sun)"
  3. Check the box next to the Java version
  4. Next, select the Security Tab, and select the "Custom Level" button
  5. Scroll down to "Scripting of Java applets"
  6. Make sure the "Enable" radio button is checked.
  7. Click OK to save your preference.
Mozilla
  1. From the menu bar, choose Edit --> Preferences
  2. Select the Advanced category
  3. Check the box labeled "Enable Java"
  4. Click OK to save your preference.

Jan 30, 2010 | Dell Microsoft Windows XP Home Edition

1 Answer

Try to play pogo games


Please follow these instrucions to clean up your Java installation.
There is a program called JavaRa that will remove all the old Java versions from your computer. Go here to read about it, then it is a free download from here.
Extract the .zip file to your desktop or create a My Programs\JavaRa folder and send it there. Close your browser and run the JavaRa.exe file and choose 'remove older versions.' When done, it will show you a log file of the Java versions found on your computer and removed. Close that.
Now open JavaRa again if it is not still there behind the log file. Choose "Search for updates" and then "Update using SunJava's website" A browser window should open at the Java home page. (http://java.sun.com/javase/downloads/index.jsp)
Scroll down to the Java SE Runtime Environment (JRE) and choose download. Choose windows from the options, check the license, then choose offline or online installation and click the orange download bar.
Save the file to your desktop or folder, then close your browsers, and click on the file to install Java.
This will give you the most recent version of Java available, which should work seamlessly with Pogo applications.
To check, go to this page and reactivate your Pogo account. Then try to play games.
If your account is properly activated, and you're still having problems, please see this page.
If you want help with these last settings, write back and let us know the exact error message you get from Pogo, and we'll help you to adjust your browser settings to get you in.
I don't see any reason why you shouldn't be able to play with Windows xp. I'm sure you can get it sorted out...

I hope this information allows you to resolve this issue. If you need further assistance, please post back with a comment to this thread.
If I've managed to answer your question or solve a problem, please take just a moment to rate this post....thanks!

Aug 23, 2009 | Sun Java Desktop System

1 Answer

Play Youtube


If you have no java runtime environment or have not been updated, you can update java and try this link.http://java.com/en/download/index.jsp. And I have flash player 8 which was downloaded together with the macromedia dreamweaver. You can search that in google and request for a dwonload.
Blessings.

Jun 23, 2009 | Microsoft Windows XP Professional

1 Answer

Playing Pogo games


Windows 98!! Yikes, thats old. and must take you ages to load,however try manually installing the Java applet from java.com and see if that works for you.

Go to java.com and download using the yellow pdate java btton on the main page.

Apr 18, 2009 | Sun Java Desktop System

2 Answers

Hi i have a problem with my java working for runescape...it lets me play the game for like 5mins then the game dissappears and the screen is black and my java icon on the bottom right corner (next to the...


awesome, someone else plays runescape also!. just uninstall your current version of java, then go to the java website and reinstall. should fix this issue. im level 93 by the way!

Mar 13, 2009 | Sun Java Desktop System

2 Answers

Vista doesnt play video clip files


download vlc media player it plays almost all video and audio codecs..

Dec 03, 2008 | Microsoft Windows XP Home Edition

Not finding what you are looking for?
Computers & Internet Logo

Related Topics:

288 people viewed this question

Ask a Question

Usually answered in minutes!

Top Computers & Internet Experts

Brian Sullivan
Brian Sullivan

Level 3 Expert

27725 Answers

kakima

Level 3 Expert

98767 Answers

David Payne
David Payne

Level 3 Expert

14065 Answers

Are you a Computer and Internet Expert? Answer questions, earn points and help others

Answer questions

Manuals & User Guides

Loading...