• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sound Player Using javax.sound.sampled.*, Getting NullPointerException  RSS feed

 
Patrick Mala
Greenhorn
Posts: 9
Android Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am making a project that should play, stop, and loop three different songs (not at the same time).
I am calling this SoundPlayer class from my main class:



When my class calls setPlayType(2), which should make it play, I get the following runtime error:

Line 32 is if(clip.isOpen()), but I'm pretty sure I instantiated clip properly in the constructor. Anyone have any hints?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure I instantiated clip properly in the constructor.

Probably, but there is still a problem in line 17.
 
Patrick Mala
Greenhorn
Posts: 9
Android Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
I'm pretty sure I instantiated clip properly in the constructor.

Probably, but there is still a problem in line 17.


Ah thanks, I had copied that from a test class and forgot to move the clip. That fixed the runtime error, however I've run into another problem. Everything seems to work fine, however when I call stopSong(), or call playSong() or loopSong() a second time, it won't play again. However, if I setSong() to a different song, it will play normally (until it ends or I stop it, than that song won't work either.) Here are my revised versions of the three methods :


I added setFramePosition(0) to make sure that it would rewind properly, but it's not playing anything at all (after stopping the first time). Anyone know a better way of switching between different sounds?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never used the audio system but I think you will need to reload the AudioInputStream. Either that or if the stream supports mark and reset, mark the stream before playing and then reset it after stopping so the stream position is set back to the beginning.
 
Patrick Mala
Greenhorn
Posts: 9
Android Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:I've never used the audio system but I think you will need to reload the AudioInputStream.

I reloaded the audioinputstream and it's working perfectly now, thanks! (I tried marking/resetting but my audioinputstream was not supported).
Here's my final code, I know I could have found a way to just reload the sound I needed but, as long it works its good for my purposes.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simplest way is probably to have your song list store the file names of the songs rather than the AudioStreams and then create the AudioStream for the currently selected song each time you want to play or loop the song.
 
Patrick Mala
Greenhorn
Posts: 9
Android Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah thanks, yes that seems easiest. I changed my list to string type and added a loadSong(String s) method and it works great.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!