50867

Cannot load sound later during runtime

Question:

Hello I have created little class to play sounds in my game. Here:

package sk.tuke.oop.game.sounds;

import java.applet.AudioClip; import java.io.File; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.sound.sampled.FloatControl; public class Sound implements AudioClip { private Clip clip; private FloatControl volume; private int framePosition; public Sound(String path) { loadMusic(path); } public void loadMusic(String path) { if (clip != null) clip.stop(); clip = null; if (!path.equals("")) { File soundFile = null; try { soundFile = new File(path); } catch (Exception e) { return; } try { AudioInputStream input = AudioSystem.getAudioInputStream(soundFile); clip = AudioSystem.getClip(); clip.open(input); } catch (Exception e) { e.printStackTrace(); clip = null; } } } public void play() { if (clip != null) { stop(); clip.start(); } } public void stop() { if (clip != null) { clip.stop(); clip.setFramePosition(0); } } public void pause() { if (clip != null) { if (clip.isRunning()) { framePosition = clip.getFramePosition(); clip.stop(); } } } public void unpause() { if (clip != null) { if (!clip.isRunning()) { clip.setFramePosition(framePosition); clip.start(); } } } public void loop() { if (clip != null) { clip.loop(clip.LOOP_CONTINUOUSLY); } } public void setVolume(float vol) { if (volume.getMinimum()+ vol <= volume.getMaximum()) { volume.setValue(volume.getMinimum()); volume.setValue(volume.getValue() + vol); } } }

It's working fine when all actors are created before game loop but when I shoot a bullet and I want to play a sound i get:

javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported.

Could you help me with that? Thank you.

Answer1:

if the sound file is not in mp3 format, then try to converted in mp3. Also make sure that the file name is in lowercase letters.

Recommend

  • Load MovieClip inline in TextField
  • Flowplayer playing over everything
  • SqlDependency vs SQLCLR call to WebService
  • How to create a jQuery price slider
  • Audio playback in java not working correctly
  • MonadTransControl instance for ProxyFast/ProxyCorrect
  • Disabling sound of embedded flash object with html
  • Leaflet z-index
  • Client side validation mvc dropdown
  • event.getSource() returns null Accessibility in android
  • Why isn't my “Fizz Buzz” test in R working?
  • Java making confirming exit
  • Implement JwtBearer Authentication in NSwag SwaggerUi
  • Angular2 - Template reference inside NgSwitch
  • abstracting over a collection
  • How to explicitly/implicitly implemented interface members in C++/CLI?
  • How to programatically 'login' a user based on 'remember me' cookie when using j
  • android google indoor map
  • Insert new calendar with SyncAdapter- Calendar API Android
  • Django simple Captcha “No module named fields” error
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • C# - Serializing and deserializing static member
  • How do i call multiple functions using if statements that have multiple parameters in C++
  • Bug in WPF DataGrid
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to set my toolbar fixed while scrolling android
  • Javascript + PHP Encryption with pidCrypt
  • AT Commands to Send SMS not working in Windows 8.1
  • How to disable jQuery.jplayer autoplay?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • apache spark aggregate function using min value
  • unknown Exception android
  • Sorting a 2D array using the second column C++
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?