iOS 音频开发之JKStreamingKit

  最近公司进行了音视频相关项目的开发,虽然之前也进行过相关知识的储备工作,但是毕竟有限,刚好就趁着这个机会,认真的学习下音视频开发。
  下面来介绍下StreamingKit这个开源库,它主要是一个音频播放的一个开源库,整体使用下来感觉还挺不错,就给大家分享一下。

STKAudioPlayerDelegate 介绍

@protocol STKAudioPlayerDelegate <NSObject>

/// Raised when an item has started playing
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer didStartPlayingQueueItemId:(NSObject*)queueItemId;
/// Raised when an item has finished buffering (may or may not be the currently playing item)
/// This event may be raised multiple times for the same item if seek is invoked on the player
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer didFinishBufferingSourceWithQueueItemId:(NSObject*)queueItemId;
/// Raised when the state of the player has changed
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer stateChanged:(STKAudioPlayerState)state previousState:(STKAudioPlayerState)previousState;
/// Raised when an item has finished playing
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer didFinishPlayingQueueItemId:(NSObject*)queueItemId withReason:(STKAudioPlayerStopReason)stopReason andProgress:(double)progress andDuration:(double)duration;
/// Raised when an unexpected and possibly unrecoverable error has occured (usually best to recreate the STKAudioPlauyer)
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer unexpectedError:(STKAudioPlayerErrorCode)errorCode;
@optional
/// Optionally implemented to get logging information from the STKAudioPlayer (used internally for debugging)
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer logInfo:(NSString*)line;
/// Raised when items queued items are cleared (usually because of a call to play, setDataSource or stop)
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer didCancelQueuedItems:(NSArray*)queuedItems;

/// Raised when datasource read stream metadata
-(void) audioPlayer:(STKAudioPlayer*)audioPlayer didReadStreamMetadata:(NSDictionary*)dictionary;

@end

上面这些是STKAudioPlayer播放时需要实现的代理方法。主要就是播放时状态变更,播放完成,取消的一个代理方法。

STKAudioPlayer常见使用方法

由于STKAudioPlayer里面的方法也挺多的,我这边暂时就是用了一些常见的方法,具体如下:

-(void) play:(NSString*)urlString;

/// Plays an item from the given URL (all pending queued items are removed)
-(void) play:(NSString*)urlString withQueueItemID:(NSObject*)queueItemId;

/// Plays an item from the given URL (all pending queued items are removed)
/// The NSURL is used as the queue item ID
-(void) playURL:(NSURL*)url;

/// Plays an item from the given URL (all pending queued items are removed)
-(void) playURL:(NSURL*)url withQueueItemID:(NSObject*)queueItemId;

-(void) pause;

/// Resumes playback from pause
-(void) resume;

/// Stops playback of the current file, flushes all the buffers and removes any pending queued items
-(void) stop;

使用起来也非常的简单。简单初始化一下就好了,具体使用如下:

- (STKAudioPlayer *)audioPlayer{
    if (!_audioPlayer) {
        _audioPlayer = [[STKAudioPlayer alloc] init];
        _audioPlayer.delegate = self;
    }
    return _audioPlayer;
}

  由于自己的项目主要是进行背景音乐的播放,在播放音乐时要求音量较小,StreamingKit也有调节音量的方法,我试了一下没有效果,不知道是不是自己的使用方法不对,项目时间比较紧张,我这边就在老的开源库的基础上新增加了一个调节音量的方法,-(instancetype) initWithVolume:(CGFloat)volume
新的开源库的名字如下:JKStreamingKit,开源地址:https://github.com/xindizhiyin2014/StreamingKit
可以通过pod 'JKStreamingKit' 导入项目

更多优质文章,可以微信扫码关注:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/HHL110120/article/details/88059177