336 lines
6.5 KiB
C#
336 lines
6.5 KiB
C#
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_radio_static.html")]
|
|
public class GUIRadioStatic : 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 InputField Name;
|
|
|
|
public Text Station;
|
|
|
|
public InputField Url;
|
|
|
|
public InputField Bitrate;
|
|
|
|
public InputField Genere;
|
|
|
|
public InputField Rating;
|
|
|
|
public Text Format;
|
|
|
|
public Text SongTitle;
|
|
|
|
public Text Elapsed;
|
|
|
|
public GameObject PlayButton;
|
|
|
|
public GameObject StopButton;
|
|
|
|
public Image MainImage;
|
|
|
|
private Color32 color;
|
|
|
|
private int invokeDelayCounter = 1;
|
|
|
|
private bool isStopped = true;
|
|
|
|
private int playtime;
|
|
|
|
public void Start()
|
|
{
|
|
if (Player != null)
|
|
{
|
|
Player.OnPlaybackStart += onPlayBackStart;
|
|
Player.OnPlaybackEnd += onPlaybackEnd;
|
|
Player.OnAudioStart += onAudioStart;
|
|
Player.OnAudioEnd += onAudioEnd;
|
|
Player.OnAudioPlayTimeUpdate += onAudioPlayTime;
|
|
Player.OnBufferingProgressUpdate += onBufferingProgress;
|
|
Player.OnErrorInfo += onErrorInfo;
|
|
Name.text = Player.Station.Name;
|
|
Station.text = Helper.CleanUrl(Player.Station.Station);
|
|
Url.text = Player.Station.Url;
|
|
Genere.text = Player.Station.Genres;
|
|
Bitrate.text = Player.Station.Bitrate.ToString();
|
|
Rating.text = Player.Station.Rating.ToString();
|
|
Format.text = Player.Station.Format.ToString();
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("'Player' is null!");
|
|
}
|
|
if (Elapsed != null)
|
|
{
|
|
Elapsed.text = Constants.TEXT_STOPPED;
|
|
}
|
|
if (Elapsed != null)
|
|
{
|
|
color = MainImage.color;
|
|
}
|
|
if (Player != null)
|
|
{
|
|
if (PlayButton != null)
|
|
{
|
|
PlayButton.SetActive(!Player.isPlayback);
|
|
}
|
|
if (StopButton != null)
|
|
{
|
|
StopButton.SetActive(Player.isPlayback);
|
|
}
|
|
if (Player.isPlayback && MainImage != null)
|
|
{
|
|
MainImage.color = PlayColor;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (SongTitle != null)
|
|
{
|
|
if (Player != null && Player.isPlayback)
|
|
{
|
|
SongTitle.text = Player.RecordInfo.StreamTitle;
|
|
}
|
|
else
|
|
{
|
|
SongTitle.text = string.Empty;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnDestroy()
|
|
{
|
|
if (Player != null)
|
|
{
|
|
Player.OnPlaybackStart -= onPlayBackStart;
|
|
Player.OnPlaybackEnd -= onPlaybackEnd;
|
|
Player.OnAudioStart += onAudioStart;
|
|
Player.OnAudioEnd += onAudioEnd;
|
|
Player.OnAudioPlayTimeUpdate -= onAudioPlayTime;
|
|
Player.OnBufferingProgressUpdate -= onBufferingProgress;
|
|
Player.OnErrorInfo -= onErrorInfo;
|
|
}
|
|
}
|
|
|
|
public void Play()
|
|
{
|
|
if (Player != null)
|
|
{
|
|
Player.Play();
|
|
}
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
if (Player != null)
|
|
{
|
|
Player.Stop();
|
|
}
|
|
}
|
|
|
|
public void OpenUrl()
|
|
{
|
|
if (Player != null)
|
|
{
|
|
Application.OpenURL(Player.Station.Station);
|
|
}
|
|
}
|
|
|
|
public void ChangeVolume(float volume)
|
|
{
|
|
if (Player != null && Player.Source != null)
|
|
{
|
|
Player.Source.volume = volume;
|
|
}
|
|
}
|
|
|
|
public void NameChanged(string name)
|
|
{
|
|
if (Player != null)
|
|
{
|
|
Player.Station.Name = name;
|
|
}
|
|
}
|
|
|
|
public void StationChanged(string station)
|
|
{
|
|
if (Player != null)
|
|
{
|
|
Player.Station.Station = station;
|
|
}
|
|
}
|
|
|
|
public void UrlChanged(string url)
|
|
{
|
|
if (Player != null)
|
|
{
|
|
Player.Station.Url = url;
|
|
}
|
|
}
|
|
|
|
public void GenresChanged(string genres)
|
|
{
|
|
if (Player != null)
|
|
{
|
|
Player.Station.Genres = genres;
|
|
}
|
|
}
|
|
|
|
public void BitrateChanged(string bitrateString)
|
|
{
|
|
if (Player != null)
|
|
{
|
|
int result;
|
|
if (int.TryParse(bitrateString, out result))
|
|
{
|
|
Player.Station.Bitrate = Helper.NearestBitrate(result, Player.Station.Format);
|
|
}
|
|
if (Bitrate != null)
|
|
{
|
|
Bitrate.text = Player.Station.Bitrate.ToString();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void RatingChanged(string ratingString)
|
|
{
|
|
if (Player != null)
|
|
{
|
|
float result;
|
|
if (float.TryParse(ratingString, out result))
|
|
{
|
|
Player.Station.Rating = Mathf.Clamp(result, 0f, 5f);
|
|
}
|
|
if (Rating != null)
|
|
{
|
|
Rating.text = Player.Station.Rating.ToString();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OpenSpotifyUrl()
|
|
{
|
|
if (Player != null)
|
|
{
|
|
Application.OpenURL(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 (PlayButton != null)
|
|
{
|
|
PlayButton.SetActive(true);
|
|
}
|
|
if (StopButton != null)
|
|
{
|
|
StopButton.SetActive(false);
|
|
}
|
|
if (MainImage != null)
|
|
{
|
|
MainImage.color = color;
|
|
}
|
|
if (Elapsed != null)
|
|
{
|
|
Elapsed.text = Constants.TEXT_STOPPED;
|
|
}
|
|
}
|
|
|
|
private void onAudioStart(RadioStation station)
|
|
{
|
|
isStopped = false;
|
|
}
|
|
|
|
private void onAudioEnd(RadioStation station)
|
|
{
|
|
isStopped = true;
|
|
}
|
|
|
|
private void onAudioPlayTime(RadioStation station, float playtime)
|
|
{
|
|
if ((int)playtime != this.playtime)
|
|
{
|
|
if (Elapsed != null)
|
|
{
|
|
Elapsed.text = Helper.FormatSecondsToHourMinSec(playtime);
|
|
}
|
|
this.playtime = (int)playtime;
|
|
if (playtime > 30f)
|
|
{
|
|
invokeDelayCounter = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void onBufferingProgress(RadioStation station, float progress)
|
|
{
|
|
if (Elapsed != null)
|
|
{
|
|
Elapsed.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;
|
|
}
|
|
Debug.LogError("Restarting station failed more than " + Retries + " times - giving up!" + Environment.NewLine + info);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Could not start the station '" + station.Name + "'! Please try another station. " + Environment.NewLine + info);
|
|
}
|
|
}
|
|
|
|
private void play()
|
|
{
|
|
if (!isStopped)
|
|
{
|
|
Play();
|
|
}
|
|
}
|
|
}
|
|
}
|