gpt4 book ai didi

ios - AVAudioPlayer 委托(delegate)不会在类方法中被调用吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:54:30 25 4
gpt4 key购买 nike

我正在使用 AVAudioPlayer 并设置它的委托(delegate),但它的委托(delegate)没有被调用

      + (void) playflip
{
NSString *path;
path = [[NSBundle mainBundle] pathForResource:@"flip" ofType:@"mp3"];
AVAudioPlayer *flip;
flip = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:Nil];
flip.delegate = self;
[flip play];
}

我正在实现的类是声音类

  @interface    SoundClass : NSObject <AVAudioPlayerDelegate>

我调用这个委托(delegate)

    - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(@"delegate called");
[player release];
player = nil;
}

最佳答案

看起来您的 flip 对象可能超出范围,因为您的其余代码看起来没问题。这是我的做法:

// controller.h
@interface SoundClass : NSObject <AVAudioPlayerDelegate> {}
// @property(nonatomic,retain) NSMutableDictionary *sounds;
// I have lots of sounds, pre-loaded in a dictionary so that I can reference by name
// With one player, you can just use:
@property(nonatomic,retain) AVAudioPlayer *player;

然后在你的.m中分配和加载声音

player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:Nil];
[player prepareToPlay];
player.delegate = self;
[player play];

现在您应该收到 DidFinishPlaying 通知。

关于ios - AVAudioPlayer 委托(delegate)不会在类方法中被调用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8343402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com