博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
访问音乐库
阅读量:6807 次
发布时间:2019-06-26

本文共 7178 字,大约阅读时间需要 23 分钟。

#import 
#import
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;@property (weak, nonatomic) IBOutlet UISlider *volumeSlider;@property (weak, nonatomic) IBOutlet UIButton *playButton;@property (weak, nonatomic) IBOutlet UITextField *artistTextField;@property (strong, nonatomic) MPMediaItemCollection *myCollection;@property (strong, nonatomic) MPMusicPlayerController *player;- (IBAction)addItems:(id)sender;- (IBAction)prevTapped:(id)sender;- (IBAction)playTapped:(id)sender;- (IBAction)nextTapped:(id)sender;- (IBAction)updateVolume:(id)sender;- (IBAction)queueMusicByArtist:(id)sender;@end
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize infoLabel;@synthesize volumeSlider;@synthesize playButton;@synthesize artistTextField;- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    self.infoLabel.text = @"...";        self.player = [MPMusicPlayerController applicationMusicPlayer];        [self setNotifications];        [self.player beginGeneratingPlaybackNotifications];        [self.player setShuffleMode:MPMusicShuffleModeOff];    self.player.repeatMode = MPMusicRepeatModeNone;        self.volumeSlider.value = self.player.volume;        self.artistTextField.delegate = self;    self.artistTextField.enablesReturnKeyAutomatically = YES;}-(void)setNotifications{    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];        [notificationCenter     addObserver: self     selector:    @selector(handleNowPlayingItemChanged:)     name:        MPMusicPlayerControllerNowPlayingItemDidChangeNotification     object:      self.player];        [notificationCenter     addObserver: self     selector:    @selector(handlePlaybackStateChanged:)     name:        MPMusicPlayerControllerPlaybackStateDidChangeNotification     object:      self.player];        [notificationCenter     addObserver: self     selector:    @selector(handleVolumeChangedFromHardware:)     name:        @"AVSystemController_SystemVolumeDidChangeNotification"     object:      nil];}-(void)handleVolumeChangedFromHardware:(id)sender{    [self.volumeSlider setValue:self.player.volume animated:YES];}- (void) handlePlaybackStateChanged: (id) notification{    MPMusicPlaybackState playbackState = [self.player playbackState];        if (playbackState == MPMusicPlaybackStateStopped)    {        [self.playButton setTitle:@"Play" forState:UIControlStateNormal];    }    else if (playbackState == MPMusicPlaybackStatePaused)    {        [self.playButton setTitle:@"Play" forState:UIControlStateNormal];    }    else if (playbackState == MPMusicPlaybackStatePlaying)    {        [self.playButton setTitle:@"Pause" forState:UIControlStateNormal];    }}- (void) handleNowPlayingItemChanged: (id) notification{    MPMediaItem *currentItemPlaying = [self.player nowPlayingItem];    if (currentItemPlaying)    {        NSString *info = [NSString stringWithFormat:@"%@ - %@", [currentItemPlaying valueForProperty:MPMediaItemPropertyTitle], [currentItemPlaying valueForProperty:MPMediaItemPropertyArtist]];        self.infoLabel.text = info;    }    else    {        self.infoLabel.text = @"...";    }}-(void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker{    [self dismissViewControllerAnimated:YES completion:NULL];}-(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection{    [self updateQueueWithMediaItemCollection:mediaItemCollection];    [self dismissViewControllerAnimated:YES completion:NULL];}-(void)updateQueueWithMediaItemCollection:(MPMediaItemCollection *)collection{    if (collection)    {        if (self.myCollection == nil)        {            self.myCollection = collection;            [self.player setQueueWithItemCollection: self.myCollection];            [self.player play];        }        else        {            BOOL wasPlaying = NO;            if (self.player.playbackState == MPMusicPlaybackStatePlaying)            {                wasPlaying = YES;            }                        MPMediaItem *nowPlayingItem        = self.player.nowPlayingItem;            NSTimeInterval currentPlaybackTime = self.player.currentPlaybackTime;                        NSMutableArray *combinedMediaItems =            [[self.myCollection items] mutableCopy];            NSArray *newMediaItems = [collection items];            [combinedMediaItems addObjectsFromArray: newMediaItems];                        self.myCollection = [MPMediaItemCollection collectionWithItems:combinedMediaItems];                        [self.player setQueueWithItemCollection:self.myCollection];                        self.player.nowPlayingItem      = nowPlayingItem;            self.player.currentPlaybackTime = currentPlaybackTime;                        if (wasPlaying)            {                [self.player play];            }        }    }}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (IBAction)addItems:(id)sender{    MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];    picker.delegate = self;    picker.allowsPickingMultipleItems = YES;    picker.prompt =    NSLocalizedString (@"Add songs to play",                       "Prompt in media item picker");    [self presentViewController:picker animated:YES completion:NULL];}- (IBAction)prevTapped:(id)sender{    if ([self.player currentPlaybackTime] > 5.0)    {        [self.player skipToBeginning];    }    else    {        [self.player skipToPreviousItem];    }}- (IBAction)playTapped:(id)sender{    if ((self.myCollection != nil) && (self.player.playbackState != MPMusicPlaybackStatePlaying))    {        [self.player play];        [self.playButton setTitle:@"Pause" forState:UIControlStateNormal];    }    else if (self.player.playbackState == MPMusicPlaybackStatePlaying)    {        [self.player pause];        [self.playButton setTitle:@"Play" forState:UIControlStateNormal];    }}- (IBAction)nextTapped:(id)sender{    [self.player skipToNextItem];}- (IBAction)updateVolume:(id)sender{    self.player.volume = self.volumeSlider.value;}- (IBAction)queueMusicByArtist:(id)sender{    NSString *artist = self.artistTextField.text;    if (artist != nil && artist != @"")    {        MPMediaPropertyPredicate *artistPredicate = [MPMediaPropertyPredicate predicateWithValue:artist forProperty:MPMediaItemPropertyArtist comparisonType:MPMediaPredicateComparisonContains];        MPMediaQuery *query = [[MPMediaQuery alloc] init];        [query addFilterPredicate:artistPredicate];                NSArray *result = [query items];        if ([result count] > 0)        {            [self updateQueueWithMediaItemCollection:[MPMediaItemCollection collectionWithItems:result]];        }        else            self.infoLabel.text = @"Artist Not Found.";    }}-(BOOL)textFieldShouldReturn:(UITextField *)textField{    [textField resignFirstResponder];    [self queueMusicByArtist:self];    return NO;}

 

转载地址:http://zotwl.baihongyu.com/

你可能感兴趣的文章
mysql-mmm+amoeba+keepalived实现mysql高可用和读写分离(二)
查看>>
Java的IO演进
查看>>
[xmind] ASP.NET 设计模式 - UX 用户体验
查看>>
Productivity Power Tools(Visual Studio 扩展) 最新亮点
查看>>
VMware View 5.0 策略列表
查看>>
svn版本内容信息存放路径
查看>>
历届奥斯卡获奖影片(1971-2014年)
查看>>
企业园区全面安防面临的问题及解决之道
查看>>
Head First Design Pattern: 策略模式
查看>>
我的友情链接
查看>>
摩游世纪CEO宋啸飞:Html5增长趋势已可见
查看>>
react学习笔记10:显示隐藏效果和tab切换效果
查看>>
i386 Linux 系统调用
查看>>
JAVA字符编码系列一:Unicode,GBK,GB2312,UTF-8概念基础
查看>>
文字服务和输入语言检测到不兼容的键盘驱动程序该对话框已被停用!解决方法...
查看>>
多路复用输入/输出 ---- select
查看>>
Mysql 基本命令
查看>>
我的友情链接
查看>>
storm问题汇总
查看>>
我选择 wxWidgets 而不是 Qt 作为图形用户界面框架的一些想法
查看>>