我无法通过单击一次按钮来找出播放和暂停音频源的正确代码。
这是我的代码
ButtonAction.GetComponent().onClick.AddListener(delegate
{
if (soundTarget.isPlaying)
{
soundTarget.Pause();
}
else if(!soundTarget.isPlaying)
{
soundTarget.Play();
playSound("sounds/English");
}
});
注意:默认情况下它不会播放,因为如果我默认情况下播放,它只会循环播放。它可以正常工作,但我需要多次单击按钮才能播放或暂停。
救救我。
答案 0 :(得分:2)
这很简单:
您的Unity场景应该是这样的:
对于我称为“ UnityUI”的脚本,如下所示:
public AudioSource MyAudioSource;
public Button PlayBtn;
public PlayButtonText;
void Start()
{
PlayBtn.onClick.AddListener(() =>
{
if (MyAudioSource.isPlaying)
{
MyAudioSource.Stop();
PlayButtonText.text = "Play me!";
}
else
{
MyAudioSource.Play();
PlayButtonText.text = "Stop me!";
}
});
}
希望这对您有所帮助!
编码愉快!
答案 1 :(得分:0)
我的代码应该是这样的。单击该按钮时,它将播放声音,再次单击时,它将暂停。
+
答案 2 :(得分:0)
void Update()
{
StateManager sm = TrackerManager.Instance.GetStateManager();
IEnumerable<TrackableBehaviour> tbs = sm.GetActiveTrackableBehaviours();
foreach (TrackableBehaviour tb in tbs)
{
string name = tb.TrackableName;
ImageTarget it = tb.Trackable as ImageTarget;
Vector2 size = it.GetSize();
Debug.Log("Active image target:" + name + " -size: " + size.x + ", "
+ size.y);
ButtonAction.gameObject.SetActive(true);
TextDescription.gameObject.SetActive(true);
PanelDescription.gameObject.SetActive(true);
ButtonMute.gameObject.SetActive(true);
if (name == "quezon")
{
ButtonAction.GetComponent<Button>().onClick.AddListener(delegate
{
if (soundTarget.isPlaying)
{
soundTarget.Pause();
btn.image.overrideSprite = Play;
}
else
{
btn.image.overrideSprite = Pause;
playSound("sounds/English");
soundTarget.Play();
}
});
TextDescription.GetComponent<Text>().text = "Manuel L. Quezon was
born on August 19, 1878, and died on August 1, 1944. He was a
Filipino statesman, soldier, and politician who served as president
of the Commonwealth of the Philippines from 1935 to 1944.";
Narrator.gameObject.SetActive(true);
void playSound(string ss)
{
clipTarget = (AudioClip)Resources.Load(ss);
soundTarget.clip = clipTarget;
soundTarget.loop = false;
soundTarget.playOnAwake = false;
soundTarget.ignoreListenerPause = true;
}