如何在使用avplayer播放音频时将音频缓存到本地

时间:2015-11-04 09:45:20

标签: ios swift avplayer avasset avurlasset

我使用AVPlayer在线播放音频,并希望在avplayer完成加载流时将数据/音频流保存到本地。

我将其实现如下:

    let fileUrl =  NSURL(string: strUrl)!
    let asset = AVURLAsset(URL: fileUrl)
    asset.resourceLoader.setDelegate(self, queue:dispatch_queue_create("AVARLDelegateDemo loader", nil))
    self.pendingRequests = [AVAssetResourceLoadingRequest]()

    asset.loadValuesAsynchronouslyForKeys(["playable"]){
        dispatch_async( dispatch_get_main_queue()){
            self.prepareToPlayAsset(asset, requestedKeys: ["playable"])
        }
    }


  func resourceLoader(resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
    .......
    return false
}

当网址为 http / https 时,它不会调用 resourceLoader(resourceLoader:AVAssetResourceLoader,shouldWaitForLoadingOfRequestedResource .... - ,当url是自定义的时候(例如: #39; test'),它调用 resourceLoader(resourceLoader:AVAssetResourceLoader,shouldWaitForLoadingOfRequestedResource ...

谁知道原因,resourceLoader不支持http / https吗?

3 个答案:

答案 0 :(得分:3)

当玩家不知道如何加载文件时,它会调用resourceLoader(resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource...方法。因为它知道如何加载URL,所以它不会调用此方法,因此您必须传递自定义URL。

答案 1 :(得分:2)

sample project and tutorial似乎详细说明了您正在寻找的内容。

上面的链接详细说明了设置自定义URL方案并设置AVAssetResourceLoaderDelegate以在加载时处理资源。初始化为:

NSURL *url = [NSURL URLWithString:@"customscheme://host/audio.mp3"];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
[asset.resourceLoader setDelegate:self queue:dispatch_get_main_queue()];

AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
[self addObserversForPlayerItem:item];

self.player = [AVPlayer playerWithPlayerItem:playerItem];
[self addObserversForPlayer];

委托方法的实现将类似于以下内容:

- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest*)loadingRequest{

    NSURL *resourceURL = [loadingRequest.request URL];

    if([resourceURL.scheme isEqualToString:@"customscheme"]){
        LSFilePlayerResourceLoader *loader = [self resourceLoaderForRequest:loadingRequest];

        if(loader==nil){
            loader = [[LSFilePlayerResourceLoader alloc] initWithResourceURL:resourceURL session:self.session];
            loader.delegate = self;
            [self.resourceLoaders setObject:loader forKey:[self keyForResourceLoaderWithURL:resourceURL]];
        }

        [loader addRequest:loadingRequest];

        return YES;
    }
    return NO;
}

- (void)resourceLoader:(AVAssetResourceLoader *)resourceLoader didCancelLoadingRequest:(AVAssetResourceLoadingRequest *)loadingRequest{

    LSFilePlayerResourceLoader *loader = [self resourceLoaderForRequest:loadingRequest];

    [loader removeRequest:loadingRequest];
}

其中LSFilePlayerResourceLoader和LSFilePlayerResourceLoader是用于处理接收数据的自定义对象(详见链接)。

答案 2 :(得分:0)

Here you can find full Swift solution based on native AVPlayerItem with custom loader.