通过iOS中的表格视图访问和播放声音文件

时间:2014-03-08 05:05:28

标签: ios objective-c avfoundation

我正在尝试制作一个基本的语音备忘录应用程序,我能够录制声音并播放它,并将文件名放在桌面视图上。

现在我想点击桌面视图行时发出声音,我无法播放声音。

我一直在这篇帖子中排队:Load audio in DOCUMENTS when UITableView "cell" is pressed

这是我的完整代码。最相关的部分位于最底层:

#import "ViewController.h"


@interface ViewController () {
AVAudioRecorder *recorder;
AVAudioPlayer *player;
NSArray *filePathsArray;
NSArray *tableData;
NSString *recorderFilePath;
NSURL *url;

}
@end

@implementation ViewController



- (void)viewDidLoad
{
[super viewDidLoad];

// Disable Stop/Play button when application launches
[self.stopButton setEnabled:NO];
[self.playButton setEnabled:NO];
[self audioSetUp];

}

-(void) audioSetUp {
// Set the audio file
NSDate *date = [NSDate date];
NSString *fileName = [date description];
NSArray *pathComponents = [NSArray arrayWithObjects:
                           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject],
                           fileName,
                           nil];
url = [NSURL fileURLWithPathComponents:pathComponents];
// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];



// Define the recorder setting
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:url settings:recordSetting error:NULL];
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}


cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}


- (IBAction)recordPauseButton:(UIButton *)sender {
}
- (IBAction)recordPauseTapped:(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];
    [self.recordPauseButton setTitle:@"Pause" forState:UIControlStateNormal];

} else {

    // Pause recording
    [recorder pause];
    [self.recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];
}

[self.stopButton setEnabled:YES];
[self.playButton setEnabled:NO];
}

- (IBAction)stopTapped:(id)sender {
[recorder stop];



//pulling up saved data for the table view
NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsFolder = [folders objectAtIndex:0];

tableData = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsFolder error:nil];
[self.tableView reloadData];

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
//[self audioSetUp];

}

- (IBAction)playTapped:(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{
[self.recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];

[self.stopButton setEnabled:NO];
[self.playButton setEnabled:YES];
}

- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Done"
                                                message: @"Finish playing the recording!"
                                               delegate: nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];
[self audioSetUp];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath             {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.caf",indexPath.row]];
url = [NSURL fileURLWithPath: fileName isDirectory:NO];

if(player == nil)
    {
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }

NSError *error;

player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

player.delegate = self;
[player play];

}

@end

1 个答案:

答案 0 :(得分:0)

所以我找到了解决我的困境的方法。感谢@ fumoboy007和@bgfriend的建议。

以下是我更改代码最后一部分的方法:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath     {

NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsFolder = [folders objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"%@/%@",documentsFolder,[tableData objectAtIndex:indexPath.row]];
url = [NSURL fileURLWithPath:fileName isDirectory:
       NO];


if(player == nil)
    {
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    }

NSError *error;

player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

player.delegate = self;
[player prepareToPlay];
[player play];
}

现在它就像一个魅力!

相关问题