使用AVAudioPlayer播放本地音乐文件。
1,声明全局AudioPlayer变量:
- 
      
 
 @property
 
 
 (
 
 
 nonatomic
 
 
 ,
 
 
 strong
 
 
 )
 
 
 AVAudioPlayer
 
 
 *movePlayer ;
 
 
   
   
    2,初始化变量并播放:
   
- 
      
 
 NSString
 
 
 *tmp=[[NSBundle
 
 
 mainBundle
 
 
 ]
 
 
 .resourcePath
 
 
 stringByAppendingPathComponent
 
 
 :
 
 
 @”audio/move.mp3″
 
 
 ];
 
 
- 
      
 NSLog(
 
 @”%@”
 
 
 ,tmp);
 
 
- 
      
 
 NSURL
 
 
 *moveMP
 
 
 3
 
 
 =[NSURL
 
 
 fileURLWithPath
 
 
 :[[NSBundle
 
 
 mainBundle
 
 
 ]
 
 
 .resourcePath
 
 
 stringByAppendingPathComponent
 
 
 :
 
 
 @”audio/move.mp3″
 
 
 ]];
 
 
- 
      
 
 NSError
 
 
 *err=
 
 
 nil
 
 
 ;
 
 
- 
      
 
 self
 
 
 .movePlayer
 
 
 =[[AVAudioPlayer
 
 
 alloc
 
 
 ]
 
 
 initWithContentsOfURL
 
 
 :moveMP
 
 
 3
 
 
 error
 
 
 :&err];
 
 
- 
      
 
 self
 
 
 .movePlayer
 
 
 .volume
 
 
 =
 
 
 1
 
 
 .0
 
 
 ;
 
 
- 
      
 [
 
 self
 
 
 .movePlayer
 
 
 prepareToPlay
 
 
 ];
 
 
- 
      
 
 if
 
 
 (err!=
 
 
 nil
 
 
 ) {
 
 
- 
      
 NSLog(
 
 @”move player init error:%@”
 
 
 ,err);
 
 
- 
      
 }
 
 else
 
 
 {
 
 
- 
      
 [
 
 self
 
 
 .movePlayer
 
 
 play
 
 
 ];
 
 
   
注意:如果播放失败,请注意检查:
1,是否将AudioPlayer声明为全局变量;
2,本地音乐文件是否存在,路径是否正确;
    3,初始化NSURL时,是否使用的是  fileURLWithPath . (如果错用
    
     URLWithString 也是会造成初始化失败的
    
    )
   
转自:http://blog.csdn.net/jiajiayouba/article/details/44241751
我项目里本地音频文件播放无声音,是因为没有将AudioPlayer声明为全局变量。
 

