我正在使用NSSound播放来自我的FTP服务器的歌曲。
我的问题:
第一首歌不会停止,以这种方式重叠第二首歌。
在这里,我粘贴完整的代码。这是我在NSTableView中单击一首歌曲时的行为
-(void)tableViewSelectionDidChange:(NSNotification *)notification{
NSInteger row = [[notification object] selectedRow];
NSString *URL = [NSString stringWithFormat:@"ftp://user:pass@IP/Public/Music/%@",[TableContents objectAtIndex:row]];
NSSound *song = [[NSSound alloc]initWithContentsOfURL:[NSURL URLWithString:URL] byReference:NO];
NSLog(@"is playing before %hhd", song.isPlaying);
if(song.isPlaying){
[song stop];
NSLog(@"is playing IF if %hhd", song.isPlaying);
}else{
[song play];
NSLog(@"is playing ELSE %hhd", song.isPlaying);
}
}输出:
当我点击第一首歌时:
2018-10-06 23:51:08.488690+0200 AIOMediaCenter[4294:492923] is playing before 0
2018-10-06 23:51:08.489099+0200 AIOMediaCenter[4294:492923] is playing ELSE 1当我点击第二首歌时:
2018-10-06 23:51:12.022284+0200 AIOMediaCenter[4294:492923] is playing before 0
2018-10-06 23:51:12.022375+0200 AIOMediaCenter[4294:492923] is playing ELSE 1发布于 2018-10-07 16:11:49
每次单击它时,都会创建一个新的song对象。创建新对象时,需要将指向song对象的指针存储在某个地方,然后检查该对象是否存在,2) isPlaying是TRUE以阻止它。这需要你做更多的计划。这超出了问题的范围。
(在分配song之后,您也永远不会发布它,这会泄漏内存。)
https://stackoverflow.com/questions/52683712
复制相似问题