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