在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中加入这句话
[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback error:nil];
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString * resourcePath = [[NSBundle mainBundle] pathForResource:@"SIMS_BreathingSpace_100215" ofType:@"mp3"];
// NSString * resourcePath = [[NSBundle mainBundle] pathForResource:@"time" ofType:@"mp3"];
// NSString * resourcePath = [[NSBundle mainBundle] pathForResource:@"time" ofType:@"mp3"];
NSData *data =[NSData dataWithContentsOfFile:resourcePath];
audioPlayer = [[AVAudioPlayer alloc]initWithData:data error:nil];
audioPlayer.delegate = self;
audioPlayer.volume = 0.8f;
[audioPlayer prepareToPlay];
}
-(IBAction)play:(id)sender
{
[audioPlayer play];
}
stackover上有解释,看的不大懂http://stackoverflow.com/questions/4640880/mp3-playing-using-avaudioplayer-not-working-on-device点击打开链接