gpt4 book ai didi

ios - AVPlayer(或 AVAudioSession?)在我离开 ViewController 后播放,当我返回时两个正在播放

转载 作者:行者123 更新时间:2023-11-29 12:21:46 26 4
gpt4 key购买 nike

我有一个 viewController 可以从网络流式传输音频。当我离开应用程序的那个页面时,音频会继续播放(这很好)。但是,当我返回时,viewdidload 方法创建了第二个音频播放器。我可以一遍又一遍地这样做,直到我有这么多的 AVplayer。这听起来令人难以置信的回声。这是我所有的相关代码。

#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

顺便说一句,有一个 UIlabel 给出了播放器的当前时间,但如果我离开页面并返回,标签会在 00:00 自动重新开始。这是因为它给出了新 AVPlayer 的当前时间。有没有办法让它给出原始 AVPlayer 的当前时间?

最佳答案

我会创建一个单例对象来控制 AVPlayer,因此无论您有多少个 View Controller ,它们都会与这个播放器 Controller 进行通信。所以基本上我建议您将 viewDidLoad 中的所有代码移动到某个 MYPlayerController 单例类。

关于ios - AVPlayer(或 AVAudioSession?)在我离开 ViewController 后播放,当我返回时两个正在播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30430528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com