【问题标题】:MPMediaQuery - getting the songs for each 'section'MPMediaQuery - 获取每个“部分”的歌曲
【发布时间】:2020-12-08 14:26:52
【问题描述】:

我正在尝试用 Objective-c 在 iOS 上构建一个媒体浏览器。 到目前为止我可以得到songsQuery:

_query = [MPMediaQuery songsQuery];

在我的 tableView 数据源中,我可以像这样获得节数和节标题:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if(_query)
    {
        return _query.itemSections.count;
    }
    
    return 1;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    return _query.itemSections[section].title;
}

这给了我(比如说)26 个部分,标题如“A”、“B”、“C”等等......正如预期的那样。 我不明白的是如何计算每个部分的歌曲数量。即如何获取“A”或“B”等部分的所有歌曲

【问题讨论】:

    标签: ios objective-c mpmediaquery


    【解决方案1】:

    如果我正确理解您的问题,您希望获取每个 MPMediaQuery 部分中的歌曲数量。

    documentationMPMediaQuerySection 应该包括一个range 存储该部分中包含的项目数组的范围的属性。然后可以通过取范围的length 来获得歌曲的数量。像这样:

    NSUInteger numSongs = _query.itemSections[section].range.length;
    

    您还应该能够像这样获得该部分中所有歌曲的数组:

    //The range of the song in the section
    NSRange *sectionRange = _query.itemSections[section].range;
    //Array of MPMediaItems in the section
    NSArray *songsInSection = [_query.items subarrayWithRange: sectionRange];
    

    【讨论】:

    • 谢谢 - 这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    相关资源
    最近更新 更多