gpt4 book ai didi

iphone - 音频文件不在 iPhone/iOS 中播放

转载 作者:行者123 更新时间:2023-11-28 19:08:43 26 4
gpt4 key购买 nike

我需要播放从 json 中获取的音频文件 (mp3)在表格 View 中,单击行时相应的歌曲路径将传递到另一个 UIView..在加载 View 时它应该播放。

我知道互联网上有很多类似的问题,我尝试了很多方法都不适合我。

这是代码片段。

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

其中playerAVAudioPlayer类的对象

   @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,所以我对其进行了评论。即使在结束时 If Loop 转义到 else。我猜可能是问题所在。

请帮帮我。

最佳答案

这段代码对我有用

@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 包含音频文件的有效路径

关于iphone - 音频文件不在 iPhone/iOS 中播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17802478/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com