Skip to content

Commit 2725fec

Browse files
更新播放网络文件的实现逻辑,以符合规范要求 (#30)
* 更新播放网络文件的实现逻辑,以符合规范要求 * 难绷注释 * 轻度改造 --------- Co-authored-by: 永安404 <101850798+YongAn404@users.noreply.github.com>
1 parent e0e4615 commit 2725fec

File tree

1 file changed

+71
-4
lines changed

1 file changed

+71
-4
lines changed

Roles/MusicManager.cs

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
using SCPSLAudioApi.AudioCore;
55
using System;
66
using System.Collections.Generic;
7+
using System.IO;
78
using System.Linq;
9+
using System.Net.Http;
10+
using System.Threading.Tasks;
811
using UnityEngine;
912
using YongAnFrame.Players;
1013
using static SCPSLAudioApi.AudioCore.AudioPlayerBase;
@@ -172,10 +175,74 @@ public AudioPlayerBase Play(string musicFile, string npcName, TrackEvent? trackE
172175
}
173176
return audioPlayerBase;
174177
}
175-
176-
public readonly struct TrackEvent(TrackLoaded trackLoaded)
178+
179+
/// <summary>
180+
/// 播放音频(Url)
181+
/// </summary>
182+
/// <param name="musicUrl">音频文件</param>
183+
/// <param name="npcName">NPC名称</param>
184+
/// <param name="trackEvent">播放事件(可null)</param>
185+
/// <param name="source">传播距离检测源头玩家(可null,null时是NPC)</param>
186+
/// <param name="distance">传播距离(-1时是全部玩家,0时是源头玩家)</param>
187+
/// <param name="extraPlay">额外可接收音频的玩家(可null)</param>
188+
/// <param name="isSole">[弃用]是否覆盖播放</param>
189+
/// <param name="volume">音量大小</param>
190+
/// <param name="isLoop">是否循环</param>
191+
/// <returns></returns>
192+
public AudioPlayerBase PlayUrl(string musicUrl, string npcName, TrackEvent? trackEvent, FramePlayer source, float distance, FramePlayer[] extraPlay, bool isSole = false, float volume = 80, bool isLoop = false)
177193
{
178-
public TrackLoaded TrackLoaded { get; } = trackLoaded;
194+
AudioPlayerBase audioPlayerBase = null;
195+
try
196+
{
197+
if (trackEvent.HasValue)
198+
{
199+
OnTrackLoaded += trackEvent.Value.TrackLoaded;
200+
}
201+
202+
ReferenceHub npc = CreateMusicNpc(npcName);
203+
audioPlayerBase = Get(npc);
204+
205+
if (distance != -1)
206+
{
207+
if (source != null)
208+
{
209+
if (distance == 0)
210+
{
211+
audioPlayerBase.BroadcastTo.Add(npc.PlayerId);
212+
}
213+
else
214+
{
215+
audioPlayerBase.BroadcastTo = FramePlayer.List.Where(p => Vector3.Distance(p.ExPlayer.Position, source.ExPlayer.Position) <= distance).Select((s) => s.ExPlayer.Id).ToList();
216+
}
217+
}
218+
219+
if (extraPlay != null)
220+
{
221+
foreach (var player in extraPlay)
222+
{
223+
if (!audioPlayerBase.BroadcastTo.Contains(player.ExPlayer.Id))
224+
{
225+
audioPlayerBase.BroadcastTo.Add(player.ExPlayer.Id);
226+
}
227+
}
228+
}
229+
}
230+
231+
audioPlayerBase.CurrentPlay = musicUrl;
232+
audioPlayerBase.Volume = volume;
233+
audioPlayerBase.Loop = isLoop;
234+
audioPlayerBase.AllowUrl = true;
235+
audioPlayerBase.Play(-1);
236+
}
237+
catch (Exception)
238+
{
239+
Stop(audioPlayerBase);
240+
}
241+
return audioPlayerBase;
179242
}
180243
}
181-
}
244+
public readonly struct TrackEvent(TrackLoaded trackLoaded)
245+
{
246+
public TrackLoaded TrackLoaded { get; } = trackLoaded;
247+
}
248+
}

0 commit comments

Comments
 (0)