Search code examples
iosxcodempmusicplayercontrollermpmediaitem

Alertview when song is unavailable


I have a nice little query that when a user clicks a "play button," a title is called and if the song of that title exists in their iTunes, the song begins playing. I'm stuck now because I am not sure how to show an alertview when the song doesn't exist.

Here's the query:

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

With handle_PlaybackStateChanged being:

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

}
}

I have just tried this:

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

}

but doesn't work. Help.


Solution

  • Try

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

    before

    [self.player setQueueWithQuery: q]