Skip to content

A audio player save file after buffer finish, and preload next file.

Notifications You must be signed in to change notification settings

dminoror/DLCachePlayer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 

Repository files navigation

DLCachePlayer


DLCachePlayer 提供播放音樂,並同時將檔案下載到本地的功能。

Features

  • 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.

How to use

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
{
    
}

ProgressSlider

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];
    }
}

Other delegate

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;
    }
}

Demo

Basic remote media play.

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.

Licenses

All source code is licensed under the MIT License.

About

A audio player save file after buffer finish, and preload next file.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published