gpt4 book ai didi

iphone - 在后台播放视频

转载 作者:可可西里 更新时间:2023-11-01 06:10:50 25 4
gpt4 key购买 nike

我看到一个注释说这个地址 http://developer.apple.com/library/ios/#qa/qa1668/_index.html

Note: If you disable the video tracks in the movie, or detach the AVPlayerLayer from its associated AVPlayer it will allow the movie audio to continue playing in the background. However, these changes must be in effect before the application is actually switched to the background.

   [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];



AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:AnUrl]];
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];


AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayerLayer.frame = self.view.layer.bounds;
UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds];
[newView.layer addSublayer:avPlayerLayer];
[self.view addSubview:newView];

[avPlayer play];

这个程序有效。但在后台,声音被切断了。我想继续播放仅背景音。我如何从 AVPlayer 分离 AVPlayerLayer?

最佳答案

您上面的代码没问题,但是在向/从后台过渡期间执行这些操作:

  • 当应用程序激活时:(您的 avPlayerLayer 必须是您添加到 View 中的 AVPlayerLayer)

    - (void)applicationDidBecomeActive:(UIApplication *)application {
    [avPlayerLayer playerLayerWithPlayer:avPlayer];

    }
  • 当应用程序进入后台时:(您的 avPlayerLayer 必须是您添加到 View 中的 AVPlayerLayer)

    - (void)applicationWillResignActive:(UIApplication *)application {
    [avPlayerLayer playerLayerWithPlayer:nil];
    }

此外,不要忘记在 plist 的 UIBackgroundModes 中添加音频。

干杯。

关于iphone - 在后台播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14502573/

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