首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用NSSound播放/停止播放一首歌曲?

如何使用NSSound播放/停止播放一首歌曲?
EN

Stack Overflow用户
提问于 2018-10-06 22:02:27
回答 1查看 78关注 0票数 0

我正在使用NSSound播放来自我的FTP服务器的歌曲。

我的问题:

第一首歌不会停止,以这种方式重叠第二首歌。

在这里,我粘贴完整的代码。这是我在NSTableView中单击一首歌曲时的行为

代码语言:javascript
复制
-(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);
      }

}

输出:

当我点击第一首歌时:

代码语言:javascript
复制
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

当我点击第二首歌时:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-10-07 16:11:49

每次单击它时,都会创建一个新的song对象。创建新对象时,需要将指向song对象的指针存储在某个地方,然后检查该对象是否存在,2) isPlayingTRUE以阻止它。这需要你做更多的计划。这超出了问题的范围。

(在分配song之后,您也永远不会发布它,这会泄漏内存。)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52683712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档