goglev.blogg.se

Play sound
Play sound









play sound
  1. #Play sound how to#
  2. #Play sound update#

It's not a buffer into which you can subsequently load more data, as you can with a SourceDataLine's buffer.Īfter opening the clip, you can specify at what point in the data it should start playback, using Clip's setFramePosition or setMicroSecondPosition methods. The bufferSize argument here just specifies how much of the byte array to load into the clip. Void open(AudioFormat format, byte data, int offset, int bufferSize)ĭespite the bufferSize argument in the second open method above, Clip (unlike SourceDataLine) includes no methods for writing new data to the buffer. To actually use the clip, you need to reserve it for your program's exclusive use by invoking one of the following Clip methods: Because a mixer might have a limited number of lines of the desired type available, it can happen that after you invoke getLine to obtain the clip, another application program jumps in and grabs the clip before you're ready to start playback. Obtaining a line just means you've gotten a way to refer to it getLine doesn't actually reserve the line for you. Getting a Line of a Desired Type Construct a DataLine.Info object with Clip.class for the first argument, and pass this DataLine.Info as an argument to the getLine method of AudioSystem or Mixer. You obtain a Clip as described earlier under

#Play sound update#

The dynamic nature of the sound transformation requires the application program to update the sound data continuously during playback, instead of supplying it all before playback starts. For example, imagine a game that gives aural feedback by "morphing" from one sound to another as the user moves the mouse. Another example of sound that can't be known in advance occurs when you synthesize or manipulate the sound data interactively in response to the user's input.

play sound

In this case, a SourceDataLine is more appropriate than a Clip. If you don't have a mixer that can send input audio right back out an output port, your application program will have to take the captured data and send it to an audio-output mixer.

play sound

  • Use a SourceDataLine for streaming data, such as a long sound file that won't all fit in memory at once, or a sound whose data can't be known in advance of playback.Īs an example of the latter case, suppose you're monitoring sound input-that is, playing sound back as it's being captured.
  • In other words, because the sound is preloaded into a clip, playback can start immediately instead of having to wait for the buffer to be filled. Finally, playback from a Clip generally has less latency than buffered playback from a SourceDataLine. If you need to start the playback at an arbitrary position in the sound, the Clip interface provides a method to do that easily. If you want the sound to play back more than once, a Clip is more convenient than a SourceDataLine, especially if you want the playback to loop (cycle repeatedly through all or part of the sound).
  • Use a Clip when you have non-real-time sound data that can be preloaded into memory.įor example, you might read a short sound file into a clip.
  • play sound

    Although there are many situations in which you could use either a Clip or a SourceDataLine, the following criteria help identify which kind of line is better suited for a particular situation: The primary difference between the two is that with a Clip you specify all the sound data at one time, before playback, whereas with a SourceDataLine you keep writing new buffers of data continuously during playback.

    #Play sound how to#

    Here you will learn how to play sound through a line.Īs you know, there are two kinds of line that you can use for playing sound: a The Java Sound API is designed to help application programs play sounds smoothly and continuously, even very long sounds.Įarlier you saw how to obtain a line from the audio system or from a mixer. With sound even more than video, it's important that the rate of data flow be maintained, because interruptions to sound playback often produce loud clicks or irritating distortion. If the data is time-based, as sound is, it must be delivered at the correct rate. The essential feature is that a sequence of data is delivered somewhere for eventual perception by a user. These are general terms that are applicable to other kinds of media besides sound. Playback is sometimes referred to as presentation or rendering.











    Play sound