문제

I'm using the latest Xcode, Xcode 5.0.2, I test it with iPad Simulator 7.0, my audio file is m4a, imported & written with GarageBand on my iPad, but when I tried to play it, it goes nil, I imported AVFoundation.framework to my project, here is the .h file

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

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

here is the .m file. I use NSLog to see if my audioPlayer is nil.

- (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");
    }
}

When I ran the app, NSLog appeared and displayed "audioPlayer is nil" I also tried a lot of other code but all I get is nil, please help, thank you

올바른 솔루션이 없습니다

다른 팁

Replace

NSLog(@"audioPlayer is nil");

with following code to get error information:

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

try this to implement the function:

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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top