Files
2026-02-21 16:45:37 +08:00

441 lines
9.2 KiB
C#

using System;
using Crosstales.Radio.Model;
using Crosstales.Radio.Model.Enum;
using Crosstales.Radio.Util;
using UnityEngine;
using UnityEngine.UI;
namespace Crosstales.Radio.Demo
{
[HelpURLAttribute("https://www.crosstales.com/media/data/assets/radio/api/class_crosstales_1_1_radio_1_1_demo_1_1_g_u_i_play_radio.html")]
public class GUIPlayOwnRadio : MonoBehaviour
{
[Tooltip("'RadioPlayer' from the scene.")]
[Header("Settings")]
public RadioPlayer Player;
[Tooltip("The color for the Play-mode.")]
public Color32 PlayColor = new Color32(0, byte.MaxValue, 0, 64);
[Tooltip("How many times should the radio station restart after an error before giving up (default: 3).")]
public int Retries = 3;
[Header("UI Objects")]
public Button PlayButton;
public Button StopButton;
public Image MainImage;
public Text Station;
public Text ElapsedTime;
public Text ErrorText;
public Text ElapsedRecordTime;
public Text RecordTitle;
public Text RecordArtist;
public Text DownloadSizeStation;
public Text ElapsedStationTime;
public Text NextRecordTitle;
public Text NextRecordArtist;
public Text NextRecordDelay;
public InputField Name;
public InputField Url;
private int invokeDelayCounter = 1;
private bool isStopped = true;
private Color32 color;
private int playtime;
private int recordPlaytime;
private RecordInfo currentRecord;
private RecordInfo nextRecord;
public void Start()
{
if (Player != null)
{
Player.OnPlaybackStart += onPlaybackStart;
Player.OnPlaybackEnd += onPlaybackEnd;
Player.OnAudioStart += onAudioStart;
Player.OnAudioEnd += onAudioEnd;
Player.OnAudioPlayTimeUpdate += onAudioPlayTimeUpdate;
Player.OnBufferingProgressUpdate += onBufferingProgressUpdate;
Player.OnErrorInfo += onErrorInfo;
Player.OnRecordChange += onRecordChange;
Player.OnRecordPlayTimeUpdate += onRecordPlayTimeUpdate;
Player.OnNextRecordChange += onNextRecordChange;
Player.OnNextRecordDelayUpdate += onNextRecordDelayUpdate;
if (ErrorText != null)
{
ErrorText.text = string.Empty;
}
if (Name != null)
{
Name.text = Player.Station.Name;
}
if (Url != null)
{
Url.text = Player.Station.Url;
}
}
else
{
string text = "'Player' is null!";
if (ErrorText != null)
{
ErrorText.text = text;
}
Debug.LogError(text);
}
if (ElapsedTime != null)
{
ElapsedTime.text = Constants.TEXT_STOPPED;
}
if (Station != null)
{
Station.text = Constants.TEXT_QUESTIONMARKS;
}
if (MainImage != null)
{
color = MainImage.color;
}
if (Player != null)
{
if (PlayButton != null)
{
PlayButton.interactable = !Player.isPlayback;
}
if (StopButton != null)
{
StopButton.interactable = Player.isPlayback;
}
if (MainImage != null && Player.isPlayback)
{
MainImage.color = PlayColor;
}
}
onPlaybackEnd(null);
}
public void Update()
{
if (Time.frameCount % 30 == 0 && Player != null && Player.isPlayback && nextRecord != null && nextRecord.Equals(currentRecord))
{
if (NextRecordTitle != null)
{
NextRecordTitle.text = string.Empty;
}
if (NextRecordArtist != null)
{
NextRecordArtist.text = string.Empty;
}
}
if (Player != null)
{
Player.Station.Name = Name.text;
Player.Station.Url = Url.text;
}
}
public void OnDestroy()
{
if (Player != null)
{
Player.OnPlaybackStart -= onPlaybackStart;
Player.OnPlaybackEnd -= onPlaybackEnd;
Player.OnAudioStart -= onAudioStart;
Player.OnAudioEnd -= onAudioEnd;
Player.OnAudioPlayTimeUpdate -= onAudioPlayTimeUpdate;
Player.OnBufferingProgressUpdate -= onBufferingProgressUpdate;
Player.OnErrorInfo -= onErrorInfo;
Player.OnRecordChange -= onRecordChange;
Player.OnRecordPlayTimeUpdate -= onRecordPlayTimeUpdate;
Player.OnNextRecordChange -= onNextRecordChange;
Player.OnNextRecordDelayUpdate -= onNextRecordDelayUpdate;
}
}
public void Play()
{
if (Player != null)
{
if (ErrorText != null)
{
ErrorText.text = string.Empty;
}
Player.Play();
if (Player.Station != null && Station != null)
{
Station.text = Player.Station.Name;
}
}
}
public void Stop()
{
if (Player != null)
{
Player.Stop();
}
if (Station != null)
{
Station.text = Constants.TEXT_QUESTIONMARKS;
}
}
public void OpenUrl()
{
if (Player != null)
{
Application.OpenURL(Player.Station.Station);
}
}
public void OpenSpotifyUrl()
{
if (Player != null)
{
Application.OpenURL(Player.RecordInfo.SpotifyUrl);
}
}
public void FormatDropdownChanged(int index)
{
if (Player != null)
{
if (index == 0)
{
Player.Station.Format = AudioFormat.MP3;
}
else
{
Player.Station.Format = AudioFormat.OGG;
}
}
}
private void onPlaybackStart(RadioStation station)
{
if (ErrorText != null)
{
ErrorText.text = string.Empty;
}
if (PlayButton != null)
{
PlayButton.interactable = false;
}
if (StopButton != null)
{
StopButton.interactable = true;
}
if (Station != null)
{
Station.text = Player.Station.Name;
}
if (MainImage != null)
{
MainImage.color = PlayColor;
}
}
private void onPlaybackEnd(RadioStation station)
{
if (PlayButton != null)
{
PlayButton.interactable = true;
}
if (StopButton != null)
{
StopButton.interactable = false;
}
if (ElapsedTime != null)
{
ElapsedTime.text = Constants.TEXT_STOPPED;
}
if (ElapsedRecordTime != null)
{
ElapsedRecordTime.text = Helper.FormatSecondsToHourMinSec(0.0);
}
if (ElapsedStationTime != null)
{
ElapsedStationTime.text = Helper.FormatSecondsToHourMinSec(0.0);
}
if (DownloadSizeStation != null)
{
DownloadSizeStation.text = Helper.FormatBytesToHRF(0L);
}
if (RecordTitle != null)
{
RecordTitle.text = string.Empty;
}
if (RecordArtist != null)
{
RecordArtist.text = string.Empty;
}
if (NextRecordTitle != null)
{
NextRecordTitle.text = string.Empty;
}
if (NextRecordArtist != null)
{
NextRecordArtist.text = string.Empty;
}
if (NextRecordDelay != null)
{
NextRecordDelay.text = string.Empty;
}
if (MainImage != null)
{
MainImage.color = color;
}
if (ElapsedTime != null)
{
ElapsedTime.text = Constants.TEXT_STOPPED;
}
}
private void onAudioStart(RadioStation station)
{
isStopped = false;
}
private void onAudioEnd(RadioStation station)
{
isStopped = true;
}
private void onAudioPlayTimeUpdate(RadioStation station, float playtime)
{
if ((int)playtime != this.playtime)
{
if (ElapsedTime != null)
{
ElapsedTime.text = Helper.FormatSecondsToHourMinSec(playtime);
}
if (DownloadSizeStation != null)
{
DownloadSizeStation.text = Helper.FormatBytesToHRF(station.TotalDataSize);
}
if (ElapsedStationTime != null)
{
ElapsedStationTime.text = Helper.FormatSecondsToHourMinSec(station.TotalPlayTime);
}
if (playtime > 30f)
{
invokeDelayCounter = 1;
}
this.playtime = (int)playtime;
}
}
private void onBufferingProgressUpdate(RadioStation station, float progress)
{
if (ElapsedTime != null)
{
ElapsedTime.text = Constants.TEXT_BUFFER + progress.ToString("0%");
}
}
private void onErrorInfo(RadioStation station, string info)
{
Stop();
onPlaybackEnd(station);
if (!isStopped)
{
if (invokeDelayCounter < Retries)
{
Debug.LogWarning("Error occoured -> Restarting station." + Environment.NewLine + info);
Invoke("play", 0.4f * (float)invokeDelayCounter);
invokeDelayCounter++;
return;
}
string text = "Restarting station failed more than " + Retries + " times - giving up!" + Environment.NewLine + info;
if (ErrorText != null)
{
ErrorText.text = text;
}
Debug.LogError(text);
}
else if (ErrorText != null)
{
ErrorText.text = info;
}
}
private void onRecordChange(RadioStation station, RecordInfo record)
{
currentRecord = record;
if (RecordTitle != null)
{
RecordTitle.text = record.Title;
}
if (RecordArtist != null)
{
RecordArtist.text = record.Artist;
}
if (NextRecordDelay != null)
{
NextRecordDelay.text = string.Empty;
}
}
private void onRecordPlayTimeUpdate(RadioStation station, RecordInfo record, float playtime)
{
if ((int)playtime != recordPlaytime)
{
recordPlaytime = (int)playtime;
if (ElapsedRecordTime != null)
{
ElapsedRecordTime.text = Helper.FormatSecondsToHourMinSec(playtime);
}
}
}
private void onNextRecordChange(RadioStation station, RecordInfo record, float delay)
{
nextRecord = record;
if (NextRecordTitle != null)
{
NextRecordTitle.text = record.Title;
}
if (NextRecordArtist != null)
{
NextRecordArtist.text = record.Artist;
}
}
private void onNextRecordDelayUpdate(RadioStation station, RecordInfo record, float delay)
{
if (NextRecordDelay != null)
{
NextRecordDelay.text = delay.ToString("#0.0");
}
}
private void play()
{
if (!isStopped)
{
Play();
}
}
}
}