Advertisement
apieceoffruit

MusicPlayer

Mar 28th, 2021
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4.  
  5. namespace JasonStorey.Music
  6. {
  7.     public class MusicPlayer
  8.     {
  9.  
  10.         /// <summary>
  11.         /// Pressing Prev in most media players will restart the track if it has been playing a while, but if it is near the start of the track will assume you meant to go back one.
  12.         /// </summary>
  13.         const float PERCENT_PREV_BECOMES_RESTART = 0.1f;
  14.  
  15.  
  16.         public void Play()
  17.         {
  18.             if (_source.isPlaying)
  19.             {
  20.                 _source.Pause();
  21.                 _paused = true;
  22.             }
  23.             else if (_source.clip != null && _paused)
  24.             {
  25.                 _source.Play();
  26.                 _paused = false;
  27.             }
  28.         }
  29.        
  30.         public void Play(AudioClip clip)
  31.         {
  32.             if(_source.clip != clip)
  33.             {
  34.                 _source.clip = clip;
  35.                 OnTrackChanged(_source.clip);
  36.             }
  37.             _source.Play();
  38.            
  39.         }
  40.  
  41.        
  42.         public void Play(Track track)
  43.         {
  44.             CurrentTrack = track;
  45.             Play(track.Clip);
  46.         }
  47.  
  48.         public void Play(Playlist playlist)
  49.         {
  50.             _currentPlaylist = playlist;
  51.             Play(playlist.Selected);
  52.         }
  53.  
  54.         public async void Play(string path,AudioType type = AudioType.WAV)
  55.         {
  56.             if (Path.GetExtension(path) == ".mp3")
  57.             {
  58.                 Debug.LogWarning($"Cannot stream mp3 files. (Copyright) <color=yellow>{Path.GetFileName(path)}</color> will not play");
  59.                 return;
  60.             }
  61.             var clip = await AudioLoader.LoadClipFromPath(path, type);
  62.             Play(clip);
  63.         }
  64.  
  65.         public void Prev()
  66.         {
  67.             if(PercentThroughCurrentTrack > PERCENT_PREV_BECOMES_RESTART)
  68.                 RestartClip();
  69.             else if(_currentPlaylist != null)
  70.             {
  71.                 _currentPlaylist.SelectPrev();
  72.                 Play(_currentPlaylist);
  73.             }
  74.         }
  75.  
  76.         public void Next()
  77.         {
  78.             if (_currentPlaylist == null) return;
  79.             _currentPlaylist.SelectNext();
  80.             Play(_currentPlaylist);
  81.         }
  82.  
  83.         void RestartClip()
  84.         {
  85.             if(_source.isPlaying) _source.Play();
  86.         }
  87.  
  88.  
  89.         float PercentThroughCurrentTrack => HasAClip ? _source.clip.length / _source.time : 0;
  90.  
  91.         public MusicPlayer(AudioSource source) => _source = source;
  92.         readonly AudioSource _source;
  93.  
  94.         public void UpdateStatus()
  95.         {
  96.             if (_paused) return;
  97.             if (!_source.isPlaying && HasAClip)
  98.             {
  99.                 if(_currentPlaylist != null)
  100.                     TryPlayingNextPlaylistTrack();
  101.                 else
  102.                 {
  103.                     _source.clip = null;
  104.                     OnStopped();
  105.                 }
  106.             }
  107.         }
  108.  
  109.         void TryPlayingNextPlaylistTrack()
  110.         {
  111.             if (_currentPlaylist.AtTheEnd)
  112.             {
  113.                 _source.clip = null;
  114.                 OnStopped();
  115.                 return;
  116.             }
  117.             _currentPlaylist.SelectNext();
  118.             Play(_currentPlaylist);
  119.         }
  120.  
  121.         public void SeekToPercent(float percent)
  122.         {
  123.             if (!HasAClip) return;
  124.             _source.time = _source.clip.length * percent;
  125.         }
  126.        
  127.         bool HasAClip => _source.clip != null;
  128.         public Track CurrentTrack { get; private set; }
  129.  
  130.         protected virtual void OnStopped() => Stopped?.Invoke();
  131.  
  132.         protected virtual void OnTrackChanged(AudioClip clip)
  133.         {
  134.             if (clip == null) return;
  135.             TrackChanged?.Invoke(clip);
  136.         }
  137.        
  138.         bool _paused;
  139.         public event Action<AudioClip> TrackChanged;
  140.         public event Action Stopped;
  141.         Playlist _currentPlaylist;
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement