首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用libspotifydotnet的流音频(Spotify - libspotify)

使用libspotifydotnet的流音频(Spotify - libspotify)
EN

Stack Overflow用户
提问于 2015-01-04 03:54:21
回答 1查看 1.5K关注 0票数 1

在查看了帖子之后,我了解到我必须使用NAudio之类的东西来播放stream音频,而且播放歌曲是通过以下方式完成的:

代码语言:javascript
复制
libspotify.sp_session_player_load(_sessionPtr, trackPtr);
libspotify.sp_session_player_play(_sessionPtr, true);

现在,如果我正确理解,我将接收stream作为原始PCM数据。我怎样才能访问这些数据?

根据文档,我看到我必须填充libspotify.sp_audioformat

我该怎么做呢?从哪来的?

我正在查看项目(Jamcast),他们做的事情如下:

代码语言:javascript
复制
libspotify.sp_audioformat format = (libspotify.sp_audioformat)Marshal.PtrToStructure(formatPtr, typeof(libspotify.sp_audioformat));

有人能给我指明正确的方向吗?我完全不知道如何检索stream

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-04 05:56:27

最后,我跟踪了问题。他们帮了我很多忙。

我不得不这样做:

代码语言:javascript
复制
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;

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27762185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档