473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Embedding music in Java

tiktik
14
Hey there,

I was doing a simple program using JCreator where I wanted to embed a song. However I don't know how I can do this, and although I browsed thoroughly through the web, I did not find any examples on how one can embed some music in his program...

Any idea on how I can embed a song from My Documents (or elsewhere) into my program?

Any help would be much appreciated.

Regards,
Tiktik
Jun 22 '09 #1
5 4318
JosAH
11,448 Expert 8TB
@tiktik

There's a method newAudioClip() in the Applet class; no need to worry because it's a static method and no Applet is needed; it could've had a better place but there are more examples like that. Given a clip you can play() it, stop() it and loop() it over and over again.

kind regards,

Jos
Jun 22 '09 #2
tiktik
14
@JosAH
Thanks a lot,

I've found an example as well an did this program, however there is one problem.

Expand|Select|Wrap|Line Numbers
  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.net.URL;
  4.  
  5. public class Music_1 {
  6.   public static void main(String[] args) {
  7.     try {
  8.       URL url = new URL("file:Zocalo.wav");
  9.       AudioClip ac = Applet.newAudioClip(url);
  10.       ac.play();
  11.  
  12.       System.out.println("Press any key to exit.");
  13.       System.in.read();
  14.       ac.stop();
  15.     } catch (Exception e) {
  16.       System.out.println(e);
  17.     }
  18.   }
  19. }
  20.  

It compiles and runs, without any errors. But the music does not come out. Where should I put the music file which i want to play (in which folder)?

kind regards,
tiktik
Jun 22 '09 #3
JosAH
11,448 Expert 8TB
@tiktik
Don't rely on an artifact that your (sound) resource is stored in a file (it might be stored in a .jar file). For now store the sound file where your class file is stored and find it relative to that class by doing:

Expand|Select|Wrap|Line Numbers
  1. URL yourClipURL= YourClass.class.getResource("yourClipResource");
  2. AudioClip yourClip= Applet.newAudioClip(yourClipURL);
  3.  
kind regards,

Jos
Jun 22 '09 #4
tiktik
14
@JosAH

Thanks, I done just that, and the resource file is indeed getting accessed now... however still no music comes out. I find it very strange seeing that the sound file is being accessed, yet still no sound is heard. Is the Windows Media Player supposed to open upon access of the resource file?

thanks a lot,
tiktik
Jun 23 '09 #5
JosAH
11,448 Expert 8TB
@tiktik
When you print out the URL, does it really point to your sound clip? Can you play sounds on your computer at all? The volume is down mayhap?

kind regards,

Jos (just guessing)
Jun 23 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Elaine Jackson | last post by:
Is there such a thing as an executable language for music? If not, why not? And if yes, where can I find it? Naturally the ideal thing would be if such a language were implemented within python,...
13
by: Rolf Magnus | last post by:
Hi, I would like to embed a python interpreter within a program, but since that program would be able to automatically download scripts from the internet, I'd like to run those in a restricted...
3
by: Jim Hargrave | last post by:
I've read that it is possible to compile jython to native code using GCJ. PyLucene uses this approach, they then use SWIG to create a Python wrapper around the natively compiled (java) Lucene. Has...
56
by: alan b | last post by:
I copied it and tried to edit the movie.htm below in the <PARAM NAME=*** location of the file where the videoclip is on my hard drive. It is already recorded by Windows Media Player. My version...
6
by: Edward | last post by:
I have been doing some research about embedding images in HTML using the data URL src method of the format: <img src="/-/data:image/gif;base64,<DATA>"> My question is, how does one generate...
2
by: Omar | last post by:
I've been learning some programming during my little sabbatical (mostly python), and I thought it'd be cool to see if other people are interested in programming for music theory. So I started a...
1
by: MissMarie | last post by:
I've been playing around with DIV tables in myspace to better learn how to rewrite my own code for my business site without having to pay someone to design it. I've tried embedding a slideshow into...
5
by: Anil Gupte | last post by:
How do I embed Windows Media Player so I can control it with VB code. I am familiar with embedding in a web page like this: *************** <embed src="Test.asx" width=320 height=300...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.