Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To Save Song in Document Directory #50

Open
shaikshabana opened this issue Apr 7, 2014 · 7 comments
Open

To Save Song in Document Directory #50

shaikshabana opened this issue Apr 7, 2014 · 7 comments

Comments

@shaikshabana
Copy link

Hi ,
How to store Cached song in Document Directory.
Thanks,
Shabana

@JacobSyndeo
Copy link

I too am interested in this.

@windhood
Copy link

windhood commented May 5, 2014

I also want this feature. But it seems not possible?
See the following links if they can help:
http://lists.apple.com/archives/coreaudio-api/2013/Aug/msg00020.html
http://stackoverflow.com/questions/6259095/caching-with-avplayer-and-avassetexportsession

@iBenjamin
Copy link

i got this work with a little research.here is my code.

+ (void)p_cacheAVPlayerItem:(AVPlayerItem *)avPlayerItem filePath:(NSString *)filePath {

    AVMutableComposition *composition = [AVMutableComposition composition];
    CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, avPlayerItem.asset.duration);
    AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionAudioTrack insertTimeRange:timeRange ofTrack:[[avPlayerItem.asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];

    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetAppleM4A];
    exportSession.outputFileType = AVFileTypeAppleM4A;
    NSURL *outputURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@.m4a", filePath]];
    exportSession.outputURL = outputURL;
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        switch(exportSession.status){
            case AVAssetExportSessionStatusExporting:
                NSLog(@"Exporting...");
                break;
            case AVAssetExportSessionStatusCompleted:
                NSLog(@"Export completed, wohooo!!");
                break;
            case AVAssetExportSessionStatusWaiting:
                NSLog(@"Waiting...");
                break;
            case AVAssetExportSessionStatusFailed:
                NSLog(@"Failed with error: %@", exportSession.error);
                break;
            default:
                NSLog(@"dont know what happened.");
                break;
        }
    }];
}

@JacobSyndeo
Copy link

Huh. If that works, it's a much less roundabout way than how I managed to implement it.
I switch the protocol from https:// to a custom one, and set one of my objects as a handler for that URL type. After switching out the AVPlayerItems' URLs to use this protocol instead of https://, it then can sit BELOW AVFoundation and intercept all the data it's loading, building files with that data.

@jaischeema
Copy link

@iBenjamin great work, been looking for something like that for a long time. I initially used what @JacobSyndeo did, it works but is not pretty.

I am seeing one issue though. It seems like the file that is saved is missing some parts, the music skips. Did you get that issue as well? If yes, how did you go about fixing this? If no, any ideas what might be causing this? Once again, great work figuring that out and thanks for your help in advance.

@kevinlin505
Copy link

@iBenjamin Any update on @jaischeema 's issue?

@JacobSyndeo
Copy link

@kevinlin505 I encountered that issue as well. I worked around it by only caching the file if it exactly matched the byte size of the original on the server. (My server tells this to my app in advance.)
I plan to migrate this to a true checksum system eventually, but I haven't had any issues with the byte count check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants