{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Games4 - Sound Effects and Music Chapter 4 of Developing...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Sound Effects and Music Chapter 4 of Developing Games in Java Friday, October 28, 2011
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Opening sounds files Use the AudioSystem class getAudioStream methods used to get an AudioInputStream object from a file File file = new File("sound.wav"); AudioInputStream stream = AudioSystem.getAudioInputStreamFile(file); AudioFormat format = stream.getFormat( ); AudioFormat class provides a way to get info about the stream -- sample rate, number of channels, bytes per sample, etc. Useful for determining memory requirements, i.e. a 1 second mono sound with 16-bit samples, 44,100KHz would be 44,100 × 1 × 2 = ~88K bytes. Friday, October 28, 2011
Image of page 2
Lines A line is an interface to send audio to the sound sub-system The Line interface supports several sub-interfaces We use a SourceDataLine Allows audio data to be written to the sound system See Chapter 4 of Game Development in Java for a discussion of clips. Clips are more limited that SourceDateLine s in the number that can be open and the number of sounds they can play at a time Friday, October 28, 2011
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Playing a sound Look at program SimpleSoundPlayer.java Loads sample from AudioInputStream Converted to an InputStream /** Opens a sound from a file. */ public SimpleSoundPlayer(String filename) { try { // open the audio input stream AudioInputStream stream = AudioSystem.getAudioInputStream( new File(filename)); format = stream.getFormat(); // get the audio samples samples = getSamples(stream); } catch (UnsupportedAudioFileException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } samples is a byte array Friday, October 28, 2011
Image of page 4
getSamples /** Gets the samples from an AudioInputStream as an array of bytes.
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern