【发布时间】: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