AVPlayer(或AVAudioSession?)在我离开ViewController后播放,当我返回时,两个播放器正在播放
我有一个viewController,它可以从web流式传输音频。当我离开我的应用程序页面时,音频会一直播放(这很好)。但是,当我返回时,viewdidload方法会创建第二个audioplayer。我可以一遍又一遍地这样做,直到我有这么多的球员。听起来真是不可思议。这是我所有的相关代码AVPlayer(或AVAudioSession?)在我离开ViewController后播放,当我返回时,两个播放器正在播放,ios,objective-c,audio,avplayer,avaudiosession,Ios,Objective C,Audio,Avplayer,Avaudiosession,我有一个viewController,它可以从web流式传输音频。当我离开我的应用程序页面时,音频会一直播放(这很好)。但是,当我返回时,viewdidload方法会创建第二个audioplayer。我可以一遍又一遍地这样做,直到我有这么多的球员。听起来真是不可思议。这是我所有的相关代码 #import "ListenViewController.h" #import <AVFoundation/AVAudioPlayer.h> #import <AVFoundation/AV
#import "ListenViewController.h"
#import <AVFoundation/AVAudioPlayer.h>
#import <AVFoundation/AVPlayerItem.h>
#import <AVFoundation/AVPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import "RSPlayPauseButton.h"
@interface ListenViewController () <AVAudioPlayerDelegate, AVAudioSessionDelegate>
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerItem *playerItem;
@property (nonatomic, strong) RSPlayPauseButton *playPauseButton;
@property (nonatomic, copy) NSDate *now;
@end
@implementation ListenViewController
static int min = -2;
-(void) viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
//[self.player pause];
min = -2;
}
-(void) viewDidLoad
{
[super viewDidLoad];
/*
if ([AVAudioSession sharedInstance].isInputAvailable) {
NSLog(@"XXXXXXXXX");
}
*/
NSURL *streamingURL = [NSURL URLWithString:@"http://URL.m3u"];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:streamingURL];
self.playerItem = playerItem;
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
[player setAllowsExternalPlayback:NO];
CMTime tm = CMTimeMake(1, 1);
[player addPeriodicTimeObserverForInterval:tm
queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
/*
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss:mmm"];
NSLog(@"%@",[DateFormatter stringFromDate:[NSDate date]]);
*/
if (!self.now) {
self.now = [NSDate date];
}
/*
NSDate *now = [NSDate date];
NSTimeInterval interval = [now timeIntervalSinceDate:self.now];
self.interval += 0.01;
interval -= (10 * self.rw2);
interval -= (60 * self.rw3);
interval += (10 * self.ff2);
self.timeLabel.text = [NSString stringWithFormat:@"%f", interval];
NSLog(@"INTERVAL: %@", [NSString stringWithFormat:@"%f", self.interval]);
*/
if (CMTimeGetSeconds(self.player.currentTime) >= 0) {
self.timeLabel.text = [NSString stringWithFormat:@"%.0f", CMTimeGetSeconds(self.player.currentTime)];
NSTimeInterval time = CMTimeGetSeconds(self.player.currentTime);
//int min = time/60;
int sec = lroundf(time) % 60;
if (sec == 0) {
++min;
}
NSLog(@"sec: %d", sec);
// update your UI with timeLeft
self. timeLabel.text = [NSString stringWithFormat:@"%.2d:%.2d", min,sec];
}
}];
[self setPlayer:player];
[player play];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
if (!self.playPauseButton) {
self.playPauseButton = [[RSPlayPauseButton alloc] initWithFrame:self.view.frame];
self.playPauseButton.tintColor = [UIColor colorWithRed:1.0 green:155.0/255.0 blue:0.0 alpha:1.0];
self.playPauseButton.animationStyle = RSPlayPauseButtonAnimationStyleSplitAndRotate;
self.playPauseButton.paused = NO;
[self.playPauseButton addTarget:self action:@selector(playPauseButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
}
[self.view addSubview:self.playPauseButton];
}
- (IBAction)rewind:(id)sender
{
//NSLog(@"currenttime: %f", CMTimeGetSeconds(self.player.currentTime));
//NSLog(@"timescale: %d", self.player.currentTime.timescale);
CMTime cmTime = CMTimeMake(CMTimeGetSeconds(self.player.currentTime) - 1.0, 1);
CMTime zero = CMTimeMake(1, 2);
if (CMTimeGetSeconds(cmTime) > CMTimeGetSeconds(zero)) {
[self.player.currentItem seekToTime:cmTime];
NSLog(@"!");
}
NSLog(@"RW");
}
- (IBAction)rewind2:(id)sender
{
/*
CMTime cmTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(self.player.currentTime) - 10.0, self.player.currentTime.timescale);
CMTime zero = CMTimeMake(0, 10000);
if (CMTimeGetSeconds(cmTime) > CMTimeGetSeconds(zero)) {
[self.player.currentItem seekToTime:cmTime];
self.rw2 += 1;
NSLog(@"RW2!");
}
*/
NSLog(@"RW2");
CMTime cmTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(self.player.currentTime) - 10.0, self.player.currentTime.timescale);
CMTime zero = CMTimeMake(1, 2);
NSLog(@"cmTime: %f", CMTimeGetSeconds(cmTime));
NSLog(@"zero: %f", CMTimeGetSeconds(zero));
if (CMTimeGetSeconds(cmTime) > CMTimeGetSeconds(zero)) {
[self.player.currentItem seekToTime:cmTime];
NSLog(@"!");
} else {
[self rewindAll];
}
}
- (IBAction)rewind3:(id)sender
{
CMTime cmTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(self.player.currentTime) - 60.0, self.player.currentTime.timescale);
CMTime zero = CMTimeMake(0, 10000);
if (CMTimeGetSeconds(cmTime) > CMTimeGetSeconds(zero)) {
[self.player.currentItem seekToTime:cmTime];
NSLog(@"RW3!");
}
NSLog(@"RW3");
}
- (void) rewindAll
{
CMTime one = CMTimeMake(1, 1);
[self.player.currentItem seekToTime:one];
}
- (IBAction)fastForward:(id)sender
{
CMTime cmTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(self.player.currentTime) + 5.0, self.player.currentTime.timescale);
NSDate *now = [NSDate date];
NSTimeInterval interval = [now timeIntervalSinceDate:self.now];
CMTime seekingCM = CMTimeMake(interval, 1);
if (CMTimeGetSeconds(cmTime) < CMTimeGetSeconds(seekingCM)) {
[self.player.currentItem seekToTime:cmTime];
}
}
- (IBAction)fastForward2:(id)sender
{
CMTime cmTime = CMTimeMakeWithSeconds(CMTimeGetSeconds(self.player.currentTime) + 10.0, self.player.currentTime.timescale);
NSDate *now = [NSDate date];
NSTimeInterval interval = [now timeIntervalSinceDate:self.now];
CMTime seekingCM = CMTimeMake(interval, 1);
if (CMTimeGetSeconds(cmTime) < CMTimeGetSeconds(seekingCM)) {
[self.player.currentItem seekToTime:cmTime];
NSLog(@"FF2!");
} else {
[self fastForward3:nil];
}
}
- (IBAction)fastForward3:(id)sender
{
NSDate *now = [NSDate date];
NSLog(@"FIRSTDATE: %@", self.now);
NSTimeInterval interval = [now timeIntervalSinceDate:self.now];
interval -= 0.5f;
NSLog(@"INT: %f", interval);
CMTime seekingCM = CMTimeMake(interval, 1);
/*
NSTimeInterval interval = self.interval - 1.0;
CMTime seekingCM = CMTimeMake(interval, 1);
[self.player.currentItem seekToTime:seekingCM];
*/
//CMTime seekingCM = CMTimeMake(self.interval, 1);
[self.player.currentItem seekToTime:seekingCM];
NSLog(@"FF3!");
}
- (NSTimeInterval)currentPlaybackTime
{
return CMTimeGetSeconds(self.player.currentItem.currentTime);
}
- (void)setCurrentPlaybackTime:(NSTimeInterval)time
{
CMTime cmTime = CMTimeMakeWithSeconds(time, NSEC_PER_SEC);
[self.player.currentItem seekToTime:cmTime];
}
- (void)viewDidLayoutSubviews
{
[self.playPauseButton sizeToFit];
self.playPauseButton.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 1.2);
}
- (void)playPauseButtonDidPress:(RSPlayPauseButton *)playPauseButton
{
[playPauseButton setPaused:!playPauseButton.isPaused animated:YES];
if (playPauseButton.isPaused) {
[self.player pause];
}
else {
[self.player play];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#导入“ListenViewController.h”
#进口
#进口
#进口
#进口
#导入“rsplaybausebutton.h”
@接口ListenViewController()
@属性(非原子,强)AVPlayer*player;
@属性(非原子,强)AVPlayerItem*playerItem;
@属性(非原子,强)RSPlayPauseButton*playPauseButton;
@属性(非原子,副本)NSDate*now;
@结束
@实现ListenViewController
静态int min=-2;
-(无效)视图消失:(BOOL)已设置动画
{
[超级视窗消失:动画];
//[self.player暂停];
最小值=-2;
}
-(无效)viewDidLoad
{
[超级视图下载];
/*
如果([AVAudioSession sharedInstance].isInputAvailable){
NSLog(@“XXXXXXXXX”);
}
*/
NSURL*streamingURL=[NSURL URLWithString:@”http://URL.m3u"];
AVPlayerItem*playerItem=[AVPlayerItem playerItemWithURL:streamingURL];
self.playerItem=playerItem;
AVPlayer*player=[AVPlayer playerWithPlayerItem:playerItem];
[播放器设置允许外部播放:否];
CMTime tm=CMTimeMake(1,1);
[player addPeriodicTimeObserverForInterval:tm
队列:dispatch_get_main_queue()使用block:^(CMTime){
/*
NSDateFormatter*DateFormatter=[[NSDateFormatter alloc]init];
[日期格式化程序setDateFormat:@“yyyy-MM-dd hh:MM:ss:mmm”];
NSLog(@“%@,[DateFormatter stringFromDate:[NSDate date]]);
*/
如果(!self.now){
self.now=[NSDate-date];
}
/*
NSDate*现在=[NSDate日期];
NSTimeInterval interval=[now TimeIntervalsIncestDate:self.now];
自我间隔+=0.01;
间隔-=(10*self.rw2);
间隔-=(60*self.rw3);
间隔+=(10*self.ff2);
self.timeLabel.text=[NSString stringWithFormat:@“%f”,间隔];
NSLog(@“INTERVAL:%@,[nsstringwithformat:@“%f”,self.INTERVAL]);
*/
如果(CMTimeGetSeconds(self.player.currentTime)>=0){
self.timeLabel.text=[NSString stringWithFormat:@“%.0f”,CMTimeGetSeconds(self.player.currentTime)];
NSTimeInterval time=CMTimeGetSeconds(self.player.currentTime);
//int min=时间/60;
整数秒=lroundf(时间)%60;
如果(秒==0){
++闵;
}
NSLog(@“秒:%d”,秒);
//使用timeLeft更新您的UI
self.timeLabel.text=[NSString stringWithFormat:@“%.2d:%.2d”,最小,秒];
}
}];
[自我设置玩家:玩家];
[玩家游戏];
[[AVAudioSession sharedInstance]设置类别:AVAudioSessionCategoryPlayback错误:nil];
[[AVAudioSession sharedInstance]设置活动:是错误:无];
如果(!self.playbausebutton){
self.playbausebutton=[[rsplaybausebutton alloc]initWithFrame:self.view.frame];
self.playbausebutton.tintColor=[UIColor colorWithRed:1.0绿色:155.0/255.0蓝色:0.0 alpha:1.0];
self.playbausebutton.animationStyle=rsplaybausebuttonanimationstylesplitandrotate;
self.playbausebutton.paused=否;
[self.playbausebutton addTarget:self action:@selector(playbausebuttonidpress:)for controlEvents:UIControlEventTouchUpInside];
}
[self.view addSubview:self.playbausebutton];
}
-(iAction)倒带:(id)发送方
{
//NSLog(@“currenttime:%f”,CMTimeGetSeconds(self.player.currenttime));
//NSLog(@“时间刻度:%d”,self.player.currentTime.timescale);
CMTime CMTime=CMTimeMake(CMTimeGetSeconds(self.player.currentTime)-1.0,1);
CMTime zero=CMTimeMake(1,2);
如果(CMTimeGetSeconds(cmTime)>CMTimeGetSeconds(零)){
[self.player.currentItem seekToTime:cmTime];
NSLog(@“!”);
}
NSLog(@“RW”);
}
-(iAction)倒带2:(id)发送方
{
/*
CMTime CMTime=CMTimeMakeWithSeconds(CMTimeGetSeconds(self.player.currentTime)-10.0,self.player.currentTime.timescale);
CMTime零点=CMTimeMake(0,10000);
如果(CMTimeGetSeconds(cmTime)>CMTimeGetSeconds(零)){
[self.player.currentItem seekToTime:cmTime];
self.rw2+=1;
NSLog(@“RW2!”);
}
*/
NSLog(@“RW2”);
CMTime CMTime=CMTimeMakeWithSeconds(CMTimeGetSeconds(self.player.currentTime)-10.0,self.player.currentTime.timescale);
CMTime zero=CMTimeMake(1,2);
NSLog(@“cmTime:%f”,CMTimeGetSeconds(cmTime));
NSLog(@“零:%f”,CMTimeGetSeconds(零));
如果(CMTim