如何在播放另一个音频时停止一个音频?
public AudioSource audioSource;
public AudioClip audioClip;
private string[,] levelWords;
//public static string glupiaZmienna;
public Text txt;
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log(gameObject.name);
WordsLoader w = new WordsLoader();
levelWords = w.wLevelWords;
SetCategoryButton scb = new SetCategoryButton();
int a = scb.Kategoria;
zupa();
AudioSource audio = gameObject.AddComponent<AudioSource>();
audio.Stop();
audio.clip = (AudioClip)Resources.Load(a+ "/" + WordsLoader.language + "/Sounds/" + gameObject.name);
audio.Play();
// txt = gameObject.GetComponent<Text>();
}
public void zupa()
{ WordsLoader w = new WordsLoader();
SetCategoryButton scb = new SetCategoryButton();
int a = scb.Kategoria;
print(w.wHowManyWords);
for (int i = 0; i < w.wHowManyWords; i++)
{
print("jestem tu");
if (levelWords[i, 0] == gameObject.name)
{
txt = GameObject.Find("Text").GetComponent<Text>(); ;
txt.text = levelWords[i, 2];
}
}
}
此代码位于脚本中,单击按钮后会开始播放声音。我想先停止音频。
答案 0 :(得分:2)
编辑:由于其下留下的评论,答案必须改变很多。覆盖所有内容是有意义的。
您需要一个指向所有AudioSource
的引用。将每个ClickAction
实例存储在List
中的另一个脚本(PlayerManager
)中。在播放音频之前,循环播放List
并在播放时停止播放。之后,您可以播放音频。
此外,您每次要播放音频时都会调用Resources.Load
。这在性能方面并不好。您应该在Start()
函数中执行此操作一次,然后在需要时播放它。此外,您的gameObject.AddComponent<AudioSource>()
代码也应该只调用一次。
您的新ClickAction
脚本:
public class ClickAction : MonoBehaviour
{
public AudioSource audioSource;
public AudioClip audioClip;
private string[,] levelWords;
//public static string glupiaZmienna;
public Text txt;
public AudioSource clickAudioSource;
PlayerManager playerManager = null;
void Start()
{
//Get PlayerManager from PlayerManager GameObject
playerManager = GameObject.Find("PlayerManager").GetComponent<PlayerManager>();
clickAudioSource = gameObject.AddComponent<AudioSource>();
clickAudioSource.clip = (AudioClip)Resources.Load(a + "/" + WordsLoader.language + "/Sounds/" + gameObject.name);
//Register this Script to the PlayerManager
playerManager.addClickAction(this);
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log(gameObject.name);
WordsLoader w = new WordsLoader();
levelWords = w.wLevelWords;
SetCategoryButton scb = new SetCategoryButton();
int a = scb.Kategoria;
zupa();
//Play Audio
playAudio();
// txt = gameObject.GetComponent<Text>();
}
private void playAudio()
{
playerManager.playAudio(clickAudioSource);
}
public void stopAudio()
{
//If not null and playing then stop audio
if (clickAudioSource != null && clickAudioSource.isPlaying)
{
clickAudioSource.Stop();
}
}
public void zupa()
{
WordsLoader w = new WordsLoader();
SetCategoryButton scb = new SetCategoryButton();
int a = scb.Kategoria;
print(w.wHowManyWords);
for (int i = 0; i < w.wHowManyWords; i++)
{
print("jestem tu");
if (levelWords[i, 0] == gameObject.name)
{
txt = GameObject.Find("Text").GetComponent<Text>(); ;
txt.text = levelWords[i, 2];
}
}
}
void OnDisable()
{
//Un-Register this Script from the PlayerManager when it is destroyed
playerManager.removeClickAction(this);
}
}
PlayerManager
脚本。创建一个名为PlayerManager
的GameObject,然后将下面的脚本附加到它:
public class PlayerManager : MonoBehaviour
{
List<ClickAction> clickActions = new List<ClickAction>();
public void addClickAction(ClickAction clickActionToAdd)
{
//Adds clickActionToAdd if it does not exist in the List already
if (!clickActions.Contains(clickActionToAdd))
{
clickActions.Add(clickActionToAdd);
}
}
public void removeClickAction(ClickAction clickActionToAdd)
{
//Removes clickActionToAdd if it exist in the List
if (clickActions.Contains(clickActionToAdd))
{
clickActions.Add(clickActionToAdd);
}
}
public bool clickActionExist(ClickAction clickActionToAdd)
{
//Removes clickActionToAdd if it exist in the List
return clickActions.Contains(clickActionToAdd);
}
public void playAudio(AudioSource audSource)
{
//Stop Other Audio
stopAllClickActionAudio();
//Now, play the one that was passed in
audSource.Play();
}
void stopAllClickActionAudio()
{
//Stop Audio on every ClickAction script
for (int i = 0; i < clickActions.Count; i++)
{
clickActions[i].stopAudio();
}
}
}
当调用playAudio
函数时,它会停止存储在AudioSource
中的List
的所有音频,然后它将播放通过的当前AudioSource
进入它。