【问题标题】:Streaming audio using libspotifydotnet (Spotify - libspotify)使用 libspotifydotnet (Spotify - libspotify) 流式传输音频
【发布时间】:2015-03-01 23:12:31
【问题描述】:

在查看了thisthisthis 的帖子后,我知道我必须使用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

【问题讨论】:

    标签: c# spotify libspotify


    【解决方案1】:

    最后,我关注了thisthis的问题。他们帮了我很多忙。

    我不得不这样做:

    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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-22
      • 2013-04-12
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多