如何使用naudio和c#播放歌曲列表?

时间:2016-08-12 21:36:02

标签: c# naudio

当我尝试播放歌曲列表时遇到问题,我的代码无效。每首歌只播放前几秒,但我想播放每首歌曲列表的所有内容:

private void Play()
{
    List<Song> songList = ViewModelLocator.Instance.SongListVM.Songs;
    foreach(Song song  in songList)
    {
        audioFileReader = new AudioFileReader(song.Path);
        waveOutDevice.Init(audioFileReader);
        waveOutDevice.Play();
    }
}

我尝试使用线程:

waveOutDevice.Play();
Thread.Sleep(audioFileReader.TotalTime);

但它只挂起。

1 个答案:

答案 0 :(得分:0)

对我来说,我用过这个:

int song_index=0; // index of the song
private void player_play()
{
        outputdevice.PlaybackStopped += playbackstopped;
        if (outputdevice.PlaybackState != PlaybackState.Paused)
        {
                to_play = playlist.Rows[song_index].Cells[0].Value.ToString();
                audiofile = new AudioFileReader(to_play);
                outputdevice.Init(audiofile);
        }
        outputdevice.Play();
}
// this event occur when the playing file has ended
private void playbackstopped(object sender, StoppedEventArgs e)
{
        song_index++;      // play the next file
        player_play();
}