簡體   English   中英

MPMusicPlayerController和iOS 11.3

[英]MPMusicPlayerController and iOS 11.3

我有一個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播放中]失敗在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(playlistsTableView)和1個“停止”按鈕。

你為什么不使用

[musicPlayer setQueueWithQuery:musicPlaylistQuery]; ->好吧

代替

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM