DLCachePlayer 提供播放音樂,並同時將檔案下載到本地的功能。
- Support remote and local media URL.
- Buffer whole current playitem, and preload next playitem.
- Totaly seekable.
- Spoport .mp3 file.
- No playlist or queue, you can use own playlist structure.
Implement DLCachePlayerDataDelegate
and DLCachePlayerStateDelegate
in your media module.
#import "DLCachePlayer.h"
@interface ViewController : UIViewController<DLCachePlayerDataDelegate, DLCachePlayerStateDelegate>
Set delegate for DLCachePlayer
- (void)viewDidLoad
{
[DLCachePlayer sharedInstance].delegate = self;
}
Implement playerGetCurrentPlayURL
and playerGetPreloadPlayURL
, return your media URL in block.
- (void)playerGetCurrentPlayURL:(AVPlayerItem * (^)(NSURL * url, BOOL cache))block
{
NSURL * playURL = @"your URL";
block(playURL, YES);
}
Call resetAndPlay
when you want to play.
[[DLCachePlayer sharedInstance] resetAndPlay];
Player will call playerGetCurrentPlayURL
, play the URL you return.
After buffer finish, you can get media data in playerDidFinishCache
, save it so you can play it locally next time.
- (void)playerDidFinishCache:(AVPlayerItem *)playerItem isCurrent:(BOOL)isCurrent data:(NSData *)data
{
}
If you want to show current buffer, DLProgressSlider
maybe useful.
Implement playerCacheProgress
and pass variables to update it's buffer progress.<\br>
- (void)playerCacheProgress:(AVPlayerItem *)playerItem isCurrent:(BOOL)isCurrent tasks:(NSMutableArray *)tasks totalBytes:(NSUInteger)totalBytes
{
if (isCurrent)
{
[progressBar updateProgress:tasks totalBytes:totalBytes];
}
}
Called when player current playitem changed, may update your UI here.
- (void)playerPlayerItemChanged:(AVPlayerItem *)playerItem
Called when current playitem play finished, should increase your playlist index and call resetAndPlay
here.
- (void)playerDidReachEnd:(AVPlayerItem *)playerItem
Called when player state changed, may update your UI when Stop
, Playing
and Pause
.
- (void)playerDidPlayStateChanged:(DLCachePlayerPlayState)state
{
switch (state)
{
case DLCachePlayerPlayStateStop:
break;
case DLCachePlayerPlayStateInit:
break;
case DLCachePlayerPlayStateReady:
break;
case DLCachePlayerPlayStatePlaying:
break;
case DLCachePlayerPlayStatePause:
break;
}
}
How preload worked. When next clicked, some progress already exist.
How seek worked. Player buffer progress after current time first.
See more in Demo project.
All source code is licensed under the MIT License.