Files
ichni_Official/Assets/Scripts/Wwise/SongPlayer.cs
SoulliesOfficial 6aa498f6be
2025-08-11 14:04:06 -04:00

154 lines
4.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using Event = AK.Wwise.Event;
namespace Ichni
{
public class SongPlayer : SerializedMonoBehaviour
{
public AudioManager audioManager;
public Event PlayMusicEvent; // 播放背景音乐的事件
public Event ResumeMusicEvent; // 恢复播放背景音乐的事件
public Event PauseMusicEvent; // 暂停播放背景音乐的事件
public Event StopMusicEvent; // 停止播放背景音乐的事件
private uint _playingId;
public float songTimeSegment = 0;
public float pauseTimeSegment;
private float duration;
private float recordedSongSeg;
public float judgeOffset = 0;
private void Update()
{
if (audioManager.isLoading)
{
return;
}
if (audioManager.isDelaying)
{
songTimeSegment += Time.deltaTime;
//songTimeSegment = Mathf.Max(songTimeSegment, 0); // 确保时间段不为负数
if (songTimeSegment >= 0)
{
audioManager.isDelaying = false;
songTimeSegment = 0; // 延迟结束后,时间段归零
GameManager.instance.audioManager.songPlayer.PlaySong();
}
}
else
{
if (GameManager.instance.isDebugging)
{
return;
}
if (audioManager.isFinished)
{
songTimeSegment = recordedSongSeg;
return;
}
if (audioManager.isPlaying)
{
songTimeSegment = PlaySegment() / 1000f - (judgeOffset / 1000f);
if (songTimeSegment > 0)
{
recordedSongSeg = songTimeSegment;
}
/*
else if (songTimeSegment == 0 && recordedSongSeg > 0)
{
audioManager.isFinished = true;
songTimeSegment = recordedSongSeg;
}*/
//Debug.Log($"Song Time Segment: {songTimeSegment}, Recorded Segment: {recordedSongSeg}");
}
else if (audioManager.isPausing)
{
songTimeSegment = pauseTimeSegment;
}
}
}
[Button]
public void PlaySong()
{
_playingId = PlayMusicEvent.Post(gameObject,
(uint)AkCallbackType.AK_EnableGetMusicPlayPosition |
(uint)AkCallbackType.AK_MusicSyncEntry |
(uint)AkCallbackType.AK_MusicSyncExit,
OnMusicEvent, null);
audioManager.isPlaying = true;
audioManager.isPausing = false;
audioManager.isFinished = false;
}
[Button]
public void PauseSong()
{
pauseTimeSegment = PlaySegment() / 1000f - (judgeOffset / 1000f);
audioManager.isPlaying = false;
audioManager.isPausing = true;
PauseMusicEvent.Post(gameObject);
Time.timeScale = 0;
}
[Button]
public void ResumeSong()
{
Time.timeScale = 1;
audioManager.isPlaying = true;
audioManager.isPausing = false;
ResumeMusicEvent.Post(gameObject);
}
[Button]
public void StopSong()
{
audioManager.isPlaying = false;
audioManager.isPausing = false;
StopMusicEvent.Post(gameObject);
}
private void OnMusicEvent(object in_cookie, AkCallbackType in_type, AkCallbackInfo in_info)
{
Debug.Log(in_type + " " + in_info);
if (in_type == AkCallbackType.AK_MusicSyncEntry)
{
if (in_info is AkMusicSyncCallbackInfo musicInfo)
{
InformationTransistor.instance.songLength = musicInfo.segmentInfo_iActiveDuration / 1000f;
InformationTransistor.instance.bpm = GameManager.instance.songInformation.bpm;
}
}
if (in_type == AkCallbackType.AK_MusicSyncExit)
{
audioManager.isFinished = true;
GameManager.instance.summaryPageCanvas.SetUpSummary();
GameManager.instance.summaryPageCanvas.FadeIn();
}
}
int PlaySegment()
{
AkSegmentInfo segmentInfo = new AkSegmentInfo();
AkSoundEngine.GetPlayingSegmentInfo(_playingId, segmentInfo,true);
return segmentInfo.iCurrentPosition;
}
}
}