弱弱的问下,avplayer 播放结束怎么播放

Play live URL streaming by AVPlayer [现场演奏的avplayer URL流] - 问题-字节技术
Play live URL streaming by AVPlayer
现场演奏的avplayer URL流
问题 (Question)
MY Real URL =& "http://125.209.70.43:1500/"
As my title said that I want to play music from my live streaming URL. I tried with following code. (following URL is for testing not real)
[self playSelectedSong:@"http://media-/HeartPlymouthV1"]; // custom method
with testing URL
Code of method.
-(void)playSelectedSong:(NSString *) urlString
self.songPlayer = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:urlString]];;
[self.songPlayer play];
I have Question like.
1) What should proper URL Pattern? is streaming URL has any rules about its pattern or not ?
For example above code is woking for streaming URL (NOT mine url) such like,
1) http://108.178.57.196:11112/los40principales
2) http://media-/HeartPlymouthV1
in my URL it is not working (for iOS Devices), my URL pattern is
http://125.209.70.43:1500/
My streaming URL pattern looking like above.
Then how can I play music by using above URL ? My above code is not working for http://125.209.70.43:1500/. Anybody have issue related as a mine ?
Please help me on this issue.
NOTE: My URL is correct and its working, I checked with vlc media player network streaming. by
=& VLC Medial Player
-& File Menu
-& Open Network
-& URL set as "http://125.209.70.43:1500/"
-& click on "Open" button.
Try with above step it's working very well.
MY Real URL =& "http://125.209.70.43:1500/"
我的标题说,我想从我的直播流媒体播放音乐URL。我试着用下面的代码。(以下URL是不是真的)测试[self playSelectedSong:@"http://media-/HeartPlymouthV1"]; // custom method
with testing URL
代码的方法。-(void)playSelectedSong:(NSString *) urlString
self.songPlayer = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:urlString]];;
[self.songPlayer play];
我有问题。1)正确的应该是什么URL模式流URL有图案或不规则?例如上面的代码工作URL (不是我的网址)如, 1) http://108.178.57.196:11112/los40principales
2) http://media-/HeartPlymouthV1
但在我的URL它不工作(针对iOS设备),我URL模式
http://125.209.70.43:1500/
我的流URL像上面的图案。那我怎么能利用上述播放音乐URL?我上面的代码是不工作的http://125.209.70.43:1500/。任何人都有相关的为我的问题?请帮我把这个问题。
笔记我的地址是正确的,它的工作,我检查了VLC媒体播放器网络流。由=& VLC Medial Player
-& File Menu
-& Open Network
-& URL set as "http://125.209.70.43:1500/"
-& click on "Open" button.
尝试用以上步骤的工作非常好。
最佳答案 (Best Answer)
Your stream is Windows Media Audio, that's not going to work with AVPlayer. If you want to support streaming to iOS devices, check out /streaming/
你是Windows媒体音频流,那是不想和avplayer。如果你想支持流到iOS设备,检查/streaming/
本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:iOS AvPlayer AvAudioPlayer音频的后台播放问题
iOS AvPlayer AvAudioPlayer音频的后台播放问题
发布时间: 3:44:07
编辑:www.fx114.net
本篇文章主要介绍了"iOS AvPlayer AvAudioPlayer音频的后台播放问题",主要涉及到iOS AvPlayer AvAudioPlayer音频的后台播放问题方面的内容,对于iOS AvPlayer AvAudioPlayer音频的后台播放问题感兴趣的同学可以参考一下。
iOS 4开始引入的multitask,我们可以实现像ipod程序那样在后台播放音频了。如果音频操作是用苹果官方的AVFoundation.framework实现,像用AvAudioPlayer,AvPlayer播放的话,要实现完美的后台音频播放,依据app的功能需要,可能需要实现几个关键的功能。
首先,播放音频之前先要设置AVAudioSession模式,通常只用来播放的App可以设为AVAudioSessionCategoryPlayback即可。模式意义及其他模式请参考文档。
[plain]&view
AVAudioSession&*session&=&[AVAudioSession&sharedInstance];&&
[session&setCategory:AVAudioSessionCategoryPlayback&error:nil];&&
[session&setActive:YES&error:nil];&&
1。通知OS该app支持background audio。缺省情况下,当按下home键时,当前正在运行的程序被suspend,状态从active变成in-active,也就是说如果正在播放音频,按下HOME后就会停止。这里需要让app在按在HOME后,转到后台运行而非被suspend,解决办法是在程序的-info.plist中增加required background modes这个key项,并选择App plays audio这个value项。
2。现在按下HOME键后,程序退到后台,但是声音仍在播放。但是如果要实现播放列表的依次播放、循环播放,即放完一首后自动切换到下一首,问题来了,当App在后台放完一首后,就会停下来。原因是在后台运行时,一旦声音停下来,程序也随之suspend,因此在切换文件加载的间隙,程序就会被suspend。曾经有山寨的解决办法是专门起一个player的实例连续不停的放同一无声音片断,阻止程序被suspend。这里提供的方法是通过申请后台taskID达到后台切换播放文件的功能。
即声明后台task id,并通过beginBackgroundTaskWithExpirationHandler将App设为后台Task,达到持续后台运行的目的。我们知道一般情况下,按HOME将程序送到后台,可以有5或10秒时间可以进行一些收尾工作,具体时间[[UIApplication sharedApplication] backgroundTimeRemaining]返回值。超时后app会被suspend,现在要做的就是用[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL]开始后台任务,可以将后台运行超时时间长时间的延长,具体延长多少时间还是见返回值,总之对于放段时间音乐应该够了。另一个问题是每个开始的后台任务,都必须用endBackgroundTask来结束。
因此,在每次开始播放后启动新的后台任务,同时结束上一个后台任务:
首先,要在viewdidload中
[plain]&view
[UIApplication&sharedApplication]&beginReceivingRemoteControlEvents];&&
否则,无法切换到下一首,接下来
[plain]&view
UIBackgroundTaskIdentifier&newTaskId&=&UIBackgroundTaskI&&
[avPlayer&play];&&
newTaskId&=&[[UIApplication&sharedApplication]&beginBackgroundTaskWithExpirationHandler:NULL];&&
if&(newTaskId&!=&UIBackgroundTaskInvalid&&&&oldTaskId&!=&UIBackgroundTaskInvalid)&{&&
&&&&&[[UIApplication&sharedApplication]&endBackgroundTask:&oldTaskId];}&&&
oldTaskId&=&newTaskId;&&
当然,还有更方便的办法就是在resignActive时beginBackgroundTaskWithExpirationHandler:并在BecomeActive中endBackgroundTask:&
3。我们知道,ipod播放程序在后台时,双击HOME键,会有个控制界面,可以对它进行播放控制。
如果您想让您的app可以像ipod一样在后台也可以方便的通过双击HOME键来控制,就要用到远程控制事件了。
首先在viewdidload等初始化的地方声明App接收远程控制事件,并在相应地方结束声明
[plain]&view
-&(void)&viewWillAppear:(BOOL)animated&&
[super&viewWillAppear:animated];&&
[UIApplication&sharedApplication]&beginReceivingRemoteControlEvents];&&
[self&becomeFirstResponder];&&
-&(void)&viewWillDisappear:(BOOL)animated&&
[super&viewWillDisappear:animated];&&
[UIApplication&sharedApplication]&endReceivingRemoteControlEvents];&&
[self&resignFirstResponder];&&
[plain]&view
-&(BOOL)canBecomeFirstResponder&&
&&&&&&&return&YES;&&
最后定义&remoteControlReceivedWithEvent,处理具体的播放、暂停、前进、后退等具体事件
[plain]&view
-&(void)&remoteControlReceivedWithEvent:&(UIEvent&*)&receivedEvent&{&&&&&&
&&&&if&(receivedEvent.type&==&UIEventTypeRemoteControl)&{&&
&&&&&&&&switch&(receivedEvent.subtype)&{&&
&&&&&&&&&&&&case&UIEventSubtypeRemoteControlTogglePlayPause:&&
&&&&&&&&&&&&&&&&[self&playButtonPressed:playButton];&&
&&&&&&&&&&&&&&&&[self&testing];&&
&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&case&UIEventSubtypeRemoteControlPreviousTrack:&&
&&&&&&&&&&&&&&&&[self&rewButtonReleased:(UIButton&*)rewButton];&&
&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&case&UIEventSubtypeRemoteControlNextTrack:&&
&&&&&&&&&&&&&&&&[self&ffwButtonReleased:(UIButton&*)ffwButton];&&
&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&default:&&
&&&&&&&&&&&&&&&&&&
&&&&&&&&}&&
4. 至此,您有播放App已经相当完美了,还有最后一个问题,那就是当用户使用耳机时,问题又来了。系统默认当插入耳机时,正在播放的声音不中断,直接切换到耳机播放,而当拔出耳机时,播放停止。如果这种行为满足您的要求,那OK,否则您就需要进一步研究耳机检测和声音路由切换的问题。
本文标题:
本页链接:下次自动登录
现在的位置:
& 综合 & 正文
iOS编程——AVPlayer解决闪屏问题
问题终于解决,选用AVPlayer,重置一下AVPlayerItem就不会出现闪屏现象,注册一个通知同样可以实现循环播放,而且AVPlayer可以自定义播放样式,给了我们更大的发挥空间,完全可以自己DIY播放器样式。并且AVPlayer完全可以实现两个视频窗口播放!
1.需要引入两个类库:
在.h中引入
&AVFoundation/AVFoundation.h&
在.m中引入
&CoreMedia/CoreMedia.h&
- (void)viewDidLoad
[super viewDidLoad];
UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
rightBtn.frame = CGRectMake(50, 420, 180, 50);
[rightBtn addTarget:self action:@selector(doRight) forControlEvents:UIControlEventTouchUpInside];
[rightBtn setTitle:@"同时播放" forState:UIControlStateNormal];
[self.view addSubview:rightBtn];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"找朋友" ofType:@"mp4"];
NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];
AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
AVPlayerItem * playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
self.player_0 = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player_0];
playerLayer.frame = CGRectMake(0, 0, 400, 500);
playerLayer.videoGravity = AVLayerVideoGravityResizeA
[self.view.layer addSublayer:playerLayer];
[_player_0 play];
//注册通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(runLoopTheMovie:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
- (void)doRight
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"字母歌" ofType:@"mp4"];
NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];
AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
self.player_1 = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer * playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player_1];
playerLayer.frame = CGRectMake(420, 0, 400, 500);
playerLayer.videoGravity = AVLayerVideoGravityResizeA
[self.view.layer addSublayer:playerLayer];
[_player_1 play];
- (void)runLoopTheMovie:(NSNotification *)n{
//注册的通知
可以自动把 AVPlayerItem 对象传过来,只要接收一下就OK
AVPlayerItem * p = [n object];
//关键代码
[p seekToTime:kCMTimeZero];
[_player_0 play];
NSLog(@"重播");
Demo链接:http://download.csdn.net/detail/u14853
运行环境:Xcode 4.6.3 ARC
&&&&推荐文章:
【上篇】【下篇】

我要回帖

更多关于 avplayer 播放结束 的文章

 

随机推荐