Pregunta

I'm using SPPlaybackManager to play tracks, but am having a difficult time with some small but important aspects of playback. Tracks are loading and streaming, but often begin playing .25 - 1.25 seconds after the callback function of SPAsyncLoading is called. I've tried a number of methods to see when the track really starts playing but all seem to indicate playback has initiated the moment the SPAsyncLoading callback is called (including playbackManager.isPlaying, playbackManager.currentTrack, etc). I determined that playback actually begins after the brief latency I mentioned before by setting an NSTimer to run for 10 seconds upon the SPAsyncLoading callback, getting playbackManager.trackPosition after the 10 seconds and finding the difference. TL;DR - the playback doesn't actually start when CocoaLibSpotify says it starts.

A few problems arise because of this: first and most importantly, the song gets chopped off at the end by whatever offset existed between callback and playback initially. Second, from a UX perspective, I need to be able to indicate to my users that they're in the "dead space" between SPAsyncLoading callback and actual playback (i.e. that the track is preparing to play, but not yet playing). Any recommendations on ways to circumvent or solve these problems much appreciated!

¿Fue útil?

Solución

Firstly, don't use SPAsyncLoading for playback-related stuff - that's not what it's for, and SPPlaybackManager doesn't confirm to <SPAsyncLoading> anyway.

The isPlaying property flips to YES once the library has accepted the track for playback, and you should base your play/pause UI on this property. Due to latency and caching and whatnot, audio may take a little while to appear as you've seen.

The trackPosition property, however, is updated as a direct result of audio being pushed to the speakers. Using a timer to watch this is overkill - just use KVO to observe the value and you'll always be in sync.

Be aware that SPPlaybackManager is a very simple class designed for playing a single track. For more advanced playback, I recommend looking at the code of SPPlaybackManager and using it for a base of your own class that uses the playback methods of SPSession and SPCoreAudioController directly - you'll get much better control over playback that way. The only Objective-C example of this I'm aware of is in the Viva project - specifically the VivaPlaybackManager class over on GitHub. That class supports shuffle, local files playback, Last.fm etc so it may be too complicated, but it does multitrack playback just fine without losing a single sample.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top