我在我的应用程序中使用录音功能,但不知道在哪里保存此文件以及在哪里可以看到此文件?还告诉我如何在Firebase存储中存储此文件?
我在我的应用中使用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, NSUserDomainMask, 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];
}
@end
Please help me to solve this??? Thanks in advance.
最佳答案 您可以在这样的方法中将文件存储在您的设备上
- (IBAction) record
{
NSError *error;
// Recording settings
NSMutableDictionary *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, NSUserDomainMask, YES);
NSString *documentPath_ = [searchPaths objectAtIndex: 0];
NSString *pathToSave = [documentPath_ stringByAppendingPathComponent:[self dateString]];
// File URL
NSURL *url = [NSURL fileURLWithPath:pathToSave];//FILEPATH];
//Save recording path to preferences
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setURL:url forKey:@"Test1"];
[prefs synchronize];
// Create recorder
recorder = [[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 name
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"ddMMMYY_hhmmssa";
return [[formatter stringFromDate:[NSDate date]] stringByAppendingString:@".aif"];
}