【发布时间】:2013-07-23 05:59:49
【问题描述】:
我需要在表格视图中播放我从json 获取的音频文件(mp3),单击行时,相应的歌曲路径正在传递到另一个 UIView..在加载视图时它应该播放。
我知道有很多类似的问题粘贴在互联网上,我尝试了很多对我没有用的方法。
这里是代码sn-p。
- (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 时,文件路径为空,所以我评论了它。即使在最后 If Loop 转义到 else。我想可能是问题所在。
请帮帮我。
【问题讨论】:
-
你的意思是@property (strong, nonatomic) AVAudioPlayer *player;line..它已经很强大了吧。
标签: iphone ios json avaudioplayer