使用 @protocol 从 UITableViewController 控制音频与主视图控制器?

发布于 2024-11-05 07:01:46 字数 455 浏览 0 评论 0原文

好的,这是场景:

我正在使用 AVAudioPlayer。

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

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

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

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

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

@protocol SongChooserDelegate

-(void) didTap:(NSData *) 数据; <------------ 我在这里猜测

@end

如果这不起作用-什么可以?

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

Ok here's the scenario:

I am using AVAudioPlayer.

I am trying to select and play a song from UITableView which appears as a popover.

The mp3 assets are located in my documents directory.

I can populate the table view and when I select a row, play that specific asset.

What I can not do is control the audio once the popover has disappeared with the controls that are on my main view controller. (play/stop/volume)

I have @protocol which makes the popover a delegate, can any one help me with the syntax of the method that would go in my protocol?

@protocol SongChooserDelegate

-(void) didTap:(NSData *) data; <------------ I'm guessing here

@end

If this won't work - what will?

Thank you, any help would be greatly appreciated.... this is my last step in building my app for my senior thesis due Friday!!!!! eeeek.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈独秀 2024-11-12 07:01:46

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

//UITableViewController.h

@protocol SongChooserDelegate

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

@end

//UITableView.m

  • (void)viewDidLoad
    {
    [self.playerprepareToPlay];

    // 指向文档目录
    NSString *path = [NSHomeDirectory()
    stringByAppendingPathComponent:@"文档"];
    NSError *错误= nil;
    NSArray *array = [[NSFileManager defaultManager]
    contentOfDirectoryAtPath:路径错误:&错误];
    如果(数组==零){
    // 处理错误
    }
    self.songs = 数组;

    [super viewDidLoad];

}

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

    UIAlertView *showSelection;
    NSString *消息;
    消息 = [[NSString alloc] initWithFormat:@"%@",
    [歌曲objectAtIndex:indexPath.row]];
    showSelection = [[UIAlertView 分配]
    initWithTitle:@"跟踪选定的" 消息:消息委托:nil
    cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [显示选择显示];
    [showSelection发布];
    [消息发布];

    NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [applicationDocumentsDirectory stringByAppendingPathComponent: [歌曲 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];

[播放器prepareToPlay];

}

It took me a while, but I figured this out finally:
If any one wants and explanation or all the code, let me know.

//UITableViewController.h

@protocol SongChooserDelegate

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

@end

//UITableView.m

  • (void)viewDidLoad
    {
    [self.player prepareToPlay];

    // Point to Document directory
    NSString *path = [NSHomeDirectory()
    stringByAppendingPathComponent:@"Documents"];
    NSError *error = nil;
    NSArray *array = [[NSFileManager defaultManager]
    contentsOfDirectoryAtPath:path error:&error];
    if (array == nil) {
    // Handle the error
    }
    self.songs = array;

    [super viewDidLoad];

}

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

    UIAlertView *showSelection;
    NSString *message;
    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 release];
    [message release];

    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];

[player prepareToPlay];

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文