Spotify - 如何暂停曲目和恢复曲目
Posted
技术标签:
【中文标题】Spotify - 如何暂停曲目和恢复曲目【英文标题】:Spotify - How to pause track and resuming track 【发布时间】:2015-12-03 15:14:24 【问题描述】:所以我制作了一个允许用户从 spotify 流式传输歌曲的应用程序,我在播放和停止使用 spotify 播放器时没有问题。但是,我在暂停和恢复歌曲时遇到问题。
这是我的逻辑:
-
停止当前播放的曲目
当我停止 Spotify 播放器时获取当前播放位置
玩耍并寻找我得到的时间
这是我的代码:
var currentOffset = User.spotifyPlayer!.currentPlaybackPosition
println(currentOffset)
var spotifyTrackUrl : NSURL = NSURL(string: self.spotifyTrackUri!)!
User.spotifyPlayer!.playURIs([spotifyTrackUrl], fromIndex: 0, callback: (error : NSError!) -> Void in
if error != nil
println(error)
else
if User.spotifyPlayer!.isPlaying == true
User.spotifyPlayer!.seekToOffset(currentOffset, callback: (error : NSError!) -> Void in
if error != nil
println("rene 3")
println("spotify seek error : \(error)")
sender.setImage(UIImage(named: "playicon.png"), forState: UIControlState.Normal)
println("rene 4")
)
)
我总是遇到以下错误:
spotify seek error : 由于未指定的问题,操作失败。
我使用的是 swift,但是如果你使用 obj-c,给我任何建议都没关系
【问题讨论】:
【参考方案1】:我认为你不需要使用seekToOffset
来暂停和恢复一首歌你只需要下面的方法:
if self.player?.isPlaying == true
self.player?.setIsPlaying(false, callback: nil)
else
self.player?.setIsPlaying(true, callback: nil)
【讨论】:
您的建议是停止播放器并从头开始播放,我需要暂停播放器,然后在暂停播放器时从当前位置继续播放。谢谢顺便说一句 其实不是从头再玩的,它的作用是暂停播放器,文档里有提到,试试看。这是其文档的链接developer.spotify.com/ios-sdk-docs/Documents/Classes/…: 你试过了吗?我已经尝试了您的解决方案,它不是暂停和恢复 是的,它对我有用,我之前发布的代码在播放/暂停按钮下,奇怪的是它不适合你。【参考方案2】:如果您只是尝试暂停和恢复播放器,则应使用 SPTAudioStreamingController 的内置方法,例如:
[self.player setIsPlaying:!self.player.isPlaying callback:nil];
如果您尝试显示跟踪进度的可视化部分,我建议使用 NSTimer 之类的东西并更新视图,例如:
-(void)updateTimerTickTock:(NSTimer*)sender
NSUInteger currentTime = self.spotify.playbackManager.player.currentPlaybackPosition;
NSUInteger totalDuration = self.spotify.playbackManager.player.currentTrackDuration;
if (totalDuration > 0)
float prg = (float)currentTime/totalDuration;
[self.prgView setProgress:prg animated:YES];
if ((totalDuration - currentTime) < 20 && !hasRequestedNextTrack)
[self.delegate playComponentRequestsPreloadOfNextTrack:self];
hasRequestedNextTrack = YES;
self.readyToPostPlayedSong = YES;
【讨论】:
您的建议是停止并从头开始播放。感谢您的建议 实际上它确实为我暂停和恢复。它不会从 0 重新开始。【参考方案3】:要恢复你应该使用方法:
playURIs:withOptions:callback:
这是一个简单的例子:
NSURL *trackURI = [NSURL URLWithString:uriString];
float startTime = 34.3;
SPTPlayOptions *options = [[SPTPlayOptions alloc] init];
[options setStartTime:startTime];
[self.spotifyPlayer playURIs:[NSArray arrayWithObject:trackURI] withOptions:options callback:^(NSError *error)
if (error != nil)
NSLog(@"*** Starting playback got error: %@", error);
return;
else
NSLog(@"Playing");
];
【讨论】:
以上是关于Spotify - 如何暂停曲目和恢复曲目的主要内容,如果未能解决你的问题,请参考以下文章
Spotify + AppleScript:将当前曲目添加到播放列表