【问题标题】:AVAudioPlayer: MP3 file not playing on device, but works in Simulator (iOS 6)AVAudioPlayer:MP3 文件不在设备上播放,但在模拟器中工作(iOS 6)
【发布时间】:2013-12-20 20:05:10
【问题描述】:

我正在使用下面的代码在 iOS 6 上使用 AVAudioPlayer 播放 MP3 文件。该文件似乎正在播放,但设备上没有声音输出。 不过,它在 iOS 模拟器中运行良好。

文件 .h:

#import "UIKit/UIKit.h"
#import "AVFoundation/AVFoundation.h"

@interface ViewController : UIViewController <AVAudioPlayerDelegate>

@property (strong, nonatomic) AVAudioPlayer *player;

@end

文件 .m:

#import "ViewController.h"

@implementation ViewController

@synthesize player;

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"beep_7" ofType:@"mp3"];
    NSLog(@"Audio path: %@", soundFilePath);

    NSError *error;
    player =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFilePath] error:&error];

    if (error) {
        NSLog(@"Error in audioPlayer: %@",[error localizedDescription]);
    } 
    else {
        [player setDelegate:self];
        [player setNumberOfLoops:3]; //just to make sure it is playing my file several times
        player.volume = 1.0f;

        if([player prepareToPlay]) //It is always ready to play
            NSLog(@"It is ready to play");
        else
            NSLog(@"It is NOT ready to play ");

        if([player play]) //It is always playing
            NSLog(@"It should be playing");
        else
            NSLog(@"An error happened");
    }
}
@end

【问题讨论】:

  • 如果你实现了委托方法audioPlayerDecodeErrorDidOccur:error:,你看到什么错误了吗?
  • 我已经实现了那个方法,它没有出现任何日志... -(void)audioPlayerDecodeErrorDidOccur: (AVAudioPlayer *)player error:(NSError *)error { NSLog(@"audioPlayerDecodeErrorDidOccur -> Error在音频播放器中:%@",[错误本地化描述]); }
  • 您似乎正在设备上进行测试。它可以在 iOS 模拟器中运行吗?
  • 确实如此。它可以在 iOS 模拟器中运行,但在我的 iPhone 中却不行……我的 iPhone 可能有什么问题?
  • 我犯了几个错误。首先,我的文件已损坏,其次,我关闭了静音按钮。无论如何谢谢:)

标签: iphone ios objective-c avaudioplayer


【解决方案1】:

你发布的代码对我来说很好用。

  • 您确定要在目标中包含 beep_7.mp3 吗?
  • 您确定文件名正确吗? iOS 设备使用区分大小写的文件系统!
  • 您确定您的设备没有静音吗?
  • 您确定该文件是有效的、可工作的 MP3 文件吗?

【讨论】:

  • 我正在测试两个不同的 mp3 文件。我已经将这些文件与我的 iPhone 同步,并且我在那里玩过并且它工作正常。所以,文件工作,我的设备没有静音。关于如果我已将这些文件包含到我的目标中,我所做的独特的事情就是将我的项目拖到支持文件中......我还缺少其他东西吗?
  • 我刚刚检查了我的目标->构建阶段,两个文件都在那里。
  • 啊,我的设备静音了。很好的答案涵盖了所有问题。
【解决方案2】:

检查您设备的静音模式是打开还是关闭? 我曾经遇到过和你一样的问题,关掉静音模式后,都解决了!

【讨论】:

    【解决方案3】:

    你设置了 AudioSessionCategory 吗?

    {
        NSError *setCategoryError = nil;
        BOOL success = [[AVAudioSession sharedInstance] setCategory: 
        avaudiosessioncategoryplayandrecorderror: &setCategoryError];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      • 2022-08-10
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多