在文档加载音频时的UITableView&QUOT

在文档加载音频时的UITableView&QUOT

本文介绍了在文档加载音频时的UITableView"细胞QUOT;是pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView在Documents目录显示LLIST F文件的...我希望它发挥的Documents目录的音频文件时pressed ...

它运行没有errores但不播放音频时pressed ...

   - (空)的tableView:(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath {NSArray的*路径= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
的NSString * documentsDirectory = [路径objectAtIndex:0];
的NSString *文件名= [documentsDirectory stringByAppendingPathComponent:[的NSString stringWithFormat:@%d.caf,indexPath.row + 1];
NSURL * audioURL = [NSURL URLWithString:文件名]。NSData的* audioData = [NSData的dataWithContentsOfURL:audioURL];NSError *错误;_audioPlayer = [[AVAudioPlayer页头] initWithContentsOfURL:audioURL错误:&放大器;错误]    _audioPlayer.delegate =自我;
        [_audioPlayer玩]
}


解决方案

更改此行:

  NSURL * audioURL = [NSURL URLWithString:文件名]。

这样:

  NSURL * audioURL = [NSURL fileURLWithPath:文件名isDirectory:NO];

然后就去做:

 如果(_audioPlayer ==无)
    {
        _audioPlayer = [[AVAudioPlayer页头] initWithContentsOfURL:audioURL错误:无];
    }
    其他
    {
        _audioPlayer = [self.audioAlert initWithContentsOfURL:audioURL错误:无];
    }

I have a UITableView displaying a llist f files in the Documents Directory... and I want it to play the audio file in the Documents directory when pressed...

It runs with no errores but doesn't play the audio when pressed...

    - (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+1]];
NSURL *audioURL = [NSURL URLWithString:fileName];

NSData *audioData = [NSData dataWithContentsOfURL:audioURL];

NSError *error;

_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];

    _audioPlayer.delegate = self;
        [_audioPlayer play];
}
解决方案

Change this line:

NSURL *audioURL = [NSURL URLWithString:fileName];

to this:

NSURL *audioURL = [NSURL fileURLWithPath:fileName isDirectory:NO];

And then just do:

 if(_audioPlayer == nil)
    {
        _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
    }
    else
    {
        _audioPlayer = [self.audioAlert initWithContentsOfURL:audioURL error:nil];
    }

这篇关于在文档加载音频时的UITableView"细胞QUOT;是pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:23