Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Streaming with ohLibSpotify and NAudio
06-02-2014, 05:32 PM (This post was last modified: 06-02-2014 05:33 PM by freakimkaefig.)
Post: #6
RE: Streaming with ohLibSpotify and NAudio
Hi,

thanks so much. I managed to play the song.

Here's my code if someone gets the same problem:
Code:
public override int MusicDelivery(SpotifySession session, AudioFormat format, IntPtr frames, int num_frames)
{
    _sessionManager.MusicDeliveryCallback(session, format, frames, num_frames);
    return num_frames;
}
The MusicDelivery callback now returns the number of frames.

Code:
public override void GetAudioBufferStats(SpotifySession session, out AudioBufferStats stats)
{
   base.GetAudioBufferStats(session, out stats);
}
The current AudioBufferStats gets updated in each iteration of _sessionManager.MusicDeliveryCallback.

The code in the _sessionManager is:
Code:
public void Play(Track track)
{
   _bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat());
   _bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(120);
   _audioBufferStats = new AudioBufferStats();

   _session.PlayerLoad(track);
   _session.PlayerPlay(true);

   IWavePlayer waveOutDevice = new WaveOut();
   waveOutDevice.Init(_bufferedWaveProvider);
   waveOutDevice.Play();
}

public void MusicDeliveryCallback(SpotifySession session, AudioFormat format, IntPtr frames, int num_frames)
{
   var size = num_frames * format.channels * 2;
   _copiedFrames = new byte[size];
   Marshal.Copy(frames, _copiedFrames, 0, size);   //Copy Pointer Bytes to _copiedFrames
   _bufferedWaveProvider.AddSamples(_copiedFrames, 0, size);    //adding bytes from _copiedFrames as samples
   SetCurrentAudioBufferStats(_bufferedWaveProvider.BufferedBytes / 2, 0);
}

private void SetCurrentAudioBufferStats(int samples, int stutter)
{
   _audioBufferStats.samples = samples;
   _audioBufferStats.stutter = stutter;
   SessionListener.GetAudioBufferStats(_session, out _audioBufferStats);
}


The song is playing, but with some stutters. Do you know, how to improve the code, to handle these stutters?

Thanks again Andrew, you saved my day!
Find all posts by this user


Messages In This Thread
RE: Streaming with ohLibSpotify and NAudio - freakimkaefig - 06-02-2014 05:32 PM

Forum Jump: