在正在播放时开始播放下一首歌曲

时间:2017-10-05 21:05:49

标签: c# fadein fadeout naudio

我有一个“玩家”类,应该管理我的全球音乐播放器。 到目前为止这也有效。课程位于最底层,如果您有任何改进建议,请随时提供给我们。

我想在当前歌曲结束时开始第二首歌。 所以FadeIn进入下一首歌曲,并从当前歌曲中选择FadeOut,这使得这首歌更加安静。

我此刻的方法是,一首歌在“waveOutDevice1”对象中运行,第二首歌在第二个对象中等待。当前歌曲即将结束时,第二个WavePlayer启动。但是,一旦现在的歌曲即将结束,我不知道我怎么能对它做出反应。

您有想法或建议吗? 亲切的问候

我的播放器课程:

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
}

2 个答案:

答案 0 :(得分:0)

如果您可以在开始播放时获得歌曲的持续时间,您可以设置duration - fadeInTime的计时器,并在计时器触发时开始淡入淡出。

答案 1 :(得分:0)

另一种方法是使用单个波形输出设备和MixingSampleProvider将第二首歌曲添加为混音器的新输入,因为第一首歌曲正在结束。您可以通过创建自己的包装ISampleProvider来完成此操作,Read方法可以准确跟踪您的目标。