美文网首页
iOS音频开发相关(三)播放 `AVAudioPlayer`

iOS音频开发相关(三)播放 `AVAudioPlayer`

作者: oceanfive | 来源:发表于2018-08-25 17:49 被阅读44次

    [TOC]

    AVAudioPlayer

    初始化方法

    /* all data must be in the form of an audio file understood by CoreAudio */
    - (nullable instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;
    - (nullable instancetype)initWithData:(NSData *)data error:(NSError **)outError;
    
    /* The file type hint is a constant defined in AVMediaFormat.h whose value is a UTI for a file format. e.g. AVFileTypeAIFF. */
    /* Sometimes the type of a file cannot be determined from the data, or it is actually corrupt. The file type hint tells the parser what kind of data to look for so that files which are not self identifying or possibly even corrupt can be successfully parsed. */
    - (nullable instancetype)initWithContentsOfURL:(NSURL *)url fileTypeHint:(NSString * __nullable)utiString error:(NSError **)outError NS_AVAILABLE(10_9, 7_0);
    - (nullable instancetype)initWithData:(NSData *)data fileTypeHint:(NSString * __nullable)utiString error:(NSError **)outError NS_AVAILABLE(10_9, 7_0);
    
    
    // 音频路径
    /* one of these properties will be non-nil based on the init... method used */
    @property(readonly, nullable) NSURL *url; /* returns nil if object was not created with a URL */
    
    // 音频二进制数据
    @property(readonly, nullable) NSData *data; /* returns nil if object was not created with a data object */
    
    • url: 文件路径,本地路径(项目中的,沙盒文件中的),不可以是网络音频路径;

    控制方法

    // 准备播放
    - (BOOL)prepareToPlay;  /* get ready to play the sound. happens automatically on play. */
    
    // 播放
    - (BOOL)play;           /* sound is played asynchronously. */
    
    // 在未来指定的时间time播放
    - (BOOL)playAtTime:(NSTimeInterval)time NS_AVAILABLE(10_7, 4_0); /* play a sound some time in the future. time is an absolute time based on and greater than deviceCurrentTime. */
    
    // 暂停
    - (void)pause;          /* pauses playback, but remains ready to play. */
    
    // 停止
    - (void)stop;           /* stops playback. no longer ready to play. */
    

    属性

    // 是否正在播放中
    @property(readonly, getter=isPlaying) BOOL playing; /* is it playing or not? */
    // 音频通道数目
    @property(readonly) NSUInteger numberOfChannels;
    // 音频时间长度
    @property(readonly) NSTimeInterval duration; /* the duration of the sound. */
    
    // 声道
    @property float pan NS_AVAILABLE(10_7, 4_0); /* set panning. -1.0 is left, 0.0 is center, 1.0 is right. */
    
    // 音量
    @property float volume; /* The volume for the sound. The nominal range is from 0.0 to 1.0. */
    - (void)setVolume:(float)volume fadeDuration:(NSTimeInterval)duration API_AVAILABLE(macos(10.12), ios(10.0), watchos(3.0), tvos(10.0)); /* fade to a new volume over a duration */
    
    // 播放速率控制
    // 允许改变播放速率
    @property BOOL enableRate NS_AVAILABLE(10_8, 5_0); /* You must set enableRate to YES for the rate property to take effect. You must set this before calling prepareToPlay. */
    // 播放速率
    @property float rate NS_AVAILABLE(10_8, 5_0); /* See enableRate. The playback rate for the sound. 1.0 is normal, 0.5 is half speed, 2.0 is double speed. */
    
    // 循环次数
    /* "numberOfLoops" is the number of times that the sound will return to the beginning upon reaching the end. 
    A value of zero means to play the sound just once.
    A value of one will result in playing the sound twice, and so on..
    Any negative number will loop indefinitely until stopped.
    */
    @property NSInteger numberOfLoops;
    
    • numberOfLoops: 循环次数;
      • 0表示播放1次
      • 1表示播放2次
      • 负数表示无限循环

    注意
    录音没有办法直接获取时间长度,播放可以获取duration

    代理

    /* the delegate will be sent messages from the AVAudioPlayerDelegate protocol */ 
    @property(assign, nullable) id<AVAudioPlayerDelegate> delegate;
    

    具体的代理方法

    @protocol AVAudioPlayerDelegate <NSObject>
    @optional 
    /* audioPlayerDidFinishPlaying:successfully: is called when a sound has finished playing. This method is NOT called if the player is stopped due to an interruption. */
    - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
    
    /* if an error occurs while decoding it will be reported to the delegate. */
    - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error;
    
    #if TARGET_OS_IPHONE
    
    /* AVAudioPlayer INTERRUPTION NOTIFICATIONS ARE DEPRECATED - Use AVAudioSession instead. */
    
    /* audioPlayerBeginInterruption: is called when the audio session has been interrupted while the player was playing. The player will have been paused. */
    - (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player NS_DEPRECATED_IOS(2_2, 8_0);
    
    /* audioPlayerEndInterruption:withOptions: is called when the audio session interruption has ended and this player had been interrupted while playing. */
    /* Currently the only flag is AVAudioSessionInterruptionFlags_ShouldResume. */
    - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags NS_DEPRECATED_IOS(6_0, 8_0);
    
    - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags NS_DEPRECATED_IOS(4_0, 6_0);
    
    /* audioPlayerEndInterruption: is called when the preferred method, audioPlayerEndInterruption:withFlags:, is not implemented. */
    - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player NS_DEPRECATED_IOS(2_2, 6_0);
    
    #endif // TARGET_OS_IPHONE
    
    @end
    
    • audioPlayerDidFinishPlaying:完成播放的代理方法

    播放时间

    /*  If the sound is playing, currentTime is the offset into the sound of the current playback position.  
    If the sound is not playing, currentTime is the offset into the sound where playing would start. */
    @property NSTimeInterval currentTime;
    
    /* returns the current time associated with the output device */
    @property(readonly) NSTimeInterval deviceCurrentTime NS_AVAILABLE(10_7, 4_0);
    
    • currentTime: 播放的当前处理
      • 播放中: 已经播放了的时间
      • 暂停/停止: 播放的开始时间;(停止的时候,可以设置currentTime0保证重新播放)
      • 播放完成: 为0

    处理:

    2018-08-24 15:12:21.761562+0800 HaiZiGuoParents[49694:13252539] 音频播放 暂停: 15.871837
    2018-08-24 15:12:36.143400+0800 HaiZiGuoParents[49694:13252539] 音频播放 暂停: 18.839637
    2018-08-24 15:12:45.701884+0800 HaiZiGuoParents[49694:13252539] 音频播放 停止: 22.414331
    

    分贝

    @property(getter=isMeteringEnabled) BOOL meteringEnabled; /* turns level metering on or off. default is off. */
    
    - (void)updateMeters; /* call to refresh meter values */
    
    - (float)peakPowerForChannel:(NSUInteger)channelNumber; /* returns peak power in decibels for a given channel */
    - (float)averagePowerForChannel:(NSUInteger)channelNumber; /* returns average power in decibels for a given channel */
    

    相关文章

      网友评论

          本文标题:iOS音频开发相关(三)播放 `AVAudioPlayer`

          本文链接:https://www.haomeiwen.com/subject/ipmgiftx.html