我需要在表格视图中播放我从json获取的音频文件(mp3) ,在单击行时,相应的歌曲路径正在传递给另一个 UIView..在加载视图时它应该播放。
我知道互联网上粘贴了很多类似的问题,我尝试了很多对我没有用的方法。
这是代码片段。
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *error = nil;
[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryAmbient error:&error];
// audioFile--contains path--
// NSString *filePath = [[NSBundle mainBundle] pathForResource:audioFile ofType:@"mp3"];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSAllLibrariesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:audioFile];
NSURL *url = [NSURL fileURLWithPath:filePath];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
if([self.player prepareToPlay])
{
NSLog(@"preparing");
}
else
{
NSLog(@"some error");
}
if([self.player play])
{
NSLog(@"playing");
}
NSLog(@"not playing");
NSLog(@"\n\nfile path-> %@\n\n url-> %@",filePath,url);
}
其中player是AVAudioPlayer类的对象
@property (strong, nonatomic) AVAudioPlayer *player;
从上面来看,来自 NSLog() 的值是(对于特定行)
file path-> /Users/ensignweb/Library/Application Support/iPhone Simulator/6.1/Applications/CE146654-3D1B-4F60-B37D-825267FD6EFB/Library/http:/www.fundamentalalvarado.com/assets/sermons/bible-doctrine-series/033113-pm-Doctrine of Creation vs The Lie of Evolution.mp3
url-> file://localhost/Users/ensignweb/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/CE146654-3D1B-4F60-B37D-825267FD6EFB/Library/http:/www.fundamentalalvarado.com/assets/sermons/bible-doctrine-series/033113-pm-Doctrine%20of%20Creation%20vs%20The%20Lie%20of%20Evolution.mp3
当我使用 NSBundle 文件路径为空时,我对其进行了评论。即使在最后,如果循环转义到else。我想可能有问题。
请帮帮我。