使用AVAudioPlayer播放音乐文件无声音


使用AVAudioPlayer播放本地音乐文件。

1、声明全局AudioPlayer变量:

@property(nonatomic,strong)AVAudioPlayer *movePlayer ; 
 
2、初始化变量并播放:
NSString *tmp=[[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"a.mp3"]; 
// NSString * tmp = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"mp3"]; 
NSURL *medioUrl=[NSURL fileURLWithPath:[[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"audio/move.mp3"]];  
NSError *err=nil;  
self.movePlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:medioUrl error:&err];  
self.movePlayer.volume=1.0;  
[ self.movePlayer prepareToPlay];

if (err!=nil) {  

NSLog(@"move player init error:%@",err);  

}else {  

 [self.movePlayer play]; 

}

3、AVAudioPlayer 模拟器有声音,真机耳机有声音,扬音器没有声音解决办法

在创建:AVAudioPlayer前,加入以下代码

AVAudioSession *audioSession = [AVAudioSession sharedInstance];  

[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];  

[audioSession setActive:YES error:nil]; 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM