4

我正在使用下面的代码在 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
4

3 回答 3

5

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

  • 您确定要在目标中包含 beep_7.mp3 吗?
  • 你确定文件名是正确的吗?iOS 设备使用区分大小写的文件系统!
  • 你确定你的设备没有静音吗?
  • 您确定该文件是有效的、可工作的 MP3 文件吗?
于 2013-02-26T15:54:12.167 回答
2

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

于 2013-10-03T02:49:19.900 回答
0

你设置了 AudioSessionCategory 吗?

{
    NSError *setCategoryError = nil;
    BOOL success = [[AVAudioSession sharedInstance] setCategory: 
    avaudiosessioncategoryplayandrecorderror: &setCategoryError];
}
于 2013-07-29T03:29:46.733 回答