/// <summary> /// Prepares media for playback /// </summary> /// <param name="filename"></param> private void LoadMedia(PlayListEntry entry) { FMOD.RESULT result; //load a new sound result = _system.createSound(entry.FileName, FMOD.MODE._2D | FMOD.MODE.NONBLOCKING | FMOD.MODE.CREATESTREAM, out _sound); CheckError(result); //how to determine the media is ready? an event perhaps? FMOD.OPENSTATE openState; uint percentBuffered; bool starving; bool diskBusy; //check to see if it is ready _sound.getOpenState(out openState, out percentBuffered, out starving, out diskBusy); //check state while (openState == FMOD.OPENSTATE.LOADING) { _sound.getOpenState(out openState, out percentBuffered, out starving, out diskBusy); Thread.Sleep(25); } Length = GetLength(); }
/// <summary> /// Begins playback or pauses playback /// </summary> /// <param name="entry">The playlist entry to play. Pass null to play from start</param> public void PlayOrPause(PlayListEntry entry) { //if we have an entry, then stop play back if (entry != null && GetIsPlaying()) { //stop playback Stop(); } //if the track is playing, then pressing play again will pause the track if (GetIsPlaying()) { //its playing: pause or unpause Pause(); } else { if (entry != null) { //set playlist index to playing entry PlayListIndex = NowPlaying.IndexOf(entry); //add the entry to the history AddToHistory(entry); } //run playback Run(entry); } }
/// <summary> /// Adds an entry to the history /// </summary> /// <param name="entry"></param> private void AddToHistory(PlayListEntry entry) { //push entry into the history History.Add(entry); //increase the history pointer HistoryPointer = History.IndexOf(entry); }
/// <summary> /// Removes an entry from the playlist /// </summary> /// <param name="entry"></param> public void RemoveFromNowPlaying(PlayListEntry entry) { //remove NowPlaying.Remove(entry); if (entry == _playingEntry) { //stop playing Stop(); } }
/// <summary> /// Begins playback. This method is called when the user initiates playback. /// </summary> private void Play(PlayListEntry entry) { if (entry == null) { //throw an error throw new ArgumentNullException("entry"); } //set playing IsPlaying = true; //create new cancellation token _playbackCts = new CancellationTokenSource(); //create a play task var playTask = Task.Run(() => { //reset FreeChannelResources(); //send the media to be played LoadMedia(entry); //play the stream. holds thread hostage until playback stops PlayStream(); }, _playbackCts.Token); //when playback is stopped, handle what to do next //either we exit and go idle, or we pick the next track. //if the user selected a different entry to play, we treat it like a hard stop. playTask.ContinueWith((t) => { //if canceled, exit if (IsStopping) { //reset IsStopping = false; //exit return; } //play the next song Run(); }); }
/// <summary> /// Gets the next media in the list /// </summary> /// <returns></returns> private PlayListEntry GetNextMedia() { if (NowPlaying.Count > 0 && PlayListIndex + 1 < NowPlaying.Count) { PlayListEntry entry = null; if (HistoryPointer < History.Count - 1) { //increase the history pointer HistoryPointer++; //get the playlist entry from the current index entry = History[HistoryPointer]; } else { //set next position PlayListIndex++; //get the playlist entry from the current index entry = NowPlaying[PlayListIndex]; //add to history AddToHistory(entry); } //return the entry we got return(entry); } else { if (RepeatOn) { //reset the playlist ResetPlaylist(); //get next media return(GetNextMedia()); } //return null to end playback return(null); } }
/// <summary> /// Run playback /// </summary> /// <param name="entry"></param> private void Run(PlayListEntry entry = null) { //check to see if we have a reference to the last entry if (_playingEntry != null) { //this entry is not playing any more _playingEntry.IsPlaying = false; } //get next media if entry is null entry = entry ?? GetNextMedia(); if (entry != null) { //keep a global reference to the playing entry _playingEntry = entry; //set is playing on the entry entry.IsPlaying = true; //nothing is playing, play something Play(entry); } }
/// <summary> /// Adds a new entry to the now playing list, and adds it to the internal playlist if currently playing /// </summary> /// <param name="entry"></param> public void AddToNowPlaying(PlayListEntry entry) { //add media entry to the now playing playlist NowPlaying.Add(entry); }