我在我的应用中使用Firebase存储.
这是我的代码.
UploadRecordedAudio.h中使用的代码:
#import <UIKit/UIKit.h>#import <AVFoundation/AVFoundation.h>@import FirebaseStorage;@import FirebaseAuth;@interface UploadRecordedAudio : UIVIEwController<AVAudioRecorderDelegate,AVAudioPlayerDelegate>- (IBAction)btnRecord:(ID)sender;@property (weak,nonatomic) IBOutlet UIbutton *record;@property (weak,nonatomic) IBOutlet UIbutton *stop;- (IBAction)btnStop:(ID)sender;@property (weak,nonatomic) IBOutlet UIbutton *play;- (IBAction)btnPlay:(ID)sender;@property (strong,nonatomic) Firstorage *storage;@property (strong,nonatomic) FirstorageReference *storageRef;@end
此代码用于UploadRecordedAudio.m:
- (voID)vIEwDIDLoad { [super vIEwDIDLoad]; // disable Stop/Play button when application launches [_play setEnabled:NO]; [_stop setEnabled:NO]; NSArray *pathComponents = [NSArray arrayWithObjects:[NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES)lastObject],@"MyAudioMemo.m4a",nil]; NSURL *outputfileURL = [NSURL fileURLWithPathComponents:pathComponents]; //NSLog(@"HERE: %@",pathComponents); //Setup Audio Session AVAudioSession *session = [AVAudioSession sharedInstance]; [session setcategory:AVAudioSessioncategoryPlayAndRecord error:nil]; //define the Recorder Settings... NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init]; [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey]; [recordSetting setValue:[NSNumber numberWithfloat:44100.0] forKey:AVSampleRateKey]; [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; //Initiate and Prepare the Recorder recorder = [[AVAudioRecorder alloc]initWithURL:outputfileURL settings:recordSetting error:NulL]; recorder.delegate = self; recorder.meteringEnabled = YES; [recorder preparetoRecord]; NSData *aData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPathComponents:pathComponents]]; NSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,YES); Nsstring *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder Nsstring *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"]; NSLog(@"%@",dataPath); [aData writetofile:dataPath atomically:YES];}- (IBAction)btnRecord:(ID)sender{ //Stop the audio player before Recording... if (player.playing) { [player stop]; } if (!recorder.recording) { AVAudioSession *session = [AVAudioSession sharedInstance]; [session setActive:YES error:nil]; //Start Recording [recorder record]; [_record setTitle:@"Pause Recording" forState:UIControlStatenormal]; } else { [recorder pause]; [_record setTitle:@"Resume Recording" forState:UIControlStatenormal]; } [_stop setEnabled:YES]; [_play setEnabled:NO];}- (IBAction)btnPlay:(ID)sender{ if (!recorder.recording){ player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil]; [player setDelegate:self]; [player play]; }}- (voID) audioRecorderDIDFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOol)flag{ [_stop setEnabled:NO]; [_play setEnabled:YES];}- (voID) audioPlayerDIDFinishPlaying:(AVAudioPlayer *)player successfully:(BOol)flag{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Done" message:@"Finish playing the recording!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:action]; [self presentVIEwController:alert animated:YES completion:nil];}- (IBAction)btnStop:(ID)sender{ [recorder stop]; AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setActive:NO error:nil];}@endPlease help me to solve this??? Thanks in advance.解决方法 您可以在这样的方法中将文件存储在您的设备上
- (IBAction) record {NSError *error;// Recording settingsNSMutableDictionary *settings = [NSMutableDictionary dictionary];[settings setValue: [NSNumber numberWithInt:kAudioFormatlinearPCM] forKey:AVFormatIDKey];[settings setValue: [NSNumber numberWithfloat:8000.0] forKey:AVSampleRateKey];[settings setValue: [NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];[settings setValue: [NSNumber numberWithInt:16] forKey:AVlinearPCMBitDepthKey];[settings setValue: [NSNumber numberWithBool:NO] forKey:AVlinearPCMIsBigEndianKey];[settings setValue: [NSNumber numberWithBool:NO] forKey:AVlinearPCMIsfloatKey];[settings setValue: [NSNumber numberWithInt: AVAudioQualityMax] forKey:AVEncoderAudioQualityKey];NSArray *searchPaths =NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,YES);Nsstring *documentPath_ = [searchPaths objectAtIndex: 0];Nsstring *pathToSave = [documentPath_ stringByAppendingPathComponent:[self dateString]];// file URLNSURL *url = [NSURL fileURLWithPath:pathToSave];//filePATH];//Save recording path to preferencesNSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];[prefs setURL:url forKey:@"Test1"];[prefs synchronize];// Create recorderrecorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];[recorder preparetoRecord];[recorder record]; }
并像这样使用它
//Load recording path from preferences NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; temporaryRecfile = [prefs URLForKey:@"Test1"]; player = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecfile error:nil];
像这样将音频文件转换为NSData
NSData *audioData = [[NSData alloc] initWithContentsOffile:filePath];
并像这样上传到firebase
// Obj-C NSData *data = ... FirstorageUploadTask *uploadTask = [riversRef putData:data Metadata:nil completion:^(FirstorageMetadata *Metadata,NSError *error) { if (error != nil) {// Uh-oh,an error occurred! } else { // Succeed } }];
从firebase下载文件
// Create a reference to the file you want to download FirstorageReference *islandRef = [storageRef child:@"images/island.jpg"]; // Create local filesystem URL NSURL *localURL = [NSURL URLWithString:@"path/to/image"]; // Download to the local filesystem FirstorageDownloadTask *downloadTask = [islandRef writetofile:localURL completion:^(NSURL *URL,NSError *error){ if (error != nil) { // Uh-oh,an error occurred! } else { // Local file URL for "images/island.jpg" is returned } }];
链接如何在obj C中的firebase上创建存储
更新:日期字符串方法
and added a new dateString method to VIEwController.m: - (Nsstring *) dateString {// return a formatted string for a file nameNSDateFormatter *formatter = [[NSDateFormatter alloc] init];formatter.dateFormat = @"ddMMMYY_hhmmssa";return [[formatter stringFromDate:[NSDate date]] stringByAppendingString:@".aif"]; }
Firebase storage
总结以上是内存溢出为你收集整理的ios – 如何在本地录制后保存音频文件或在目标c中录制firebase?全部内容,希望文章能够帮你解决ios – 如何在本地录制后保存音频文件或在目标c中录制firebase?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)