OS X - 在mainBundle之外播放mp3

时间:2013-09-30 14:18:18

标签: xcode macos mp3

我想在mainBundle之外播放一个mp3文件(来自硬盘上的任何位置)。

这是我的尝试(不起作用):

NSString* theFileName = [filepath stringByDeletingPathExtension];

NSLog(@"%@", theFileName); // -> /Volumes/Data%20HD/Music/iTunes/Music/Linkin%20Park/Hybrid%20Theory/01%20Papercut

NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath2 = [mainBundle pathForResource:theFileName ofType:@"mp3" ];
NSData *fileData = [NSData dataWithContentsOfFile:filePath2];
NSError *error2;
audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&error2];
[audioPlayer play];

1 个答案:

答案 0 :(得分:0)

您可以使用NSSound

NSURL* file = [NSURL fileURLWithPath:@"/Music/1.mp3"];
NSSound *sound = [[NSSound alloc] initWithContentsOfURL:file byReference:NO];
[sound play];