2017-10-05 520 views
0

我有一個「播放器」類,應該管理我的全球音樂播放器。 這也適用於目前爲止。如果你有任何改進的建議,那麼這個班就在最後,隨時可以給我們提供。開始播放下一首歌曲

我想在當前歌曲結束時開始播放第二首歌曲。 因此,淡入到當前歌曲的下一首歌曲和FadeOut中,這使得歌曲更加安靜。

我現在的做法是一首歌曲在「waveOutDevice1」對象中運行,另一首歌曲在第二個對象中等待。只要當前歌曲即將結束,第二個WavePlayer就會啓動。但是現在的歌曲即將結束,我不知道該如何反應。

您有什麼想法或建議嗎? 隨着親切的問候

我Player類:

public class Player 
{ 
    #region Properties, Fields 
    private IWavePlayer waveOutDevice1; 
    private IWavePlayer waveOutDevice2; 
    private AudioFileReader fileReader1; 
    private AudioFileReader fileReader2; 

    public AudioFile CurrentSong { get; private set; } 
    public Playlist CurrentPlaylist { get; private set; } 

    public List<AudioFile> lstPastSongs { get; private set; } 

    public List<AudioFile> lstNextSongs { get; set; } 

    public PlaybackState PlaybackState { get; private set; } 

    public bool Muted { get; private set; } 

    private float OldVolume = 0.0f; 
    #endregion 

    public Player() 
    { 
     this.lstNextSongs = new List<AudioFile>(); 
     this.lstPastSongs = new List<AudioFile>(); 
    } 

    #region Methods 
    public void Play(int index) 
    { 
     if (this.lstNextSongs.Count > 0 && index >= 0 && index < this.lstNextSongs.Count) 
     { 
      this.ResetFileReader(); 
      this.CurrentSong = this.lstNextSongs[index]; 
      this.CurrentPlaylist = this.CurrentSong.Playlist; 
      this.lstNextSongs.RemoveAt(index); 
      this.fileReader1 = new AudioFileReader(this.CurrentSong.Path); 
      this.waveOutDevice1 = new WaveOut(); 
      this.waveOutDevice1.PlaybackStopped += WaveOutDevice1_PlaybackStopped; 

      this.waveOutDevice1.Init(this.fileReader1); 
      this.PlaybackState = PlaybackState.Playing; 
      this.waveOutDevice1.Play(); 
     } 
    } 

    private void WaveOutDevice1_PlaybackStopped(object sender, StoppedEventArgs e) 
    { 
     this.Next(); 
    } 

    private void ResetFileReader() 
    { 
     if (this.fileReader1 != null) 
     {     
      this.fileReader1.Dispose(); 
      this.fileReader1 = null; 
     } 
     if (this.fileReader2 != null) 
     { 
      this.fileReader2.Dispose(); 
      this.fileReader2 = null; 
     } 
     if(this.waveOutDevice1 != null) 
     { 
      this.waveOutDevice1.Dispose(); 
      this.waveOutDevice1 = null; 
     } 
     if(this.waveOutDevice2 != null) 
     { 
      this.waveOutDevice2.Dispose(); 
      this.waveOutDevice2 = null; 
     } 
    } 

    public void Pause() 
    { 
     if(this.waveOutDevice1 != null) 
      if (this.waveOutDevice1.PlaybackState == PlaybackState.Playing) 
       this.waveOutDevice1.Pause(); 
     if(this.waveOutDevice2 != null) 
      if (this.waveOutDevice2.PlaybackState == PlaybackState.Playing) 
       this.waveOutDevice2.Pause(); 
     this.PlaybackState = PlaybackState.Paused; 
    } 

    public void Continue() 
    { 
     if (this.waveOutDevice1 != null) 
      if (this.waveOutDevice1.PlaybackState == PlaybackState.Paused) 
       this.waveOutDevice1.Play(); 
     if (this.waveOutDevice2 != null) 
      if (this.waveOutDevice2.PlaybackState == PlaybackState.Paused) 
       this.waveOutDevice2.Play(); 
     this.PlaybackState = PlaybackState.Playing; 
    } 

