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



我正在尝试制作一个基本的语音备忘录,我能够记录声音并播放它,然后我将文件名放在表视图上。p>现在,当我单击表视图行时,我想播放声音,我无法让声音播放。

我一直在这篇文章中排队:uitableview'cell''按下

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

#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

,所以我找到了解决困境的解决方案。感谢 @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];
}

现在它就像魅力一样!

最新更新