歌曲不可用时的警报视图

Posted

技术标签:

【中文标题】歌曲不可用时的警报视图【英文标题】:Alertview when song is unavailable 【发布时间】:2015-04-28 05:29:14 【问题描述】:

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

这是查询:

- (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];


但不起作用。帮助。

【问题讨论】:

这无济于事,但 UIAlertView 在 ios 8 中已被弃用。使用 UIAlertController。 Documentation 哈,谢谢。一旦我知道确切的位置,我就会记住这一点。 在 [self.player setQueueWithQuery: q] 之前尝试 if( q.items.count @amergin 我从未如此感激。非常感谢。请将其添加为下面的回复,以便我批准您的回答。 【参考方案1】:

试试

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

之前

[self.player setQueueWithQuery: q]

【讨论】:

以上是关于歌曲不可用时的警报视图的主要内容,如果未能解决你的问题,请参考以下文章

Shell Script 和 yum,包不可用时怎么办。森托斯

使用适用于 iOS 的 XMPP 框架:当订阅者可用/不可用时接收状态

Android WifiP2p - 当发现的设备不可用时收到通知

当目的地不可用时如何停止使用骆驼路线中的消息

当原始存储库不可用时,是不是可以将默认存储库添加到 SLES?

检查gps在iphone中是不是可用