我正在使用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