为什么我的录音无法播放? (iPhone SDK)

发布于 2024-08-22 21:23:46 字数 5739 浏览 6 评论 0原文

我正在编写一个应用程序,可以让我录制门铃声音并在用户按下按钮时看到的门铃上回放它们。 我的应用程序似乎记录并保存了文件(根据 iPhone 模拟器,文件在那里(在文档中),因为我在应用程序委托中创建了目录))。 但是播放不起作用。 (顺便说一句,记录属性返回 TRUE。)

更新:这是整个项目的 zip

有人可以帮我看看出了什么问题吗?

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.layer.cornerRadius = 8;
    [actionButton setBackgroundImage:[[UIImage imageNamed:@"biggreenbutton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
    NSFileManager *nsf = [NSFileManager defaultManager];
    NSArray *allDocs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [allDocs objectAtIndex:0];
    NSString *savedDBells = [path stringByAppendingPathComponent:@"Custom Doorbells"];
    NSString *nextFile;
    for (NSInteger i = 1; i < 100; i++){
        if (![nsf fileExistsAtPath:[savedDBells stringByAppendingPathComponent:[NSString stringWithFormat:@"d%i%@",i,@".caf"]]]){
            nextFile = [savedDBells stringByAppendingPathComponent:[NSString stringWithFormat:@"d%i%@",i,@".caf"]];
            break;
        }
    }
    NSURL *url = [[NSURL alloc] initFileURLWithPath:nextFile];
    NSDictionary *setup = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:kAudioFormatMPEG4AAC],AVFormatIDKey,
                           [NSNumber numberWithInt:44100.0],AVSampleRateKey,
                           [NSNumber numberWithInt: 2],AVNumberOfChannelsKey,
                           [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
                           [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
                           [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                           nil];

    NSError *err;
    NSLog(@"%@",url);
    recorder = [[AVAudioRecorder alloc] initWithURL:url settings:setup error:&err];
    [setup release];
    [url release];
    //Gradient code
    /*
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.view.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
    [self.view.layer insertSublayer:gradient atIndex:0];
     */
}

-(IBAction) actionButtonPressed: (id) sender{
    NSLog(@"%@",actionButton.titleLabel.text);
    if ([actionButton.titleLabel.text isEqual:@"Finish Recording"]){
        BOOL test = recorder.recording;
        NSLog(@"Recording property = %i",test);
        [upCounter invalidate];
        [recorder stop];

        [self dismissModalViewControllerAnimated:YES];
    }
    if ([actionButton.titleLabel.text isEqual:@"Begin Recording"]){
        upCounter = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:upCounter forMode:NSDefaultRunLoopMode];
        [actionButton setTitle:@"Finish Recording" forState:UIControlStateNormal];
        [actionButton setBackgroundImage:[[UIImage imageNamed:@"bigredbutton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
        NSLog(@"Recorder record = %i",[recorder record]);
    }

}

在另一个视图控制器中:

-(IBAction) recordingButtonPressed:(id) sender{
    AVAudioSession *avs = [AVAudioSession sharedInstance];
    //Test for written folder
    /*NSFileManager *fileMan = [NSFileManager defaultManager];
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    */

    if (avs.inputIsAvailable){
        NSString *recordedFile;

        RecordingViewController *recorderVC = [[RecordingViewController alloc] initWithNibName:@"RecordingViewController" bundle:nil];
        recorderVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentModalViewController:recorderVC animated:YES];
        recordedFile = [recorderVC.recorder.url absoluteString]; 
        [recorderVC release];
        self.currentSoundPath = recordedFile;
        NSLog(@"%@",currentSoundPath);

        /*if ([fileMan fileExistsAtPath:recordedFile]){
        UIActionSheet *overwrite = [[UIActionSheet alloc] initWithTitle:@"Overwrite the current doorbell recording?" delegate:self cancelButtonTitle:@"Cancel"  destructiveButtonTitle:@"Overwrite" otherButtonTitles:nil];
        overwrite.delegate = self;
        [overwrite showInView:self.view];
        [overwrite release];
        }*/

        /*NSDictionary *recorderSettingsDict=[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:kAudioFormatAppleIMA4],AVFormatIDKey,
                                            [NSNumber numberWithInt:44100.0],AVSampleRateKey,
                                            [NSNumber numberWithInt: 2],AVNumberOfChannelsKey,
                                            [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
                                            [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
                                            [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                                            nil];
        AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:recordedFile settings:<#(NSDictionary *)settings#> error:<#(NSError **)outError#>];*/
    }
    else{
        UIAlertView *noAudio = [[UIAlertView alloc] initWithTitle:@"No microphone connected" message:@"Please connect a microphone to record a doorbell." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [noAudio show];
        [noAudio release];
    }
}

I'm programming an app that lets me record doorbell sounds and play them back on a doorbell that the user sees when they push the button.
My app seems to record and save the file (the file is there (in Documents) according to the iPhone simulator as I've created the directory in the app delegate)).
However playback isn't working. (By the way, the recording property returns TRUE.)

UPDATE: Here's the zip of the whole project.

Can someone help me see what's wrong?

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.layer.cornerRadius = 8;
    [actionButton setBackgroundImage:[[UIImage imageNamed:@"biggreenbutton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
    NSFileManager *nsf = [NSFileManager defaultManager];
    NSArray *allDocs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [allDocs objectAtIndex:0];
    NSString *savedDBells = [path stringByAppendingPathComponent:@"Custom Doorbells"];
    NSString *nextFile;
    for (NSInteger i = 1; i < 100; i++){
        if (![nsf fileExistsAtPath:[savedDBells stringByAppendingPathComponent:[NSString stringWithFormat:@"d%i%@",i,@".caf"]]]){
            nextFile = [savedDBells stringByAppendingPathComponent:[NSString stringWithFormat:@"d%i%@",i,@".caf"]];
            break;
        }
    }
    NSURL *url = [[NSURL alloc] initFileURLWithPath:nextFile];
    NSDictionary *setup = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:kAudioFormatMPEG4AAC],AVFormatIDKey,
                           [NSNumber numberWithInt:44100.0],AVSampleRateKey,
                           [NSNumber numberWithInt: 2],AVNumberOfChannelsKey,
                           [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
                           [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
                           [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                           nil];

    NSError *err;
    NSLog(@"%@",url);
    recorder = [[AVAudioRecorder alloc] initWithURL:url settings:setup error:&err];
    [setup release];
    [url release];
    //Gradient code
    /*
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.view.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
    [self.view.layer insertSublayer:gradient atIndex:0];
     */
}

-(IBAction) actionButtonPressed: (id) sender{
    NSLog(@"%@",actionButton.titleLabel.text);
    if ([actionButton.titleLabel.text isEqual:@"Finish Recording"]){
        BOOL test = recorder.recording;
        NSLog(@"Recording property = %i",test);
        [upCounter invalidate];
        [recorder stop];

        [self dismissModalViewControllerAnimated:YES];
    }
    if ([actionButton.titleLabel.text isEqual:@"Begin Recording"]){
        upCounter = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:upCounter forMode:NSDefaultRunLoopMode];
        [actionButton setTitle:@"Finish Recording" forState:UIControlStateNormal];
        [actionButton setBackgroundImage:[[UIImage imageNamed:@"bigredbutton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
        NSLog(@"Recorder record = %i",[recorder record]);
    }

}

In another view controller:

-(IBAction) recordingButtonPressed:(id) sender{
    AVAudioSession *avs = [AVAudioSession sharedInstance];
    //Test for written folder
    /*NSFileManager *fileMan = [NSFileManager defaultManager];
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    */

    if (avs.inputIsAvailable){
        NSString *recordedFile;

        RecordingViewController *recorderVC = [[RecordingViewController alloc] initWithNibName:@"RecordingViewController" bundle:nil];
        recorderVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentModalViewController:recorderVC animated:YES];
        recordedFile = [recorderVC.recorder.url absoluteString]; 
        [recorderVC release];
        self.currentSoundPath = recordedFile;
        NSLog(@"%@",currentSoundPath);

        /*if ([fileMan fileExistsAtPath:recordedFile]){
        UIActionSheet *overwrite = [[UIActionSheet alloc] initWithTitle:@"Overwrite the current doorbell recording?" delegate:self cancelButtonTitle:@"Cancel"  destructiveButtonTitle:@"Overwrite" otherButtonTitles:nil];
        overwrite.delegate = self;
        [overwrite showInView:self.view];
        [overwrite release];
        }*/

        /*NSDictionary *recorderSettingsDict=[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:kAudioFormatAppleIMA4],AVFormatIDKey,
                                            [NSNumber numberWithInt:44100.0],AVSampleRateKey,
                                            [NSNumber numberWithInt: 2],AVNumberOfChannelsKey,
                                            [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
                                            [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
                                            [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                                            nil];
        AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:recordedFile settings:<#(NSDictionary *)settings#> error:<#(NSError **)outError#>];*/
    }
    else{
        UIAlertView *noAudio = [[UIAlertView alloc] initWithTitle:@"No microphone connected" message:@"Please connect a microphone to record a doorbell." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [noAudio show];
        [noAudio release];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海里的那抹蓝 2024-08-29 21:23:46

唯一让我惊讶的是你正在调用 [NSNumber numberWithInt:44100.0] 而不是 numberWithDouble。

The only thing that jumps out at me is that you're calling [NSNumber numberWithInt:44100.0] instead of numberWithDouble.

淡淡的优雅 2024-08-29 21:23:46

虽然这可能不是此代码的问题,但就我而言,无法播放录制的音频文件是由于未调用 [audioRecorder stop] 造成的。当我的音频播放器引用到达应该调用停止的代码时,我的代码为零,因此使用调试器来确保当您告诉录音停止时您仍然有到录音机的链接。

While it may not be the problem this code had, in my case the inability to play back a recorded audio file was due to not calling [audioRecorder stop]. I had the code in place by my audio player reference was nil when it got to the code that was supposed to call stop, so use the debugger to make sure when you tell the recording to stop you still have a link to the audio recorder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文