gpt4 book ai didi

ios - 歌曲不可用时的警报 View

转载 作者:行者123 更新时间:2023-11-29 02:04:45 26 4
gpt4 key购买 nike

我有一个很好的小查询,当用户单击“播放按钮”时,将调用一个标题,如果该标题的歌曲存在于他们的 iTunes 中,该歌曲就会开始播放。我现在被卡住了,因为我不确定当歌曲不存在时如何显示警报 View 。

这是查询:

- (void) queryMusic
{
MPMediaQuery *q = [[MPMediaQuery alloc] init];
[q addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:@"Beyoncé" forProperty:MPMediaItemPropertyArtist comparisonType:MPMediaPredicateComparisonEqualTo]];


[q addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:_DetailModal[0]
forProperty:MPMediaItemPropertyTitle comparisonType:MPMediaPredicateComparisonEqualTo]];



[q setGroupingType:MPMediaGroupingTitle];

self.player = [MPMusicPlayerController applicationMusicPlayer];
[self.player setRepeatMode:MPMusicRepeatModeAll];
[self.player setShuffleMode:MPMusicShuffleModeSongs];
[self.player setQueueWithQuery: q];



[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleStateChanged:)
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector (handle_PlaybackStateChanged:)
name: MPMusicPlayerControllerPlaybackStateDidChangeNotification
object: self.player];

[self.player beginGeneratingPlaybackNotifications];
}

handle_PlaybackStateChanged 为:

- (void) handle_PlaybackStateChanged: (id) notification {

MPMusicPlaybackState playbackState = [self.player playbackState];


if (playbackState == MPMusicPlaybackStatePaused) {

self.textLabel.text = @"play";
[PlayButton setTitle:@"STOP" forState:UIControlStateNormal]; // To set the title

NSLog (@"This is paused");
self.playBarButton.title = @"Play";

self.PlayButton = PlayButton;


[self.PlayButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[playPauseButton setImage:[UIImage imageNamed:@"playbss.png"] forState:UIControlStateNormal];



} else if (playbackState == MPMusicPlaybackStatePlaying) {

self.textLabel.text = @"pause";
[PlayButton setTitle:@"Pause" forState:UIControlStateNormal];

self.PlayButton = PauseButton;
[playPauseButton setImage:[UIImage imageNamed:@"pausesss@2x.png"] forState:UIControlStateNormal];

NSLog (@"This is playing");




} else if (playbackState == MPMusicPlaybackStateStopped) {

self.textLabel.text = @"Play";
self.PlayButton = PlayButton;
[playPauseButton setImage:[UIImage imageNamed:@"playbss.png"] forState:UIControlStateNormal];


[self.player stop];

}
}

我刚试过这个:

MPMediaItem *nowPlayingItem = self.player.nowPlayingItem;

NSString *title  = [nowPlayingItem valueForProperty:MPMediaItemPropertyTitle];

if (title == (id)[NSNull null] || title.length == 0) {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!"
message:@"This is your first UIAlertview message."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];

[message show];

}

但不起作用。帮助。

最佳答案

尝试

if( q.items.count < 1 )  {
[self showAlert];
return;
}

之前

[self.player setQueueWithQuery: q]

关于ios - 歌曲不可用时的警报 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29911011/

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