我花了一段时间,但我终于想通了:如果有人想要解释或所有代码,请告诉我。
//UITableViewController.h
@protocol SongChooserDelegate
-(void) didTap:(NSURL *)songUrl;
@结尾
//UITableView.m
(void)viewDidLoad { [self.player prepareToPlay];
// 指向文档目录 NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSError *error = nil; NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error]; if (array == nil) { // 处理错误 } self.songs = array;
[超级视图DidLoad];
}
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *showSelection; NSString *消息;message = [[NSString alloc] initWithFormat:@"%@", [songs objectAtIndex:indexPath.row]]; showSelection = [[UIAlertView alloc] initWithTitle:@"Track Selected" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [showSelection show];[showSelection 发布];【消息发布】;
NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *filePath = [applicationDocumentsDirectory stringByAppendingPathComponent: [songs objectAtIndex:indexPath.row]]; NSURL *url = [NSURL fileURLWithPath:filePath];
[self.delegate didTap:url];
}
//ViewController.h
@interface ViewController : UIViewController < SongChooserDelegate, AVAudioPlayerDelegate >
//ViewController.m
-(void) didTap:(NSURL *)songUrl{
player = [[AVAudioPlayer alloc]initWithContentsOfURL:songUrl error:nil];
[播放器准备播放];
}