我需要在表格视图中播放我从json获取的音频文件(mp3),在点击行时,相应的歌曲路径将传递到另一个应该播放的UIView ..on加载视图。
我知道在互联网上粘贴了很多类似的问题,我尝试了很多方法对我没用。
以下是代码段。
- (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);
}
播放器是 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时,filePath为null所以我对它进行了评论。即使在最后如果循环转义为其他。我猜可能会出现问题。
请帮帮我。
答案 0 :(得分:2)
这段代码对我有用
@interface PlayAudioVc : UIViewController<AVAudioPlayerDelegate>
{
//for playback section
AVAudioPlayer *audioPlayer;
}
-(IBAction) playAudio{
NSError *error;
NSURL *url = [NSURL fileURLWithPath:self.audioName];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.delegate = self;
if (error)
NSLog(@"Error: %@", [error localizedDescription]);
else
[audioPlayer play];
}
self.audioName
包含音频文件的有效路径
答案 1 :(得分:1)
我在我的应用程序中这样做,它就像一个魅力
/**
* This method plays the 'pop' sound during the animation.
*/
-(void) playSound : (NSString *) fName : (NSString *) ext
{
NSString *path = [[NSBundle mainBundle] pathForResource : fName ofType :ext];
if ([[NSFileManager defaultManager] fileExistsAtPath : path])
{
NSURL *pathURL = [NSURL fileURLWithPath : path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
}
else
{
NSLog(@"error, file not found: %@", path);
}
}
您需要导入#import AVFoundation / AVFoundation.h和相应的框架。
转到项目摘要并在其下构建阶段选项卡,以复制捆绑资源。
在复制包资源下检查音频文件是否存在。
答案 2 :(得分:1)
试试这个,
包含 AVFoundtion.Framework 并将 AVFoundation / AVFoundation.h 导入ViewController,并在“.h”文件中添加 AVAudioPlayerDelegate 。
然后为 AVAudioPlayer 类
创建对象@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
NSString *path = [[NSBundle mainBundle]pathForResource:@"AIRTEL" ofType:@".MP3"];
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else {
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer play];
}
希望这会有所帮助。
答案 3 :(得分:0)
您的网址看起来很奇怪:
URL-&GT;文件://localhost/Users/ensignweb/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/CE146654-3D1B-4F60-B37D-825267FD6EFB/Library/http:/www.fundam
以 file:// 开头,包含 http:/