AVDudioPlayer在viewDidLoad之后是零

时间:2014-03-03 02:33:35

标签: ios iphone avaudioplayer null

我正在使用最新的Xcode,Xcode 5.0.2,我使用我的音频文件iPad Simulator 7.0进行测试 是m4a,进口&在我的iPad上用GarageBand写的,但是当我尝试播放它时,它会消失 没有,我将AVFoundation.framework导入到我的项目中,这里是.h文件

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface NastyCansViewController : UIViewController
{
AVAudioPlayer *audioPlayer;
}
@end

这是.m文件。我使用NSLog来查看我的audioPlayer是否为零。

- (void)viewDidLoad
{
    NSURL *songURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/song.m4a"], NSBundle mainBundle] resourcePath];
    NSError * error
    audioPlayer = [[AVAudioPlayer alloc] initWithContentOfURL:songURL error:&error];
    if (audioPlayer == nil) {
        NSLog(@"audioPlayer is nil");
    }
}

当我运行应用程序时,NSLog出现并显示“audioPlayer is nil” 我也尝试了很多其他代码,但我得到的只是零,请帮助,谢谢

1 个答案:

答案 0 :(得分:0)

替换

NSLog(@"audioPlayer is nil");

使用以下代码获取错误信息:

NSLog(@"audioPlayer init error:%@", error); 

尝试这个来实现这个功能:

NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"m4a"];
NSError *error = nil;
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:urlStr]
                                             options:NSDataReadingMappedIfSafe
                                               error:&error];
audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
if (audioPlayer == nil) {
    NSLog(@"audioPlayer is init error:%@", error);
}
相关问题