如何在mpmoviePlayer中播放下一首歌?

时间:2014-09-30 05:50:37

标签: ios iphone mpmovieplayercontroller avaudioplayer mpmovieplayer

我正在使用MPMoviePlayerController来播放音频 我试图播放下一个音频,但它不起作用。 以便我可以使用MpMoviePlayerController播放下一个音频。

我正在使用此代码...

MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:contentURL]];

    if (moviePlayerViewController)
    {
        [self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
        [moviePlayerViewController.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];

        if ([moviePlayerViewController.moviePlayer respondsToSelector:@selector(setAllowsAirPlay:)])
        {
            [moviePlayerViewController.moviePlayer setAllowsAirPlay:YES];
        }

        [[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController queue:nil usingBlock:^(NSNotification *notification)
        {
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            [self dismissMoviePlayerViewControllerAnimated];
            [moviePlayerViewController release];
        }];

        [moviePlayerViewController.moviePlayer play];
    }

1 个答案:

答案 0 :(得分:0)

//
//  NowPlayingViewController.h

#import <UIKit/UIKit.h>

#import <MediaPlayer/MediaPlayer.h>

@interface NowPlayingViewController : UIViewController
{

    MPMusicPlayerController *musiPlayer;

    IBOutlet UIImageView *artworkImageView;

    IBOutlet UIButton *playPauseButton;

    IBOutlet UISlider *volumeSlider;

    IBOutlet UILabel *artistLabel;

    IBOutlet UILabel *titleLabel;

    IBOutlet UILabel *albumLabel;


}
@property (nonatomic, retain) MPMusicPlayerController *musicPlayer;

- (IBAction)playPause:(id)sender;

- (IBAction)nextSong:(id)sender;

- (IBAction)previousSong:(id)sender;

- (IBAction)volumeSliderChanged:(id)sender;

- (void) registerMediaPlayerNotifications;

@end

//
//  NowPlayingViewController.m
//  Music

#import "NowPlayingViewController.h"

@interface NowPlayingViewController ()

@end

@implementation NowPlayingViewController

@synthesize musicPlayer;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        // Custom initialization
    }

    return self;
}

#pragma mark - UI Setup

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Music player setup

    musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
    [self registerMediaPlayerNotifications];

    // Back button

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = backButton;

    // Custom UI

    artistLabel.textColor = [UIColor colorWithRed:0.278 green:0.278 blue:0.278 alpha:1.0];
    artistLabel.shadowColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    artistLabel.shadowOffset = CGSizeMake(0, 1);

    titleLabel.textColor = [UIColor colorWithRed:0.278 green:0.278 blue:0.278 alpha:1.0];
    titleLabel.shadowColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    titleLabel.shadowOffset = CGSizeMake(0, 1);

    albumLabel.textColor = [UIColor colorWithRed:0.278 green:0.278 blue:0.278 alpha:1.0];
    albumLabel.shadowColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    albumLabel.shadowOffset = CGSizeMake(0, 1);
}

- (void) back
{

    [self.navigationController popViewControllerAnimated:YES];

}

- (void) viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:YES];

    // update control button

    if ([musicPlayer playbackState] == MPMusicPlaybackStatePlaying) {

          [playPauseButton setImage:[UIImage imageNamed:@"Pause-icon.png"] forState:UIControlStateNormal];
    }

    else {
          [playPauseButton setImage:[UIImage imageNamed:@"Play-icon.png"] forState:UIControlStateNormal];
    }


    // Update volumelider

    [volumeSlider setValue:[musicPlayer volume]];


    // Update now playing info

    MPMediaItem *currentItem = [musicPlayer nowPlayingItem];

    MPMediaItemArtwork *artwork = [currentItem valueForProperty: MPMediaItemPropertyArtwork];
    UIImage *artworkImage = [artwork imageWithSize: CGSizeMake (320, 320)];

    if (!artworkImage) {

          artworkImage = [UIImage imageNamed:@"No-artwork.png"];
    }

    [artworkImageView setImage:artworkImage];


    NSString *titleString = [currentItem valueForProperty:MPMediaItemPropertyTitle];
    if (titleString) {
          titleLabel.text = titleString;
    } 

    else {
          titleLabel.text = @"Unknown title";
    }

    NSString *artistString = [currentItem valueForProperty:MPMediaItemPropertyArtist];
    if (artistString) {
          artistLabel.text = artistString;
    } 

    else {
          artistLabel.text = @"Unknown artist";
    }

    NSString *albumString = [currentItem valueForProperty:MPMediaItemPropertyAlbumTitle];
    if (albumString) {
          albumLabel.text = albumString;
    } 

    else {
          albumLabel.text = @"Unknown album";
    }

}

