Skip to content

Commit

Permalink
Use NSCachesDirectory... (#141)
Browse files Browse the repository at this point in the history
instead of NSTemporaryDirectory

NSCachesDirectory is better suited for this use case -- can be cleared when storage is running low, but not cleared arbitrarily / on restart :) 

NSCachesDirectory is the directory that the officially supported `path_provider` package returns upon calling it's `getTemporaryDirectory()` method.

path_provider Packaged referenced here:
https://pub.dev/packages/path_provider
https://flutter.dev/docs/cookbook/persistence/reading-writing-files
  • Loading branch information
kltdwrds authored and hyochan committed Oct 22, 2019
1 parent 0351042 commit 6fda301
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions ios/Classes/FlutterSoundPlugin.m
@@ -1,6 +1,11 @@
#import "FlutterSoundPlugin.h"
#import <AVFoundation/AVFoundation.h>

NSString* GetDirectoryOfType(NSSearchPathDirectory dir) {
NSArray* paths = NSSearchPathForDirectoriesInDomains(dir, NSUserDomainMask, YES);
return [paths.firstObject stringByAppendingString:@"/"];
}

@implementation FlutterSoundPlugin{
NSURL *audioFileURL;
AVAudioRecorder *audioRecorder;
Expand Down Expand Up @@ -181,9 +186,10 @@ - (void)setDbLevelEnabled:(BOOL)enabled result: (FlutterResult)result {

- (void)startRecorder :(NSString*)path :(NSNumber*)numChannels :(NSNumber*)sampleRate :(NSNumber*)iosQuality :(NSNumber*)bitRate result: (FlutterResult)result {
if ([path class] == [NSNull class]) {
audioFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"sound.m4a"]];

audioFileURL = [NSURL fileURLWithPath:[GetDirectoryOfType(NSCachesDirectory) stringByAppendingString:@"sound.m4a"]];
} else {
audioFileURL = [NSURL fileURLWithPath: [NSTemporaryDirectory() stringByAppendingString:path]];
audioFileURL = [NSURL fileURLWithPath: [GetDirectoryOfType(NSCachesDirectory) stringByAppendingString:path]];
}

NSMutableDictionary *audioSettings = [NSMutableDictionary dictionaryWithObjectsAndKeys:
Expand Down Expand Up @@ -246,7 +252,7 @@ - (void)stopRecorder:(FlutterResult)result {
- (void)startPlayer:(NSString*)path result: (FlutterResult)result {
bool isRemote = false;
if ([path class] == [NSNull class]) {
audioFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"sound.m4a"]];
audioFileURL = [NSURL fileURLWithPath:[GetDirectoryOfType(NSCachesDirectory) stringByAppendingString:@"sound.m4a"]];
} else {
NSURL *remoteUrl = [NSURL URLWithString:path];
if(remoteUrl && remoteUrl.scheme && remoteUrl.host){
Expand Down

0 comments on commit 6fda301

Please sign in to comment.