Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新播放网络文件的实现逻辑,以符合规范要求 #30

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 71 additions & 4 deletions Roles/MusicManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
using SCPSLAudioApi.AudioCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using UnityEngine;
using YongAnFrame.Players;
using static SCPSLAudioApi.AudioCore.AudioPlayerBase;
Expand Down Expand Up @@ -172,10 +175,74 @@ public AudioPlayerBase Play(string musicFile, string npcName, TrackEvent? trackE
}
return audioPlayerBase;
}

public readonly struct TrackEvent(TrackLoaded trackLoaded)

/// <summary>
/// 播放音频(Url)
/// </summary>
/// <param name="musicUrl">音频文件</param>
/// <param name="npcName">NPC名称</param>
/// <param name="trackEvent">播放事件(可null)</param>
/// <param name="source">传播距离检测源头玩家(可null,null时是NPC)</param>
/// <param name="distance">传播距离(-1时是全部玩家,0时是源头玩家)</param>
/// <param name="extraPlay">额外可接收音频的玩家(可null)</param>
/// <param name="isSole">[弃用]是否覆盖播放</param>
/// <param name="volume">音量大小</param>
/// <param name="isLoop">是否循环</param>
/// <returns></returns>
public AudioPlayerBase PlayUrl(string musicUrl, string npcName, TrackEvent? trackEvent, FramePlayer source, float distance, FramePlayer[] extraPlay, bool isSole = false, float volume = 80, bool isLoop = false)
{
public TrackLoaded TrackLoaded { get; } = trackLoaded;
AudioPlayerBase audioPlayerBase = null;
try
{
if (trackEvent.HasValue)
{
OnTrackLoaded += trackEvent.Value.TrackLoaded;
}

ReferenceHub npc = CreateMusicNpc(npcName);
audioPlayerBase = Get(npc);

if (distance != -1)
{
if (source != null)
{
if (distance == 0)
{
audioPlayerBase.BroadcastTo.Add(npc.PlayerId);
}
else
{
audioPlayerBase.BroadcastTo = FramePlayer.List.Where(p => Vector3.Distance(p.ExPlayer.Position, source.ExPlayer.Position) <= distance).Select((s) => s.ExPlayer.Id).ToList();
}
}

if (extraPlay != null)
{
foreach (var player in extraPlay)
{
if (!audioPlayerBase.BroadcastTo.Contains(player.ExPlayer.Id))
{
audioPlayerBase.BroadcastTo.Add(player.ExPlayer.Id);
}
}
}
}

audioPlayerBase.CurrentPlay = musicUrl;
audioPlayerBase.Volume = volume;
audioPlayerBase.Loop = isLoop;
audioPlayerBase.AllowUrl = true;
audioPlayerBase.Play(-1);
}
catch (Exception)
{
Stop(audioPlayerBase);
}
return audioPlayerBase;
}
}
}
public readonly struct TrackEvent(TrackLoaded trackLoaded)
{
public TrackLoaded TrackLoaded { get; } = trackLoaded;
}
}
Loading