How to play a spotify music stream

泪湿孤枕 提交于 2019-12-21 21:30:59

问题


First of all, i am new to audio-programming, so bear with me.

I am trying to play spotify music with NAudio or BASS.Net or any other .net audio-library.

As far as i known, libspotify delivers music as raw PCM data. what is the sample rate of spotify stream (libspotify)?

From the spotify docs: Samples are delivered as integers, see sp_audioformat. One frame consists of the same number of samples as there are channels. I.e. interleaving is on the sample level.

When i try to play a song, spotify makes a callback with a 8192 byte buffer

channels = 2

sample_rate = 44100

num_frames = 2048

I need a little help translating this information to NAudio terms.

I have also tried with a spotify to Bass.Net sample (BASSPlayer.cs). But i haven't heard a single note from my speakers yet. I have tried to play an mp3-song with NAudio and Bass.NET and this works fine, so the speaker volume is ok. https://github.com/Alxandr/SpotiFire/blob/master/SpotiFire.Server/BASSPlayer.cs


回答1:


There is breakthrough with NAudio. This is what i came up with, by using the trial and error method. I'm not sure if this is the right way to calculate the parameters from sampleRate/channels...

But the song is playing :-)

IWavePlayer waveOutDevice = new WaveOut();

using (var pcmStream = new FileStream(PcmFile, FileMode.Open))
{
    const int songDuration = 264000;
    const int sampleRate = 44100;
    const int channels = 2;
    var waveFormat = WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm, sampleRate * channels, 1, sampleRate * 2 * channels, channels, 16);
    var waveStream = new RawSourceWaveStream(pcmStream, waveFormat);

    waveOutDevice.Init(waveStream);
    waveOutDevice.Play();
    Thread.Sleep(songDuration);
    waveOutDevice.Stop();
    waveStream.Close();
    waveOutDevice.Dispose();
}


来源:https://stackoverflow.com/questions/11452236/how-to-play-a-spotify-music-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!