IOS利用AVPlayer开发多媒体在线播放器

这篇博客介绍了如何在iOS应用中使用AVPlayer组件播放在线音频文件,包括初始化AVPlayer,设置播放源,监听播放状态,调整音量以及获取播放时间的方法。通过观察播放器的状态来控制播放,并提供了音量调整的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里给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.

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值