MPMusicPlayerController和iOS 11.3

问题描述 投票:1回答:1

我有一个iOS应用程序在iOS 6.0到11.2上工作正常,但刚开始失败11.3(.1)和11.4 beta。该应用程序播放来自本地设备的播放列表中的歌曲。我已经为测试用例提取了应用程序的基本部分,但它以同样的方式失败。难道我做错了什么?我正在使用XCode 7.3.1并以iOS 6.0为目标。当它失败时,我在日志中得到的唯一消息(除了通过NSLog的歌曲列表之外还有以下1行错误:libc ++ abi.dylib:以NSException类型的未捕获异常终止。它在[musicPlayer play]上失败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

用户界面非常简单 - 1个UITableView(播放列表表)和1个停止按钮。

ios crash mpmusicplayercontroller ios11.3
1个回答
0
投票

你为什么不用

[musicPlayer setQueueWithQuery:musicPlaylistQuery]; - >好的

代替

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

© www.soinside.com 2019 - 2024. All rights reserved.