【问题标题】:NSSound stop functionNSSound 停止功能
【发布时间】:2020-08-26 12:46:44
【问题描述】:

感谢您的帮助。

这会激活引用文件的基本播放:

NSSound *sound = [[NSSound alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ping" ofType:@"aiff"] byReference:NO];

[sound play];

终止播放的正确方法是什么?执行以下操作没有运气:

NSSound *sound = [[NSSound alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ping" ofType:@"aiff"] byReference:NO];

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

感谢您的帮助。

【问题讨论】:

  • sound[sound stop] 中与[sound play] 中的NSSound 实例是否相同?
  • 谢谢,是的 - 虽然功能是在离散的操作中实现的。
  • 如果答案解决了问题,那么不,[sound stop] 中的soundNSSound 实例中的[sound play] 不同。
  • 是的。我得到纠正......谢谢。

标签: objective-c nssound


【解决方案1】:

从您在问题中包含的几个代码 sn-ps 来看,看起来您正在这样做:

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

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

// 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];
}

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

#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

【讨论】:

  • 非常感谢!一切正常。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2016-04-09
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-22
  • 2018-05-07
  • 2013-01-27
相关资源
最近更新 更多