C# 使用libspotifydotnet(Spotify-libspotify)进行音频流传输

C# 使用libspotifydotnet(Spotify-libspotify)进行音频流传输,c#,spotify,libspotify,C#,Spotify,Libspotify,退房并发布后,我了解到我必须使用NAudio之类的方式来流式播放音频,而且播放歌曲的方式是: libspotify.sp_session_player_load(_sessionPtr, trackPtr); libspotify.sp_session_player_play(_sessionPtr, true); 现在,如果我理解正确,我将接收流作为原始PCM数据。我如何访问这些数据 根据文档,我发现我必须填充libspotify.sp_audioformat 我到底该怎么做?从哪里来 我正

退房并发布后,我了解到我必须使用
NAudio
之类的方式来
流式播放
音频,而且播放歌曲的方式是:

libspotify.sp_session_player_load(_sessionPtr, trackPtr);
libspotify.sp_session_player_play(_sessionPtr, true);
现在,如果我理解正确,我将接收
流作为原始PCM数据。我如何访问这些数据

根据文档,我发现我必须填充
libspotify.sp_audioformat

我到底该怎么做?从哪里来

我正在检查项目(Jamcast),他们做了如下工作:

libspotify.sp_audioformat format = (libspotify.sp_audioformat)Marshal.PtrToStructure(formatPtr, typeof(libspotify.sp_audioformat));
有人能给我指出正确的方向吗? 我完全不知道如何检索

,最后,我接着问。他们帮了我很多忙

我必须这样做:

public static libspotify.sp_error LoadPlayer(IntPtr trackPtr)
{
    bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat());
    bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(120);

    return libspotify.sp_session_player_load(_sessionPtr, trackPtr);
}

public static void Play()
{
    libspotify.sp_session_player_play(_sessionPtr, true);

    IWavePlayer waveOutDevice = new WaveOut();
    waveOutDevice.Init(bufferedWaveProvider);
    waveOutDevice.Play();
    while (waveOutDevice.PlaybackState == PlaybackState.Playing)
    {
        Thread.Sleep(100);
    }   
}

private static int music_delivery(IntPtr sessionPtr, IntPtr formatPtr, IntPtr framesPtr, int num_frame)
{
    if (num_frame == 0)
        return 0;

    libspotify.sp_audioformat format = (libspotify.sp_audioformat)Marshal.PtrToStructure(formatPtr, typeof(libspotify.sp_audioformat));
    byte[] buffer = new byte[num_frame * sizeof(Int16) * format.channels];
    Marshal.Copy(framesPtr, buffer, 0, buffer.Length);

    bufferedWaveProvider.AddSamples(buffer, 0, num_frame * sizeof(Int16) * format.channels);

    if (Session.OnAudioDataArrived != null)            
        Session.OnAudioDataArrived(buffer);

    return num_frame;

}