AVAudioPlayer

要使用 AVAudioPlayer 的对象播放文件,你只需为其指定一个音频文件并设定一个实现了AVAudioPlayerDelegate 协议的 delegate 对象。

初始化;
1:initWithData:error:他使用一个指向内存中一些音频数据的NSData对象. 这种形式对于已经把音频数据下载到缓冲区的情形很有用.
initWithContentsOfURL:error NSURL 它只能从file://格式的URL装入音频数据不支持流式音频及HTTP流和网络流

/获取文件路径
NSString *soundFilePath =  [[NSBundle mainBundle]pathForResource:@"background"  ofType:@"wav" ];
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
    AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil ];
3属性:
     Bool     meteringEnabled 可以监控音量变化
     double     volume=1.0;//设置音量
      setDelegate设置代理
  这两个属性可以监控音频的回放进度
double f=player.duration//音乐的播放总时间
double      currentTime //当前播放的时间
  bool    playing//判断是否正在播放
integer   numberOfLoops ;//设置循环播放的此次   
方法:
-(double) averagePowerForChannel:0//平均音量
-(double) peakPowerForChannel:0//最高音量
-(void) updateMeters //更新音量
-(void)prepareToPlay];//准备播放
-(void) play;//播放
-(void) pause//暂停;
-(void)stop//停止

4.委托方法:
//音频结束时调用
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {}
//音频产生错误时调用
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error {}

猜你喜欢

转载自lizhuang.iteye.com/blog/2102579
今日推荐