【问题标题】:AVAudioPlayerDelegate method doesn't seem to workAVAudioPlayerDelegate 方法似乎不起作用
【发布时间】:2012-08-25 19:51:55
【问题描述】:

我只是在音频文件播放完毕后尝试做一些事情,但它似乎不起作用。

在我的 .h 文件中,我有这个:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>

@interface soundViewController : UIViewController
<AVAudioPlayerDelegate>
{
    //various outlets
}
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;

@end

在 .m 文件中我得到了:

#import "soundViewController.h"
@interface soundViewController ()
@end

@implementation soundViewController
@synthesize audioPlayer;

以及方法的:

- (void) playWord{
    Word *currentWord = [[Word alloc]init];
    currentWord = [testWords objectAtIndex:wordNum-1];
    NSString *item = [currentWord word];
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.mp3", [[NSBundle mainBundle] resourcePath], item]];

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil){
    NSLog(@"error in audioPlayer");
    }
else{
    [audioPlayer play];
        NSLog(@"Playing word");
    }
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    if (flag==YES){
        NSLog(@"Finished!");
    }
}

所以......这一切都很好(除了声音文件如果以大写字母开头将无法工作),但为什么当声音结束时我没有看到日志消息“完成”?

感谢您的帮助, 莫腾

【问题讨论】:

    标签: iphone avaudioplayer delegation xcode4.4


    【解决方案1】:

    您实际上需要在实例化播放器后设置它的委托,例如:

    audioPlayer.delegate = self;
    

    希望这会有所帮助!

    【讨论】:

    • 卡尔 - 非常感谢。这解决了问题。我在代码开头“audioPlayer = ....”之后立即插入了您建议的代码,现在它可以完美运行了!!!感谢您抽出宝贵时间查看此内容。
    • 嘿,不客气!很高兴它有帮助。如果您有第二个问题,请接受此答案,以将此问题标记为已为其他用户解决。谢谢!
    • DONE :-) PS:我想你不知道我怎样才能让 AVAudioPlayer 与区分大小写的音频文件识别一起工作?
    • 你到底是什么意思?您是否看到 sim 上的作品/不在设备上的作品,反之亦然?
    • 你好,卡尔。是的 - 我有一个说“我”这个词的音频文件。如果我将它命名为“i.mp3”并使用上面的代码检索它,它可以在我的 iPhone 和 SIM 卡上运行。如果我在 x-code 中删除它,在 x-code 之外将其重命名为“I.mp3”并再次导入,它可以在 SIM 中工作,但是当它尝试在 iPhone 上播放单词时没有声音,我收到错误消息:Error Domain=NSOSStatusErrorDomain Code=-43 “操作无法完成。(OSStatus 错误 -43。)”!由于应用程序中的其他考虑因素,我确实需要将某些特定文件大写...也许我应该开始一个新线程...
    猜你喜欢
    • 1970-01-01
    • 2023-02-04
    • 2021-11-27
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2015-04-26
    相关资源
    最近更新 更多