【问题标题】:Does not play mp3 file using AVAudioPlayer不使用 AVAudioPlayer 播放 mp3 文件
【发布时间】:2016-06-03 04:33:35
【问题描述】:

我想使用 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 "The operation couldn’t be completed. (OSStatus error 560557684.)" 提前致谢

【问题讨论】:

  • 尝试使用您设置为nil 的所有error 参数并检查这些方法的相应返回值。
  • 为什么需要检查 [self.playerAudio prepareToPlay] ?你不能删除这个 if 条件吗?
  • 您可以尝试将NSError *activationError = nil; 添加到self.playerAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&activationError];[[AVAudioSession sharedInstance] setActive: YES error:&activationError]; 检查错误吗?
  • @AadilKeshwani:检查我更新的代码。
  • 你能在 if 条件中设置断点并检查 error 是否仍然为 nil 吗?

标签: ios objective-c avaudioplayer avaudiosession


【解决方案1】:

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

    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];
    }

【讨论】:

  • 您的错误检查都不正确。例如,它应该是:if (![[AVAudioSession sharedInstance] setActive: YES error: &error]) { NSLog(@"Can't set active: %@", error);。永远不要检查是否设置了错误。查看方法的返回值以确定它是成功还是失败。如果失败则记录错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多