AVAudioPlayerDelegate方法似乎不起作用

时间:2012-08-31 01:30:55

标签: iphone avaudioplayer delegation xcode4.4

我只是在音频文件播放完毕后尝试做某事,但似乎没有用。

在我的.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!");
    }
}

所以......一切都在游泳(除了声音文件以大写字母开头不起作用的事实),但是为什么声音结束时我没有看到“完成”的日志信息?

感谢您的帮助, 的Morten

1 个答案:

答案 0 :(得分:2)

实际上,实际需要设置播放器的委托,例如:

audioPlayer.delegate = self;

希望这有帮助!