1

我有一个 iOS 应用程序,它在 iOS 6.0 到 11.2 上运行良好,但在 11.3(.1) 和 11.4 测试版上才开始失败。该应用程序从本地设备播放播放列表中的歌曲。我已经为测试用例提取了应用程序的基本部分,但它以同样的方式失败。难道我做错了什么?我正在使用 XCode 7.3.1 并针对 iOS 6.0。当它失败时,我在日志中收到的唯一消息(除了通过 NSLog 列出的歌曲列表之外,还有以下 1 行错误:libc++abi.dylib:以 NSException 类型的未捕获异常终止。它在 [musicPlayer 播放] 上失败playPlaylist 例程中的命令。下面是代码:

#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>

#define TESTMUSICPLAYER

MPMusicPlayerController *musicPlayer;

@interface ViewController ()

@property (strong, nonatomic) NSMutableArray *validPlayListArray;
@property (strong, nonatomic) NSString *musicPlaylist;
@property (strong, nonatomic) IBOutlet UIView *playlistsTableView;
@property (strong, nonatomic) NSMutableArray *playListArray;
@property NSInteger currentPlaylistIndex;
@property (strong, nonatomic) MPMediaQuery *musicPlaylistQuery;
@property (strong, nonatomic) NSArray *collections;
- (IBAction)stopButtonPressed:(id)sender;
@end

@implementation ViewController
@synthesize validPlayListArray;
@synthesize musicPlaylistQuery;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [validPlayListArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *musicCellIdentifier = @"MusicCell";
    UITableViewCell *musicCell = [tableView dequeueReusableCellWithIdentifier:musicCellIdentifier];
    if (musicCell == nil)
        {
            musicCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:musicCellIdentifier];
            musicCell.selectionStyle = UITableViewCellSelectionStyleGray;
        }
    musicCell.textLabel.text = [validPlayListArray objectAtIndex:indexPath.row];
    return musicCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _musicPlaylist = [validPlayListArray objectAtIndex:indexPath.row];
    [self playMusic];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self loadThePlaylistArrays];
}

- (void)loadThePlaylistArrays
{
    // Clear the valid playlist array
    validPlayListArray = nil;
    // Allocate our arrays
    validPlayListArray = [[NSMutableArray alloc] init];
    MPMediaPropertyPredicate *playlistName;
    MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery];
    _playListArray = (NSMutableArray*)[playlistsQuery collections];
    // Set the currentPlaylistIndex to -1 in case there are no matches
    _currentPlaylistIndex = -1;
    int validCount = 0;
    for (int i = 0; i < [_playListArray count]; ++i)
    {
        NSString *stringplaylistName = [[_playListArray objectAtIndex:i] valueForProperty:MPMediaPlaylistPropertyName];
        playlistName = [MPMediaPropertyPredicate predicateWithValue:stringplaylistName forProperty:MPMediaPlaylistPropertyName];
        musicPlaylistQuery = [MPMediaQuery playlistsQuery];
        [musicPlaylistQuery addFilterPredicate:playlistName];
        _collections = [musicPlaylistQuery items];
        if ([_collections count] > 0)
        {
            //NSLog(@"Playlist: %@ Item count: %d", stringplaylistName, [_collections count]);
            if (validCount > 0)
            {
                if (![stringplaylistName isEqualToString:[validPlayListArray objectAtIndex:validCount - 1]])
                {
                    if (![stringplaylistName isEqualToString:@""])
                    {
                        [validPlayListArray addObject:stringplaylistName];
                        if ([_musicPlaylist isEqualToString:stringplaylistName])
                        {
                            _currentPlaylistIndex = validCount;
                        }
                        validCount ++;
                    }
                }
            }
            else
            {
                if (![stringplaylistName isEqualToString:@""])
                {
                    [validPlayListArray addObject:stringplaylistName];
                    if ([_musicPlaylist isEqualToString:stringplaylistName])
                    {
                        _currentPlaylistIndex = validCount;
                    }
                    validCount ++;
                }
            }
        }
    }
}

- (void)playMusic
{
    MPMediaPropertyPredicate *playlistName;
    [self initializeMusicPlayer];
    playlistName = [MPMediaPropertyPredicate predicateWithValue:_musicPlaylist forProperty:MPMediaPlaylistPropertyName];
    musicPlaylistQuery = [MPMediaQuery playlistsQuery];
    [musicPlaylistQuery addFilterPredicate:playlistName];
    _collections = [musicPlaylistQuery items];
    #ifdef TESTMUSICPLAYER
        for (MPMediaItem *song in _collections)
        {
            NSString* songTitle = [song valueForProperty:MPMediaItemPropertyTitle];
            NSLog(@"%@", songTitle);
        }
    #endif
    if ([_collections count] > 0)
    {
        [self playPlaylist];
    }
}

- (void)initializeMusicPlayer
{
    musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
    [musicPlayer setShuffleMode:MPMusicShuffleModeOff];
    [musicPlayer setRepeatMode:MPMusicRepeatModeAll];
}

- (void)playPlaylist
{
    [musicPlayer setQueueWithItemCollection:(MPMediaItemCollection *)musicPlaylistQuery];
    [musicPlayer play];
}

- (IBAction)stopButtonPressed:(id)sender
{
    [musicPlayer stop];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

UI 相当简单 - 1 个 UITableView (playlistsTableView) 和 1 个停止按钮。

4

1 回答 1

0

你为什么不使用

[musicPlayer setQueueWithQuery:musicPlaylistQuery];--> 好的

代替

[musicPlayer setQueueWithItemCollection:(MPMediaItemCollection *)musicPlaylistQuery];--> 失败

于 2018-08-31T19:19:14.640 回答