【问题标题】:Control Audio with main view controller from UITableViewController using @protocol?使用@protocol 从 UITableViewController 的主视图控制器控制音频?
【发布时间】:2011-05-04 22:05:03
【问题描述】:

好的,这是场景:

我正在使用 AVAudioPlayer。

我正在尝试从 UITableView 中选择并播放一首歌曲,该歌曲显示为弹出框。

mp3 资源位于我的文档目录中。

我可以填充表格视图,当我选择一行时,播放该特定资产。

我不能做的是在弹出框消失后使用我的主视图控制器上的控件控制音频。 (播放/停止/音量)

我有@protocol,它使弹出框成为一个委托,任何人都可以帮助我了解我的协议中的方法的语法吗?

@protocol SongChooserDelegate

-(void) didTap:(NSData *) 数据;

@结束

如果这行不通 - 会怎样?

谢谢你,任何帮助将不胜感激......这是我为周五到期的高级论文构建应用程序的最后一步!!!!!!哎呀。

【问题讨论】:

    标签: protocols uitableview avaudioplayer popover


    【解决方案1】:

    我花了一段时间,但我终于想通了: 如果有人想要解释或所有代码,请告诉我。

    //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:路径错误:&error]; 如果(数组==零){ // 处理错误 } self.songs = 数组;

      [超级viewDidLoad];

    }

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

      UIAlertView *showSelection; NSString *消息; message = [[NSString alloc] initWithFormat:@"%@", [歌曲对象AtIndex:indexPath.row]]; showSelection = [[UIAlertView 分配] 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

    //ViewController.m

    -(void) didTap:(NSURL *)songUrl{

     player = [[AVAudioPlayer alloc]initWithContentsOfURL:songUrl error:nil];
    

    [播放器准备播放];

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多