首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSSound停止函数

NSSound停止函数
EN

Stack Overflow用户
提问于 2020-08-26 12:46:44
回答 1查看 67关注 0票数 0

谢谢你的帮助。

这将激活引用文件的基本回放:

代码语言:javascript
复制
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ping" ofType:@"aiff"] byReference:NO];

[sound play];

终止播放的正确方法是什么?没有成功地实施以下措施:

代码语言:javascript
复制
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ping" ofType:@"aiff"] byReference:NO];

if([sound isPlaying])  {
    
    [sound stop];

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-26 18:01:18

从您在问题中包含的几个代码片段来看,您似乎正在这样做:

代码语言:javascript
复制
// create new sound object
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ping" ofType:@"aiff"] byReference:NO];
// start it playing
[sound play];

然后创建另一个新的声音对象:

代码语言:javascript
复制
// create new sound object
NSSound *sound = [[NSSound alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ping" ofType:@"aiff"] byReference:NO];

// this cannot be true - you just created the sound object
if([sound isPlaying])  {
    [sound stop];
}

试试这个非常简单的例子(只需将Play和Stop按钮添加到视图控制器并将它们连接起来):

代码语言:javascript
复制
#import "ViewController.h"

@interface ViewController() {
    NSSound *sound;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // instantiate sound object with file from bundle
    sound = [[NSSound alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1162" ofType:@"aiff"] byReference:NO];
}

- (IBAction)playClicked:(id)sender {
    if (sound) {
        [sound play];
    }
}
- (IBAction)stopClicked:(id)sender {
    if (sound && [sound isPlaying]) {
        [sound stop];
    }
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];
}

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

https://stackoverflow.com/questions/63598023

复制
相关文章

相似问题

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