1

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

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

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

谢谢你的帮助,莫腾

4

1 回答 1

2

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

audioPlayer.delegate = self;

希望这可以帮助!

于 2012-08-31T03:28:05.423 回答