使用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