#pragma mark - Register media player notifications

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

    [notificationCenter addObserver: self
                           selector: @selector (handle_VolumeChanged:)
                               name: MPMusicPlayerControllerVolumeDidChangeNotification
                             object: musicPlayer];

    [musicPlayer beginGeneratingPlaybackNotifications];
}

- (void) handle_NowPlayingItemChanged: (id) notification
{

    if ([musicPlayer playbackState] != MPMusicPlaybackStateStopped) {
        MPMediaItem *currentItem = [musicPlayer nowPlayingItem];

        MPMediaItemArtwork *artwork = [currentItem valueForProperty: MPMediaItemPropertyArtwork];
        UIImage *artworkImage = [artwork imageWithSize: CGSizeMake (320, 320)];

        if (!artworkImage) {
              artworkImage = [UIImage imageNamed:@"No-artwork.png"];
        }

        [artworkImageView setImage:artworkImage];

        NSString *titleString = [currentItem valueForProperty:MPMediaItemPropertyTitle];
        if (titleString) {
              titleLabel.text = titleString;
        } 

        else {
              titleLabel.text = @"Unknown title";
        }

        NSString *artistString = [currentItem valueForProperty:MPMediaItemPropertyArtist];

        if (artistString) {
              artistLabel.text = artistString;
        } 


        else {
              artistLabel.text = @"Unknown artist";
        }

        NSString *albumString = [currentItem valueForProperty:MPMediaItemPropertyAlbumTitle];

        if (albumString) {
              albumLabel.text = albumString;
        } 

        else {
              albumLabel.text = @"Unknown album";
        }

    }
}


- (void) handle_PlaybackStateChanged: (id) notification
{
    MPMusicPlaybackState playbackState = [musicPlayer playbackState];

    if (playbackState == MPMusicPlaybackStatePaused) {
        [playPauseButton setImage:[UIImage imageNamed:@"Play-icon.png"] forState:UIControlStateNormal];


    } 

        else if (playbackState == MPMusicPlaybackStatePlaying) {
        [playPauseButton setImage:[UIImage imageNamed:@"Pause-icon.png"] forState:UIControlStateNormal];

    } 

        else if (playbackState == MPMusicPlaybackStateStopped) {

        [playPauseButton setImage:[UIImage imageNamed:@"Play-icon.png"] forState:UIControlStateNormal];

            [musicPlayer stop];

        [self.navigationController popViewControllerAnimated:YES];

    }

}


- (void) handle_VolumeChanged: (id) notification
{
      [volumeSlider setValue:[musicPlayer volume]];
}


- (void)didReceiveMemoryWarning
{
      [super didReceiveMemoryWarning];


}

#pragma mark - Controls

- (IBAction)playPause:(id)sender
{
    if ([musicPlayer playbackState] == MPMusicPlaybackStatePlaying) {

        [musicPlayer pause];

    }

   else {

        [musicPlayer play];
    }
}

- (IBAction)nextSong:(id)sender
{

      [musicPlayer skipToNextItem];
}

- (IBAction)previousSong:(id)sender
{

      [musicPlayer skipToPreviousItem];

}

- (IBAction)volumeSliderChanged:(id)sender
{

      [musicPlayer setVolume:volumeSlider.value];

}


@end