Reputation: 67
I created a very simple music player using MPMusicPlayerController that is working fine except the stop button (see picture below). When the music is playing and stop button is pressed, the app doesn't play anymore. I tried to terminate it when in background but the app doesn't work. I just can use the app again after press play on center control (picture two).
And my code
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
{
MPMusicPlayerController *playerController;
MPMediaQuery *query;
}
@end
@implementation ViewController
@synthesize musicTitleLabel;
- (void)viewDidLoad {
[super viewDidLoad];
playerController = [[MPMusicPlayerController alloc] init];
}
- (IBAction)playPressed:(id)sender {
query = [MPMediaQuery songsQuery];
MPMediaItem *item = [[query collections] objectAtIndex:0];
[playerController setNowPlayingItem:item];
[playerController play];
NSString *titleString = [item valueForProperty:MPMediaItemPropertyTitle];
musicTitleLabel.text = [NSString stringWithFormat:@"%@",titleString];
}
- (IBAction)pausePressed:(id)sender {
[playerController pause];
}
- (IBAction)stopPressed:(id)sender {
[playerController stop];
}
How can I fix that problem? What's the function o the Stop Method?
Upvotes: 1
Views: 468
Reputation: 38
try this:
MPMusicPlayerController *player = [MPMusicPlayerController systemMusicPlayer];
Playing media items with the systemMusicPlayer will replace the user's current Music state.
Upvotes: 1