gpt4 book ai didi

ios - MPMusicPlayerController和iOS 11.3

转载 作者:行者123 更新时间:2023-12-03 16:05:27 25 4
gpt4 key购买 nike

我有一个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];->失败

关于ios - MPMusicPlayerController和iOS 11.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50418016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com