using System; using Crosstales.Radio.Model; 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 GUIPlayRadio : MonoBehaviour { [Tooltip("'SimplePlayer' from the scene.")] [Header("Settings")] public SimplePlayer 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 GameObject PlayButton; public GameObject 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; 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.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; } } 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) { PlayButton.SetActive(!Player.isPlayback); StopButton.SetActive(Player.isPlayback); if (Player.isPlayback && MainImage != null) { MainImage.color = PlayColor; } } onPlaybackEnd(null); } public void Update() { if (Time.frameCount % 30 != 0 || !(Player != null) || Player.RadioStation == null || !Player.isPlayback) { return; } if (Station != null) { Station.text = Player.RadioStation.Name; } if (nextRecord != null && nextRecord.Equals(currentRecord)) { if (NextRecordTitle != null) { NextRecordTitle.text = string.Empty; } if (NextRecordArtist != null) { NextRecordArtist.text = string.Empty; } } } public void OnDestroy() { if (Player != null && Player.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.RadioStation != null && Station != null) { Station.text = Player.RadioStation.Name; } } } public void Stop() { if (Player != null) { Player.Stop(); } if (Station != null) { Station.text = Constants.TEXT_QUESTIONMARKS; } } public void OpenUrl() { if (Player != null && Player.Player != null) { Application.OpenURL(Player.Player.Station.Station); } } public void OpenSpotifyUrl() { if (Player != null && Player.Player != null) { Application.OpenURL(Player.Player.RecordInfo.SpotifyUrl); } } private void onPlaybackStart(RadioStation station) { if (PlayButton != null) { PlayButton.SetActive(false); } if (StopButton != null) { StopButton.SetActive(true); } if (MainImage != null) { MainImage.color = PlayColor; } } private void onPlaybackEnd(RadioStation station) { 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 (PlayButton != null) { PlayButton.SetActive(true); } if (StopButton != null) { StopButton.SetActive(false); } 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(); } } } }