• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

HashMap Sound

 
Ranch Hand
Posts: 197
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




I found a code on Stack Over Flow, I do not know if it works
I adapt it to my way

but there is always this file error not found like I explained in the Jtable Jprogressbar topic


Exception in thread "main" java.io.FileNotFoundException: 60s rock beat.wav (Le fichier spécifié est introuvable)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153)
at java.desktop/com.sun.media.sound.SunFileReader.getAudioInputStream(SunFileReader.java:117)
at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1062)
at HashMap01.main(HashMap01.java:22)

 
Rancher
Posts: 3232
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What don't you understand about the Exception?

It is telling you the file is not found. We can't verify if you entered the file names correctly.

Did you:

1. try another file?
2. make sure the file exists in the directory where the class file is located?

Note, even if you find the file it won't play the file because:

1. the filename in the HashMap is different than the filename in your get(...) method. Instead of typing the filename twice try using



2. The class will finish executing before the clip is played. You can add a Thread.sleep(1000) statement at the end of your class.
 
Master Rancher
Posts: 3827
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming you've spelled the filenames correctly, most likely there is an issue with what directory you're in.  I suggest replacing this:

with

This should tell you where the file is expected to be found, and you can compare that with where it is actually located.
 
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...as mentioned to you on your other thread.
 
ronald deux
Ranch Hand
Posts: 197
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have typed the names of the files and well placed in the SoundS folder
I always have the same mistake
I have changed file name
 
Master Rancher
Posts: 4250
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is printed for the absolute path of the .wav file when the code is executed?
Is that where the file is located?

BTW the print statement must be immediately after line 21 where the value is assigned to the variable: file.
 
Carey Brown
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Might you want:
 
ronald deux
Ranch Hand
Posts: 197
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I put println on line 21 and adds "sounds \\" + filename
It shows the 3 songs in my folder
but no sound for now
 
Carey Brown
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Rob Camick
Rancher
Posts: 3232
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You last question had 100 replies. This is an indication of a poorly communicated question.

This question is now up to 10 replies. Its time you start to learn to communicate better.

You made the following comment:

but no sound for now



So what does that mean?

You first stated you were getting: Exception in thread "main" java.io.FileNotFoundException: 60s rock beat.wav

Does this mean you have made changes and you no longer get this message?

If so, then you should:

1. tell us the "original" problem has been solved.
2. ask a new question stating your new problem.

This way it keeps the thread concentrated on a single concept.

In any case if you hear no sound, then as I stated in my previous answer you need to add a Thread.sleep(...) otherwise the JVM closes before the wav file can be played. You added your code in the wrong place.

To get the code working first do:

1. forget about the "sounds" folder. Put the .wav files in the same directory as the class file
2. the Thread.sleep(...) needs to be execute AFTER you start the music playing

That is try to solve one problem at a time to keep the logic simple. Once the above two steps work, then you worry about placing the .wav file in a separate folder.

The following example shows how to play 5 second of a song:


Once the above works you move the .wav files to your "sound" directory and then retest.

Something like:


Give you variables meaningful names so you can understand how to use the variable. Why would you have a static variable and local variable both use the name "file"?
 
Carey Brown
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure you can have more than one clip open at a time? When I was playing around with it it wouldn't let me play another one while the resources ("line") was in use until I called clip.close() on the first one.
 
ronald deux
Ranch Hand
Posts: 197
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To answer a rob, yes it works with thread.sleep, was enough to move it
As the song lasts 16 seconds, I made thread.sleep (16000)

And to answer Corey, it's a bit of what I was looking for, a song after another, I just tested your code, it works perfectly well
if I understood correctly, must close the first song to go to the next
I will try to test it with the jprogressbar
 
ronald deux
Ranch Hand
Posts: 197
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A question comes to mind
For sounds, it must be used Hashmap or Hashtable?
what is better?
 
Carey Brown
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the purpose of the HashMap? If you know the file name and the directory then what else do you need to look up? Is the value going to be an instance of a Track object for example? You already have a table in your GUI unless you've abandoned that for the time being.
 
ronald deux
Ranch Hand
Posts: 197
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The goal of hashmap is to have a collection with a key to its value
As it works by key, I thought to make a scanner associated with the key and playing music, it's just an idea
Is this feasible? I do not know
no i did not abandon my jtable
I am looking for ideas to operate each progress bar associated with the duration of the song
 
Carey Brown
Saloon Keeper
Posts: 7999
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Certainly feasible, that's what Maps are for. But what do you envision the key and the value to be?
 
ronald deux
Ranch Hand
Posts: 197
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think put a number one song as a key
I try to reflech to your question
I hope to have answered
 
This is my favorite show. And this is my favorite tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic