Music Player tutorial

Discussion in 'iOS Programming' started by wildchild247, Apr 2, 2014.

  1. wildchild247 macrumors newbie

    Joined:
    Mar 2, 2014
    #1
    Hi

    I am doing the tutorial here

    http://code.tutsplus.com/tutorials/ios-sdk-music-library-access--mobile-6188

    I have no errors and I am wondering if anyone could look at my code

    Thanks

    Code:
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    @synthesize musicPlayer;
    
    - (void)viewDidLoad{
        [super viewDidLoad];
        
        musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
        
        [volumeSlider setValue:[musicPlayer volume]];
        
        if ([musicPlayer playbackState] == MPMusicPlaybackStatePlaying) {
            
            [playPauseButton setImage:[UIImage imageNamed:@"pauseButton.png"] forState:UIControlStateNormal];
            
        } else {
            
            [playPauseButton setImage:[UIImage imageNamed:@"playButton.png"] forState:UIControlStateNormal];
        }
    
        [self registerMediaPlayerNotifications];
    }
    
    - (void) registerMediaPlayerNotifications{
        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
        
        [notificationCenter addObserver: self
                               selector: @selector (handle_NowPlayingItemChanged:)
                                   name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification
                                 object: musicPlayer];
        
        [notificationCenter addObserver: self
                               selector: @selector (handle_PlaybackStateChanged:)
                                   name: MPMusicPlayerControllerPlaybackStateDidChangeNotification
                                 object: musicPlayer];
        
        [musicPlayer beginGeneratingPlaybackNotifications];
    }
    
    - (void) handle_NowPlayingItemChanged: (id) notification{
        MPMediaItem *currentItem = [musicPlayer nowPlayingItem];
        UIImage *artworkImage = [UIImage imageNamed:@"noArtworkImage.png"];
        MPMediaItemArtwork *artwork = [currentItem valueForProperty: MPMediaItemPropertyArtwork];
        
        if (artwork) {
            artworkImage = [artwork imageWithSize: CGSizeMake (200, 200)];
        }
        
        [artworkImageView setImage:artworkImage];
        
        NSString *titleString = [currentItem valueForProperty:MPMediaItemPropertyTitle];
        if (titleString) {
            titleLabel.text = [NSString stringWithFormat:@"Title: %@",titleString];
        } else {
            titleLabel.text = @"Title: Unknown title";
        }
        
        NSString *artistString = [currentItem valueForProperty:MPMediaItemPropertyArtist];
        if (artistString) {
            artistLabel.text = [NSString stringWithFormat:@"Artist: %@",artistString];
        } else {
            artistLabel.text = @"Artist: Unknown artist";
        }
        
        NSString *albumString = [currentItem valueForProperty:MPMediaItemPropertyAlbumTitle];
        if (albumString) {
            albumLabel.text = [NSString stringWithFormat:@"Album: %@",albumString];
        } else {
            albumLabel.text = @"Album: Unknown album";
        }
    }
    
    - (void) handle_PlaybackStateChanged: (id) notification{
        MPMusicPlaybackState playbackState = [musicPlayer playbackState];
        
        if (playbackState == MPMusicPlaybackStatePaused) {
            [playPauseButton setImage:[UIImage imageNamed:@"playButton.png"] forState:UIControlStateNormal];
            
        } else if (playbackState == MPMusicPlaybackStatePlaying) {
            [playPauseButton setImage:[UIImage imageNamed:@"pauseButton.png"] forState:UIControlStateNormal];
            
        } else if (playbackState == MPMusicPlaybackStateStopped) {
            
            [playPauseButton setImage:[UIImage imageNamed:@"playButton.png"] forState:UIControlStateNormal];
            [musicPlayer stop];
        }
    }
    
    - (void) handle_VolumeChanged: (id) notification{
        [volumeSlider setValue:[musicPlayer volume]];
    }
    
    
    
    - (IBAction)volumeChanged:(id)sender{
        [musicPlayer setVolume:[volumeSlider value]];
    }
    
    - (IBAction)showMediaPicker:(id)sender{
        MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAny];
        
        mediaPicker.allowsPickingMultipleItems = YES;
        mediaPicker.prompt = @"Select songs to play";
    }
    
    - (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection{
        if (mediaItemCollection) {
            
            [musicPlayer setQueueWithItemCollection: mediaItemCollection];
            [musicPlayer play];
        }
    }
    
    - (void) mediaPickerDidCancel: (MPMediaPickerController *) mediaPicker{
    
    }
    
    - (IBAction)previousSong:(id)sender{
        [musicPlayer skipToPreviousItem];
    }
    
    - (IBAction)playPause:(id)sender{
        if ([musicPlayer playbackState] == MPMusicPlaybackStatePlaying) {
            [musicPlayer pause];
            
        } else {
            [musicPlayer play];
            
        }
    }
    - (IBAction)nextSong:(id)sender{
        [musicPlayer skipToNextItem];
    }
    
    @end
    


    ----------

    Code:
    #import <UIKit/UIKit.h>
    #import <MediaPlayer/MediaPlayer.h>
    
    @interface ViewController : UIViewController{
    
        IBOutlet UIImageView *artworkImageView;
        IBOutlet UISlider *volumeSlider;
        IBOutlet UIButton *playPauseButton;
    
        IBOutlet UILabel *titleLabel;
        IBOutlet UILabel *artistLabel;
        IBOutlet UILabel *albumLabel;
       
    }
    @property (nonatomic, retain) MPMusicPlayerController *musicPlayer;
    
    - (IBAction)volumeChanged:(id)sender;
    - (IBAction)showMediaPicker:(id)sender;
    - (IBAction)previousSong:(id)sender;
    - (IBAction)playPause:(id)sender;
    - (IBAction)nextSong:(id)sender;
    
    - (void) registerMediaPlayerNotifications;
    
    @end
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What seems to be the problem?

    P.S. I suggest you read about Getting Answers
     
  3. wildchild247 thread starter macrumors newbie

    Joined:
    Mar 2, 2014
    #3
    when I run it the ios simulator pops up and then crashes

    I am new to obj C and maybe Ive made a simple mistake
     
  4. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #4
    If you were to post the crash report, then we might be able to help you more :)
     

Share This Page