加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用NAudio在C#中播放ohLibSpotify pcm数据流

发布时间:2020-12-16 01:59:41 所属栏目:百科 来源:网络整理
导读:我正在尝试播放来自ohLibSpotify c#library( https://github.com/openhome/ohLibSpotify)的原始pcm数据. 我在以下回调中获取数据: public void MusicDeliveryCallback(SpotifySession session,AudioFormat format,IntPtr frames,int num_frames){ //EXAMPLE
我正在尝试播放来自ohLibSpotify c#library( https://github.com/openhome/ohLibSpotify)的原始pcm数据.

我在以下回调中获取数据:

public void MusicDeliveryCallback(SpotifySession session,AudioFormat format,IntPtr frames,int num_frames)
{
    //EXAMPLE DATA
    //format.channels = 2,format.samplerate = 44100,format.sample_type = Int16NativeEndian
    //frames = ?
    //num_frames = 2048
}

现在我想用NAudio(http://naudio.codeplex.com/)直接播放收到的数据.使用以下代码片段,我可以从磁盘播放mp3文件.是否可以直接将从spotify接收的数据传递给NAudio并实时播放?

using (var ms = File.OpenRead("test.pcm"))
using (var rdr = new Mp3FileReader(ms))
using (var wavStream = WaveFormatConversionStream.CreatePcmStream(rdr))
using (var baStream = new BlockAlignReductionStream(wavStream))
using (var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
{
    waveOut.Init(baStream);
    waveOut.Play();
    while (waveOut.PlaybackState == PlaybackState.Playing)
    {
        Thread.Sleep(100);
    }
}

编辑:
我更新了我的代码.该程序不会抛出任何错误,但我也听不到音乐.我的代码有什么问题吗?

这是音乐传递回调:

public void MusicDeliveryCallback(SpotifySession session,int num_frames)
{
    //format.channels = 2,format.sample_type = Int16NativeEndian
    //frames = ?
    //num_frames = 2048

    byte[] frames_copy = new byte[num_frames];
    Marshal.Copy(frames,frames_copy,num_frames);

    bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat(format.sample_rate,format.channels));
    bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(40);            
    bufferedWaveProvider.AddSamples(frames_copy,num_frames);
    bufferedWaveProvider.Read(frames_copy,num_frames);

    if (_waveOutDeviceInitialized == false)
    {
        IWavePlayer waveOutDevice = new WaveOut();
        waveOutDevice.Init(bufferedWaveProvider);
        waveOutDevice.Play();
        _waveOutDeviceInitialized = true;
    }
}

这些是SessionListener中被覆盖的回调:

public override int MusicDelivery(SpotifySession session,int num_frames)
{
    _sessionManager.MusicDeliveryCallback(session,format,frames,num_frames);
    return base.MusicDelivery(session,num_frames);
}

public override void GetAudioBufferStats(SpotifySession session,out AudioBufferStats stats)
{
    stats.samples = 2048 / 2;   //???
    stats.stutter = 0;          //???
}

解决方法

我想你可以这样做:

>创建一个BufferedWaveProvider.
>将此传递给waveOut.Init.
>在MusicDeliveryCallback中,使用Marshal.Copy从本机缓冲区复制到托管字节数组.
>将此托管字节数组传递给BufferedWaveProvider上的AddSamples.
>在GetAudioBufferStats回调中,对“samples”使用bufferedWaveProvider.BufferedBytes / 2,并将“stutters”保留为0.

我认为这会奏效.它涉及一些不必要的复制,并没有准确地记录口吃,但它是一个很好的起点.我认为实现IWaveProvider并自己管理缓冲可能是更好(更有效和可靠)的解决方案.

我写了ohLibSpotify包装器库,但我不再为同一家公司工作了,所以我不再参与它的开发了.你可以在这个论坛上得到更多帮助:http://forum.openhome.org/forumdisplay.php?fid=6就音乐传递而言,ohLibSpotify旨在尽可能减少开销.它根本不复制音乐数据,只是传递了libspotify库本身提供的相同本机指针,因此您可以将其自己复制到最终目的地,避免不必要的复制层.不过,它确实让它变得有点笨拙.

祝好运!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读