    public void Next() 
    { 
     if(this.lstNextSongs.Count > 0) 
     { 
      if (this.CurrentSong != null) 
       this.lstPastSongs.Add(this.CurrentSong); 
      if (GlobalSettings.Shuffle) 
      { 
       System.Random random = new System.Random(); 
       int randomNumber = random.Next(0, this.lstNextSongs.Count - 1); 
       this.Play(randomNumber); 
      } 
      else 
       this.Play(0); 
     } 
     else 
     { 
      if(GlobalSettings.Replay) 
       if(GlobalSettings.CurrentPlaylist != null) 
        for (int i = 0; i < GlobalSettings.CurrentPlaylist.panPlaylist.SongPlaylist.NumberOfSongs; i++) 
         this.lstNextSongs.AddRange(GlobalSettings.CurrentPlaylist.panPlaylist.SongPlaylist.AllSongs); 
     } 
    } 

    public void Previous() 
    { 
     if(this.CurrentSong == null) 
     { 
      if(this.lstPastSongs.Count > 0) 
      { 
       this.lstNextSongs.Insert(0, this.lstPastSongs[this.lstPastSongs.Count - 1]); 
       this.lstPastSongs.RemoveAt(this.lstPastSongs.Count - 1); 
       this.Play(0); 
      } 
     } 
     else 
     { 
      if(this.fileReader1 != null) 
       this._Previous(this.waveOutDevice1, this.fileReader1); 
      else if(this.fileReader2 != null) 
       this._Previous(this.waveOutDevice2, this.fileReader2); 
     } 
    } 

    private void _Previous(IWavePlayer waveOutDevice, AudioFileReader fileReader) 
    { 
     if (fileReader.CurrentTime.Seconds >= 10 || this.lstPastSongs.Count == 0) 
     { 
      waveOutDevice.Pause(); 
      fileReader.CurrentTime = new System.TimeSpan(0, 0, 0); 
      waveOutDevice.Play(); 
     } 
     else 
     { 
      this.lstNextSongs.Insert(0, this.CurrentSong); 
      this.lstNextSongs.Insert(0, this.lstPastSongs[this.lstPastSongs.Count - 1]); 
      this.lstPastSongs.RemoveAt(this.lstPastSongs.Count - 1); 
      this.Play(0); 
     } 
    } 

    public void SetVolume(int Volume) 
    { 
     if (Volume > -1 && Volume < 101) 
     { 

      float vol = (float)Volume/100; 
      if (this.fileReader1 != null) 
       this.fileReader1.Volume = vol; 
      if (this.fileReader2 != null) 
       this.fileReader2.Volume = vol; 
      this.Muted = false; 
     } 
    } 

    public void Mute() 
    { 
     if(this.Muted) 
     { 
      if(this.fileReader1 != null) 
      { 
       this.fileReader1.Volume = this.OldVolume; 
       this.Muted = false; 
      } 
      else if(this.fileReader2 != null) 
      { 
       this.fileReader2.Volume = this.OldVolume; 
       this.Muted = false; 
      } 
     } 
     else 
     { 
      this.Muted = true; 
      if(this.fileReader1 != null) 
      { 
       this.OldVolume = this.fileReader1.Volume; 
       this.fileReader1.Volume = 0; 
      } 
      else if(this.fileReader2 != null) 
      { 
       this.OldVolume = this.fileReader2.Volume; 
       this.fileReader2.Volume = 0; 
      } 
     } 
    } 
    #endregion 
} 

回答

0

如果你能得到,當它開始播放歌曲的時間,你可以設置定時器用於duration - fadeInTime並開始淡出定時器觸發時。

+0

yesthat將是一種替代方案。但在我看來,計時器對於一個好的表現來說不是個好主意。 – ExclusivAtom

+0

它不應該。特別是如果你在另一個線程上運行它。 'Thread.Sleep()'影響非常小。 – theGleep

+0

然後我會使用另一個計時器,直到找到更好的解決方案。 – ExclusivAtom

0

另一種方法是使用單波輸出設備和MixingSampleProvider將第二首歌曲作爲第一首歌曲結尾的新輸入添加到調音臺。您可以通過創建自己的包裝ISampleProvider來實現這一點,其Read方法可以準確跟蹤您的位置。

+0

現在我已經坐過渡了幾個小時了,但是我似乎無法正確理解。我還觀看了音樂節目「MusicToCodeBy」,音樂同時播放,但是一首接一首地轉換,我無法到達那裏。你有一個例子嗎? – ExclusivAtom

+0

我也觀看了課程「https://stackoverflow.com/questions/9468083/fading-sound-in-out-using-naudio」 但是這個班沒有讓我到任何地方。 – ExclusivAtom

相關問題