使用 libspotifydotnet 流式传输音频(Spotify - libspotify)
Streaming audio using libspotifydotnet (Spotify - libspotify)
查看 this, this, and this 帖子后,我了解到我必须使用 NAudio
之类的东西来 stream
音频,并且播放歌曲是通过以下方式完成的:
libspotify.sp_session_player_load(_sessionPtr, trackPtr);
libspotify.sp_session_player_play(_sessionPtr, true);
现在,如果我理解正确的话,我正在接收 stream
作为原始 PCM 数据。我怎样才能访问这些数据?
根据文档,我看到我必须填充 libspotify.sp_audioformat
我该怎么做呢?从哪里来?
我正在检查 this 项目 (Jamcast),他们做了类似的事情:
libspotify.sp_audioformat format = (libspotify.sp_audioformat)Marshal.PtrToStructure(formatPtr, typeof(libspotify.sp_audioformat));
有人能给我指出正确的方向吗?
我完全不知道如何检索 stream
最后,我跟进了this, and this问题。他们帮了我很多。
我不得不这样做:
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;
}
查看 this, this, and this 帖子后,我了解到我必须使用 NAudio
之类的东西来 stream
音频,并且播放歌曲是通过以下方式完成的:
libspotify.sp_session_player_load(_sessionPtr, trackPtr);
libspotify.sp_session_player_play(_sessionPtr, true);
现在,如果我理解正确的话,我正在接收 stream
作为原始 PCM 数据。我怎样才能访问这些数据?
根据文档,我看到我必须填充 libspotify.sp_audioformat
我该怎么做呢?从哪里来?
我正在检查 this 项目 (Jamcast),他们做了类似的事情:
libspotify.sp_audioformat format = (libspotify.sp_audioformat)Marshal.PtrToStructure(formatPtr, typeof(libspotify.sp_audioformat));
有人能给我指出正确的方向吗?
我完全不知道如何检索 stream
最后,我跟进了this, and this问题。他们帮了我很多。
我不得不这样做:
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;
}