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 に答える