AVAudioPlayer可以在iOS模拟器上玩,但不能在iPhone上玩

AVAudioPlayer可以在iOS模拟器上玩,但不能在iPhone上玩,iphone,ios,objective-c,avaudioplayer,Iphone,Ios,Objective C,Avaudioplayer,我正在开发一个必须播放mp3文件的应用程序。对于这个问题,我正在使用AVAudioPlayer,这是我从其他帖子中读到的指示。 但我的问题是,这段代码(我附在下面)可以在iOS模拟器上运行,但当我尝试在iPhone上使用它时,它就不起作用了。。。 我希望你们中的任何人都能在这个问题上帮助我 File.h: #import "UIKit/UIKit.h" #import "AVFoundation/AVFoundation.h" @interface ViewController : UIVie

我正在开发一个必须播放mp3文件的应用程序。对于这个问题,我正在使用AVAudioPlayer,这是我从其他帖子中读到的指示。 但我的问题是,这段代码(我附在下面)可以在iOS模拟器上运行,但当我尝试在iPhone上使用它时,它就不起作用了。。。 我希望你们中的任何人都能在这个问题上帮助我

File.h:

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

@interface ViewController : UIViewController <AVAudioPlayerDelegate>

@property (strong, nonatomic) AVAudioPlayer *player;

@end
#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;
    self.player =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFilePath] error:&error];

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

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

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

-(void)audioPlayerDecodeErrorDidOccur: (AVAudioPlayer *)player error:(NSError *)error {
    NSLog(@"audioPlayerDecodeErrorDidOccur -> Error in audioPlayer: %@",[error localizedDescription]);
}
-(void)audioPlayerDidFinishPlaying: (AVAudioPlayer *)player successfully:(BOOL)flag{
    NSLog(@"audioPlayerDidFinishPlaying");
}
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{
    NSLog(@"audioPlayerBeginInterruption");
}
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player{
    NSLog(@"audioPlayerEndInterruption");
}

@end

当音频(或图像)在模拟器中工作(显示)而不是在设备上时,首先要检查的是您试图访问的音频文件的拼写

由于Mac OSX不区分大小写,但iOS区分大小写,这意味着代码中拼写为
image.png
并作为
image.png
访问的图像将显示在模拟器中,但不会显示在iPhone上

另一件要检查的事情可能是你不小心按下了iPhone上的物理静音按钮。(上周发生在我身上)


希望有帮助

当音频(或图像)在模拟器中工作(显示)而不是在设备上时,首先要检查的是您试图访问的音频文件的拼写

由于Mac OSX不区分大小写,但iOS区分大小写,这意味着代码中拼写为
image.png
并作为
image.png
访问的图像将显示在模拟器中,但不会显示在iPhone上

另一件要检查的事情可能是你不小心按下了iPhone上的物理静音按钮。(上周发生在我身上)


希望有帮助

谢谢,我打了物理静音按钮,但我没有意识到这一点!谢谢,汉克斯,我撞上了身体上的哑巴波顿,我没有意识到这一点!谢谢