[英]AVAudioPlayer is nil after viewDidLoad
我正在使用最新的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為nil”,我也嘗試了很多其他代碼,但我得到的都是nil,請幫助,謝謝
更換
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.