我正在构建一个必须播放mp3文件的应用程序。对于那个问题,我正在使用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
答案 0 :(得分:2)
首先检查音频(或图像)何时在模拟器中工作(显示)而不是在设备上是你试图访问的音频文件的拼写。
由于Mac OSX不区分大小写,但iOS意味着代码拼写iMage.png
并在代码中以image.png
访问的图像将显示在模拟器中但不会显示在iPhone上。
要检查的另一件事可能是您不小心碰到了iPhone上的物理静音按钮。 (上周发生在我身上)
希望它有所帮助!