IOS利用AVPlayer开发多媒体在线播放器
2014-05-31 13:52
120 查看
在这里给AVPlayer播放在线音频文件作个记号
1.在H文件中初始:
==============
AVPlayer *mp3Player;
AVPlayerItem *mp3PlayerItem;
id audioMix;
id volumeMixInput;
2.在MM文件中:
view source
print
?
01.
//作品播放
02.
NSURL * songUrl = [NSURL URLWithString:userInfo.songUrl];
03.
AVURLAsset *movieAsset = [[[AVURLAsset alloc]initWithURL:songUrl options:nil]autorelease];
04.
05.
06.
self. mp3PlayerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
07.
[self. mp3PlayerItem addObserver:self forKeyPath:@"status" options:0 context:NULL];
08.
self. mp3Player = [AVPlayer playerWithPlayerItem:self. mp3PlayerItem];
09.
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self. mp3Player];
10.
playerLayer.frame = self.view.layer.bounds;
11.
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
12.
[self.view.layer addSublayer:playerLayer];
13.
[self. mp3Player setAllowsExternalPlayback:YES];
3.实现代理方法:
view source
print
?
01.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
02.
{
03.
if ([keyPath isEqualToString:@"status"])
04.
{
05.
if (AVPlayerItemStatusReadyToPlay == self. mp3Player.currentItem.status)
06.
{
07.
[self. mp3Player play];
08.
}
09.
}
10.
}
4.现实音量调整
view source
print
?
01.
-(void) setVolume:(float)volume{
02.
//作品音量控制
03.
NSMutableArray *allAudioParams = [NSMutableArray array];
04.
AVMutableAudioMixInputParameters *audioInputParams =[AVMutableAudioMixInputParameters audioMixInputParameters];
05.
[audioInputParams setVolume:volume atTime:kCMTimeZero];
06.
[audioInputParams setTrackID:1];
07.
[allAudioParams addObject:audioInputParams];
08.
audioMix = [AVMutableAudioMix audioMix];
09.
[audioMix setInputParameters:allAudioParams];
10.
[self. mp3PlayerItem setAudioMix:audioMix]; // Mute the player item
11.
12.
[avAudioPlayer setVolume:volume];
13.
}
5.取得播放时间
view source
print
?
01.
- (NSTimeInterval) playableDuration
02.
{
03.
AVPlayerItem * item = self.worksPlayer.currentItem;
04.
if (item.status == AVPlayerItemStatusReadyToPlay) {
05.
return CMTimeGetSeconds(self.worksPlayer.currentItem.duration);
06.
}
07.
else
08.
{
09.
return(CMTimeGetSeconds(kCMTimeInvalid));
10.
}
11.
}
12.
- (NSTimeInterval) playableCurrentTime
13.
{
14.
AVPlayerItem * item = self.worksPlayer.currentItem;
15.
16.
if (item.status == AVPlayerItemStatusReadyToPlay) {
17.
NSLog(@"%f\n",CMTimeGetSeconds(self.worksPlayer.currentItem.currentTime));
18.
if (!playBeginState&&CMTimeGetSeconds(self.worksPlayer.currentItem.currentTime)==CMTimeGetSeconds(self.worksPlayer.currentItem.duration)) {
19.
[streamer stop];
20.
}
21.
playBeginState = NO;
22.
return CMTimeGetSeconds(self.worksPlayer.currentItem.currentTime);
23.
}
24.
else
25.
{
26.
return(CMTimeGetSeconds(kCMTimeInvalid));
27.
}
28.
}
1.在H文件中初始:
==============
AVPlayer *mp3Player;
AVPlayerItem *mp3PlayerItem;
id audioMix;
id volumeMixInput;
2.在MM文件中:
view source
?
01.
//作品播放
02.
NSURL * songUrl = [NSURL URLWithString:userInfo.songUrl];
03.
AVURLAsset *movieAsset = [[[AVURLAsset alloc]initWithURL:songUrl options:nil]autorelease];
04.
05.
06.
self. mp3PlayerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
07.
[self. mp3PlayerItem addObserver:self forKeyPath:@"status" options:0 context:NULL];
08.
self. mp3Player = [AVPlayer playerWithPlayerItem:self. mp3PlayerItem];
09.
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self. mp3Player];
10.
playerLayer.frame = self.view.layer.bounds;
11.
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
12.
[self.view.layer addSublayer:playerLayer];
13.
[self. mp3Player setAllowsExternalPlayback:YES];
3.实现代理方法:
view source
?
01.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
02.
{
03.
if ([keyPath isEqualToString:@"status"])
04.
{
05.
if (AVPlayerItemStatusReadyToPlay == self. mp3Player.currentItem.status)
06.
{
07.
[self. mp3Player play];
08.
}
09.
}
10.
}
4.现实音量调整
view source
?
01.
-(void) setVolume:(float)volume{
02.
//作品音量控制
03.
NSMutableArray *allAudioParams = [NSMutableArray array];
04.
AVMutableAudioMixInputParameters *audioInputParams =[AVMutableAudioMixInputParameters audioMixInputParameters];
05.
[audioInputParams setVolume:volume atTime:kCMTimeZero];
06.
[audioInputParams setTrackID:1];
07.
[allAudioParams addObject:audioInputParams];
08.
audioMix = [AVMutableAudioMix audioMix];
09.
[audioMix setInputParameters:allAudioParams];
10.
[self. mp3PlayerItem setAudioMix:audioMix]; // Mute the player item
11.
12.
[avAudioPlayer setVolume:volume];
13.
}
5.取得播放时间
view source
?
01.
- (NSTimeInterval) playableDuration
02.
{
03.
AVPlayerItem * item = self.worksPlayer.currentItem;
04.
if (item.status == AVPlayerItemStatusReadyToPlay) {
05.
return CMTimeGetSeconds(self.worksPlayer.currentItem.duration);
06.
}
07.
else
08.
{
09.
return(CMTimeGetSeconds(kCMTimeInvalid));
10.
}
11.
}
12.
- (NSTimeInterval) playableCurrentTime
13.
{
14.
AVPlayerItem * item = self.worksPlayer.currentItem;
15.
16.
if (item.status == AVPlayerItemStatusReadyToPlay) {
17.
NSLog(@"%f\n",CMTimeGetSeconds(self.worksPlayer.currentItem.currentTime));
18.
if (!playBeginState&&CMTimeGetSeconds(self.worksPlayer.currentItem.currentTime)==CMTimeGetSeconds(self.worksPlayer.currentItem.duration)) {
19.
[streamer stop];
20.
}
21.
playBeginState = NO;
22.
return CMTimeGetSeconds(self.worksPlayer.currentItem.currentTime);
23.
}
24.
else
25.
{
26.
return(CMTimeGetSeconds(kCMTimeInvalid));
27.
}
28.
}
相关文章推荐
- IOS利用AVPlayer开发多媒体在线播放器
- iOS开发-- 利用AVPlayer播放远程音乐和视频
- iOS开发之多媒体播放
- 【iOS-Cocos2d游戏开发之十三】CCSprite利用Bezier(贝塞尔)做抛物线动作并让CCSprite同时播放两个Action动作!
- iOS开发笔记——利用segue传参
- (ios开发学习笔记四)利用toolbar实现多窗体跳转
- IOS开发:多媒体-音频的操作(一) 短声音的播放
- 利用Trimedia的TSSA组件技术进行多媒体开发(转)
- Android多媒体开发【2】-- 利用VideoView开发最简单的视频播放器
- 【iOS-cocos2d-X 游戏开发之九】Cocos2dx利用CCSAXParser解析xml数据 推荐
- 【iOS-Cocos2d游戏开发之十四】音频/音效/视频播放(利用Cocos2D-iPhone-Extensions嵌入Cocos2d进行视频播放!)
- 在IOS上利用ArcGIS API For IOS进行移动GIS开发新建程序的配置步骤
- 【iOS-Cocos2d游戏开发之十三】CCSprite利用Bezier(贝塞尔)抛物线并同时播放两个Action动作! 推荐
- 【iOS-Cocos2d游戏开发之十三】CCSprite利用Bezier(贝塞尔)做抛物线动作并让CCSprite同时播放两个Action动作!
- iPhone 和 iPad的ios 开发中 利用 WebViewJavascriptBridge组件,通过 UIWebView 对Html进行双向通讯
- iOS开发之多媒体播放
- iOS开发之多媒体播放
- 【iOS-Cocos2d游戏开发之二十】精灵的基础知识点总汇(位图操作/贴图更换/重排z轴等)以及利用CCSprite与CCLayerColor制作简单遮盖层!
- iOS开发之多媒体播放
- 【iOS-Cocos2d游戏开发之十三】CCSprite利用Bezier(贝塞尔)抛物线并同时播放两个Action动作!