PreviousArticleThis article describes how to add and play sound effects.
Sound Effect instance
Call the play method of the soundeffect object to provide a simple method for playing the sound. In many cases, it will be sufficient. For gunshots, explosions, successful player sounds, and other effects, this is probably what you need.
However, in other places, you may find that a high level of control is required to surpass the sound you play. The ability to play audio cyclically, for example, is very useful, but this capability is not available only by the functions provided by the soundeffect class. The main reason is that we will not be able to block the sound; we cannot get a sound ID or any similar value, so once some sound is activated, we won't be able to tell the class which one should stop playing.
This problem can be solved in the soundeffectinstance class. Create an instance by calling the createinstance method of the soundeffect object.
The soundeffectinstance object is very similar to the soundeffect object, but there are several key differences:
1) each instance can only play one sound within one time period. Multiple calls to play will not work. To play multiple sounds at the same time, you need to create multiple instance object instance objects from the underlying sound effects when using sound effects instances.
2) the volume, pitch, and motion of an object are specified by class attributes instead of parameters of the play method. After the sound starts playing, you can also modify its attributes.
3) The soundeffectinstance object contains an islooped attribute, which notifies loop playback when it is set to true.
4) In addition to the play method, the effect instance also provides a method to pause and stop the playing sound. They do not need to be used when the sound is not played cyclically, but if it loops through one or another, it will be very useful to stop the playing sound. Pause will remember the progress of the played sound and resume and play from the current point before the next playback. To check whether the audio instance is stopped, paused, or playing, you can query its state attribute.
In addition, if you slide your fingers left and right when playing a sound, the pitch of the sound will change immediately. Again, only soundeffectinstance allows this change, because only the soundeffect object allows you to set the pitch when the sound starts playback first (the volume and the shift are the same)
BelowCodeDescribe playing, retuning, and pausing a sound.
Touchcollection Tc = touchpanel. getstate ();
If(TC. Count> 0)
{
// Find the region of the screen that has been touched
Screenregion = (Int) (TC [0]. position. y * 4/window. clientbounds. Height );
// Ensure we have a region between 0 and 3
If(Screenregion> = 0 & screenregion <= 3)
{
// What type of touch event do we have?
Switch(TC [0]. State)
{
CaseTouchlocationstate. Pressed:
// Set the pitch based on the horizontal touch position
_ Soundinstances [screenregion]. Pitch = (TC [0]. position. X/This. Window. clientbounds. width) * 2-1;
// Start the sound for this region
_ Soundinstances [screenregion]. Play ();
Break;
CaseTouchlocationstate. Moved:
// Is the sound for this region currently playing?
If(_ Soundinstances [screenregion]. State = soundstate. Playing)
{
// Yes, so set the pitch based on the horizontal touch position
_ Soundinstances [screenregion]. Pitch = (TC [0]. position. X/This. Window. clientbounds. width) * 2-1;
}
Break;
CaseTouchlocationstate. Released:
// Pause all of the sounds
For(IntI = 0; I <_ soundinstances. length; I ++)
{
_ Soundinstances [I]. Pause ();
}
Break;
}
}
}
When the touch point is released, try to change the code so that it can call the stop method of the soundeffectinstance object. method 2 is not the pause method. Then you will see that each time the sound is played, it is restarted at the start rather than recovered from the last interruption.
Other sound Properties
Let's take a brief look at the two attributes exposed through other attributes of the soundeffect class.
The first is the Duration Attribute. It returns a timespan, which correctly indicates how long the sample will be played in its default audio High School. This attribute is useful from one sound to the next sound.
The second is the mastervolume attribute, which is indeed a static attribute called from the soundeffect class rather than from an instantiated object. It controls the total volume of all subsequent or ongoing audio effects, so it is a good way to increase or decrease the volume.
Like a separate sound volume, mastervolume ranges from 0 (silent) to 1 (maximum volume, based on the maximum volume of the current device. The default value is 1.