2017-10-05 462 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

相关问题