iOS7 Programming Cookbook 第十二章学习笔记

####Accessing the Music Library

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<MPMediaPickerControllerDelegate,AVAudioPlayerDelegate>
//音乐播放界面
@property (nonatomic, strong) MPMusicPlayerController *myMusicPlayer;
//挑选和播放
@property (nonatomic, strong) UIButton *buttonPickAndPlay;
//停止播放
@property (nonatomic, strong) UIButton *buttonStopPlaying;
//媒体采集vc
@property (nonatomic, strong) MPMediaPickerController *mediaPicker;
@end

@implementation ViewController

- (void) musicPlayerStateChanged:(NSNotification *)paramNotification{

NSLog(@"播放状态改变");

/* 得到播放状态 */
NSNumber *stateAsObject = [paramNotification.userInfo objectForKey:@"MPMusicPlayerControllerPlaybackStateKey"];

NSInteger state = [stateAsObject integerValue];

/* 根据state判断 */
switch (state){
case MPMusicPlaybackStateStopped:{
/* 媒体播放器已停止播放队列. */
break;
}
case MPMusicPlaybackStatePlaying:{
/* 更多处理能力的player */
break;
}
case MPMusicPlaybackStatePaused:{
/* player播放一下,你可以通过图形显示给用户*/
break;
}
case MPMusicPlaybackStateInterrupted:{
/* 一个中断停止播放媒体列表 */
break;
}
case MPMusicPlaybackStateSeekingForward:{
/* 用户正在寻求队列中 */
break;
}
case MPMusicPlaybackStateSeekingBackward:{
/* 用户正在寻求向后队列 */
break;
}
} /* switch (State){ */

}

<!-- more -->

- (void) nowPlayingItemIsChanged:(NSNotification *)paramNotification{

NSLog(@"Playing Item Is Changed");

NSString *persistentID =[paramNotification.userInfo
objectForKey:@"MPMusicPlayerControllerNowPlayingItemPersistentIDKey"];

NSLog(@"Persistent ID = %@", persistentID);

}

- (void) volumeIsChanged:(NSNotification *)paramNotification{
NSLog(@"Volume Is Changed");
/* The userInfo dictionary of this notification is normally empty */
}


//delegate modal 一个vc在一个父类vc
- (void) mediaPicker:(MPMediaPickerController *)mediaPicker
didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection{

NSLog(@"Media Picker returned");

/* 首先,创建了音乐播放器,让我们释放它 */
self.myMusicPlayer = nil;
//创建
self.myMusicPlayer = [[MPMusicPlayerController alloc] init];
//开始播放生成通知
[self.myMusicPlayer beginGeneratingPlaybackNotifications];

/* notificationcenter接受通知 */
//通知相关的对象是音乐播放器的播放状态发生了变化
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(musicPlayerStateChanged:) name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:self.myMusicPlayer];

/*   播放得到通知。我们只会允许
  我们的用户选择一个音乐文件 */

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(nowPlayingItemIsChanged:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:self.myMusicPlayer];

/* 音乐播放器改变 */
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(volumeIsChanged:) name:MPMusicPlayerControllerVolumeDidChangeNotification object:self.myMusicPlayer];

/* 开始项目集合 */
[self.myMusicPlayer setQueueWithItemCollection:mediaItemCollection];
[self.myMusicPlayer play];

/* Finally dismiss the media picker controller */
[mediaPicker dismissViewControllerAnimated:YES completion:nil];

}

- (void) mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker{

/* player 选择取消*/
NSLog(@"Media Picker was cancelled");
[mediaPicker dismissViewControllerAnimated:YES completion:nil];

}

- (void) stopPlayingAudio{

if (self.myMusicPlayer != nil){
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:self.myMusicPlayer];

[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:self.myMusicPlayer];

[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMusicPlayerControllerVolumeDidChangeNotification object:self.myMusicPlayer];

[self.myMusicPlayer stop];
}

}

//显示媒体选择和音乐项目
- (void)displayMediaPickerAndPlayItem{

self.mediaPicker =
[[MPMediaPickerController alloc]
initWithMediaTypes:MPMediaTypeAnyAudio];

if (self.mediaPicker != nil){

NSLog(@"Successfully instantiated a media picker.");
self.mediaPicker.delegate = self;
self.mediaPicker.allowsPickingMultipleItems = YES;
self.mediaPicker.showsCloudItems = YES;
self.mediaPicker.prompt = @"Pick a song please...";

[self.view addSubview:self.mediaPicker.view];

[self.navigationController presentViewController:self.mediaPicker
animated:YES
completion:nil];

} else {
NSLog(@"Could not instantiate a media picker.");
}

}

- (void)viewDidLoad {
[super viewDidLoad];
//title
self.title = @"Media picker...";

self.buttonPickAndPlay = [UIButton buttonWithType:UIButtonTypeSystem];
self.buttonPickAndPlay.frame = CGRectMake(0.0f,0.0f,200,37.0f);
self.buttonPickAndPlay.center = CGPointMake(self.view.center.x,
self.view.center.y - 50);
[self.buttonPickAndPlay setTitle:@"Pick and Play" forState:UIControlStateNormal];
[self.buttonPickAndPlay addTarget:self action:@selector(displayMediaPickerAndPlayItem) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.buttonPickAndPlay];

self.buttonStopPlaying = [UIButton buttonWithType:UIButtonTypeSystem];
self.buttonStopPlaying.frame = CGRectMake(0.0f,0.0f,200,37.0f);
self.buttonStopPlaying.center = CGPointMake(self.view.center.x, self.view.center.y + 50);
[self.buttonStopPlaying setTitle:@"Stop Playing"
forState:UIControlStateNormal];
[self.buttonStopPlaying addTarget:self
action:@selector(stopPlayingAudio)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.buttonStopPlaying];

}

@end

Reference

坚持原创技术分享,您的支持将鼓励我继续创作!
0%