【问题标题】:Displaying files from Bundle into UITableView - iOS将 Bundle 中的文件显示到 UITableView - iOS
【发布时间】:2016-06-19 11:23:54
【问题描述】:

我的包中有几个音频文件,我想在UITableView 中显示它们(名称)。当用户点击任何cell 时,应播放具有该名称的音频文件。

例如

我用谷歌搜索,但我得到的只是从 Documents Directory 中读取文件。

如何显示我的捆绑文件?

请帮忙, 谢谢!

【问题讨论】:

    标签: ios objective-c uitableview resources nsbundle


    【解决方案1】:

    您必须首先获得指向主包的指针,其中存储了应用程序的资源。使用此指针,您可以搜索所有扩展名为 mp3 的资源。 您可以使用如下代码:

    NSBundle *mainBundle = [NSBundle mainBundle];
    NSArray *paths = [mainBundle pathsForResourcesOfType:@"mp3" inDirectory:nil];
    

    每个路径中的最后一个元素是相应 mp3 文件的文件名。

    如需了解更多信息,请查看 Apple 的Bundle Programming Guide

    【讨论】:

      【解决方案2】:

      tableview didselectrowatindexpath方法中写下这段代码

      NSString * soundSelected = cell.textlabel.text;
          NSString *path = [NSString stringWithFormat:@"%@/%@.mp3", [[NSBundle mainBundle] resourcePath],soundSelected];
              NSURL *soundUrl = [NSURL fileURLWithPath:path];
      
              AVAudioPlayer* cellTapSound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
              [cellTapSound play];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多