【发布时间】:2013-03-29 05:49:34
【问题描述】:
我想在支持 Spotify 的应用中从一首曲目交叉淡入淡出到下一首曲目。两个曲目都是 Spotify 曲目,由于一次只能来自 Spotify 的一个数据流,我怀疑我需要缓冲(我想我可以提前 1.5 倍播放速度)第一首曲目的最后几秒钟,开始流对于轨道二,使用 AudioUnit 淡出一并淡入二。
我查看了示例应用: Viva - https://github.com/iKenndac/Viva SimplePlayer with EQ - https://github.com/iKenndac/SimplePlayer-with-EQ 并试图让我的思想围绕 SPCircularBuffer,但我仍然需要帮助。有人可以给我指出另一个例子或帮助指出一个轨道交叉淡入淡出的游戏计划吗?
更新:感谢 iKenndac,我已经完成了 95% 的工作。我会发布我目前所拥有的:
在 SPPlaybackManager.m 中:initWithPlaybackSession:(SPSession *)aSession {
添加:
self.audioController2 = [[SPCoreAudioController alloc] init];
self.audioController2.delegate = self;
在
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
...
self.audioController.audioOutputEnabled = self.playbackSession.isPlaying;
// for crossfade, add
self.audioController2.audioOutputEnabled = self.playbackSession.isPlaying;
并添加了一个基于 playTrack 的新方法
-(void)crossfadeTrack:(SPTrack *)aTrack callback:(SPErrorableOperationCallback)block {
// switch audiocontroller from current to other
if (self.playbackSession.audioDeliveryDelegate == self.audioController)
{
self.playbackSession.audioDeliveryDelegate = self.audioController2;
self.audioController2.delegate = self;
self.audioController.delegate = nil;
}
else
{
self.playbackSession.audioDeliveryDelegate = self.audioController;
self.audioController.delegate = self;
self.audioController2.delegate = nil;
}
if (aTrack.availability != SP_TRACK_AVAILABILITY_AVAILABLE) {
if (block) block([NSError spotifyErrorWithCode:SP_ERROR_TRACK_NOT_PLAYABLE]);
self.currentTrack = nil;
}
self.currentTrack = aTrack;
self.trackPosition = 0.0;
[self.playbackSession playTrack:self.currentTrack callback:^(NSError *error) {
if (!error)
self.playbackSession.playing = YES;
else
self.currentTrack = nil;
if (block) {
block(error);
}
}];
}
这会启动交叉淡入淡出的计时器
crossfadeTimer = [NSTimer scheduledTimerWithTimeInterval: 0.5 目标:自己 选择器:@selector (crossfadeCountdown) 用户信息:无 重复:是];
为了在第一首曲目的数据加载到 SPCoreAudioController.m 后继续播放,我更改了目标缓冲区长度:
static NSTimeInterval const kTargetBufferLength = 20;
在 SPSession.m 中:end_of_track(sp_session *session) {
我删除了
// sess.playing = NO;
我调用 preloadTrackForPlayback: 大约在曲目结束前 15 秒,然后在 10 秒前调用 crossfadeTrack:。
然后设置crossfadeCountdownTime = [你想要crossfade多少秒]*2;
我在 crosssfade 上淡入音量:
- (void) crossfadeCountdown
{
[UIAppDelegate.playbackSPManager setVolume:(1- (((float)crossfadeCountdownTime/ (thisCrossfadeSeconds*2.0)) *0.2) )];
crossfadeCountdownTime -= 0.5;
if (crossfadeCountdownTime == 1.0)
{
NSLog(@"Crossfade countdown done");
crossfadeCountdownTime = 0;
[crossfadeTimer invalidate];
crossfadeTimer = nil;
[UIAppDelegate.playbackSPManager setVolume:1.0];
}
}
我会继续努力,如果我能做得更好,我会更新。再次感谢 iKendac 的及时帮助!
【问题讨论】:
-
我正在尝试在 CocoaLibSpotify 中实现交叉淡入淡出功能,目前卡在最后一步,淡化两个音频控制器的音量。您能否详细说明您是如何做到这一点的?
标签: ios spotify cocoalibspotify-2.0