0
我有一個叫做音樂的按鈕,當我點擊它時,MPMediaPickerController彈出並選擇播放哪首歌曲。它在應用程序中運行良好,但是當我退出應用程序並再次進入應用程序時,音樂停止。任何想法?當我使用MPMusicPlayerController重新打開應用程序時,如何恢復音樂
ViewController.h:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController <MPMediaPickerControllerDelegate>
{
MPMusicPlayerController *musicPlayer;
IBOutlet UIButton *music;
IBOutlet UIButton *stopmusic;
}
- (IBAction)music:(id)sender;
@end
ViewController.m:
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
- (IBAction)stopmusic:(id)sender
{
[musicPlayer stop];
}
- (IBAction)music:(id)sender
{
MPMediaPickerController *musicSelect;
musicSelect = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
musicSelect.delegate = self;
[self presentViewController:musicSelect animated:YES completion: nil];
}
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker
didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
[musicPlayer setQueueWithItemCollection:(MPMediaItemCollection *)mediaItemCollection];
[self dismissViewControllerAnimated:YES completion:nil];
[musicPlayer play];
}
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
當我試圖建立一個簡單的音樂播放器,但沒有投入時間來弄清楚爲什麼會發生這種情況時,我遇到了同樣的問題。我建議尋找'MPMusicPlayerControllerPlaybackStateDidChangeNotification'這應該可以幫助你在播放狀態改變時進行調試。 – AdamPro13