2013-02-27 108 views
0

我正在构建一个必须播放mp3文件的应用程序。对于这个问题,我使用AVAudioPlayer后,我一直在阅读其他帖子的迹象。 但我的问题是,代码(我已附上下图)适用于iOS模拟器,但是当我尝试在我的iPhone中使用它时,它不起作用... 我希望你们中的任何人都可以帮助我问题。AVAudioPlayer在iOS模拟器中播放,但不在iPhone上

文件的.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; 
    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 

回答

2

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

因为Mac OSX不区分大小写,但iOS的是,这意味着图像拼iMage.pngimage.png在你的代码访问将在模拟器,但不是在你的iPhone显示。

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

希望它有帮助!

+3

谢谢,我打了物理静音botton,我没有意识到这一点!谢谢 – rubrin 2013-02-27 10:08:43

相关问题