gpt4 book ai didi

ios - 不使用 AVAudioPlayer 播放 mp3 文件

转载 作者:行者123 更新时间:2023-11-29 00:56:00 29 4
gpt4 key购买 nike

我想用 AVAudioPlayer 播放歌曲。但这有时不起作用。我不明白是什么问题。我正在使用下面的代码从本地播放 mp3 文件。

NSString *soundFile;
soundFile = [[NSBundle mainBundle] pathForResource:@"notification_sound" ofType:@"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:soundFile];
NSError *error=nil;
self.playerAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
[[AVAudioSession sharedInstance] setActive: YES error: &error];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self.playerAudio setDelegate:self];
NSLog(@"Error:%@",error);
self.playerAudio.numberOfLoops = -1;
self.playerAudio.volume=1;
if([self.playerAudio prepareToPlay])
{
NSLog(@"Prepare to play successfully");
[self.playerAudio play];
}

if([self.playerAudio prepareToPlay]) 这个条件永远不会满足为什么会这样,请帮我解决这个问题。

我收到以下错误:Error:Error Domain=NSOSStatusErrorDomain Code=560557684 “操作无法完成。(OSStatus 错误 560557684。)”提前致谢

最佳答案

这就是解决方案,而且效果很好:)

    NSError *error=nil,*sessionError = nil;
NSString * soundFile = [[NSBundle mainBundle] pathForResource:@"notification_sound" ofType:@"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:soundFile];

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers | AVAudioSessionCategoryOptionDuckOthers
error:&sessionError];
if (sessionError) {
NSLog(@"ERROR: setCategory %@", [sessionError localizedDescription]);
}
[[AVAudioSession sharedInstance] setActive: YES error: &error];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

self.playerAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];

[[AVAudioSession sharedInstance] setActive: YES error: &error];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

[self.playerAudio setDelegate:self];
self.playerAudio.numberOfLoops = -1;
self.playerAudio.volume=1;
if([self.playerAudio prepareToPlay])
{
[self.playerAudio play];
}

关于ios - 不使用 AVAudioPlayer 播放 mp3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37605993/

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