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

标签 ios objective-c audio avplayer avaudiosession

我有一个 viewController,可以从网络传输音频。当我离开应用程序的该页面时,音频会继续播放(这很好)。但是,当我返回时,viewdidload 方法创建了第二个音频播放器。我可以一遍又一遍地这样做,直到我有这么多的 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

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

最佳答案

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

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

相关文章:

audio - 切比雪夫LPF引入噪音

java - 在后台播放声音文件 (.wav) 并让 block 振动

ios - Parse.com 响应推送通知

ios - SwiftUI - 数据源为空时动态列表过滤动画飞到右侧

ios - UIButton 没有被实例化

iphone - 从 .txt 文件中读取 - Objective C

html - HTML中的音频标签找不到音频文件

iOS 10 Collection View :prefetchItemsAt not called

iphone - UILabel 和 UIView 在 vi​​ewDidAppear 上返回大小 (0, 0)

iphone - BlocksKit 1.8.1/Xcode 4.5/iOS 的编译器警告