jjzjj

xcode - AVPlayer 一旦移动到背景就卡住

coder 2023-09-06 原文

我有以下加载视频并循环播放的代码。但是,当按下主页并返回到应用程序时,视频会卡住并且不会再次播放。我觉得答案在 AppDelegate 函数中,但似乎无法让它们工作。

import UIKit
import AVKit
import AVFoundation



class ViewController: UIViewController, UIApplicationDelegate {



var videoPlayer: AVPlayer!
var playerLayer: AVPlayerLayer?



override func viewDidLoad() {
    super.viewDidLoad()
    let path = NSBundle.mainBundle().pathForResource("video", ofType:"mp4")
    let url = NSURL(fileURLWithPath: path!)
    let playerItem = AVPlayerItem(URL: url)

    self.videoPlayer = AVPlayer(playerItem: playerItem)
    self.playerLayer = AVPlayerLayer(player: self.videoPlayer)
    self.playerLayer!.frame = self.view.frame
    self.videoPlayer!.play()

    self.view.layer.addSublayer(self.playerLayer!)


    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("playerDidReachEnd:"), name: AVPlayerItemDidPlayToEndTimeNotification, object:nil)
}


func playerDidReachEnd(notification: NSNotification) {
    self.videoPlayer.seekToTime(kCMTimeZero)
    self.videoPlayer.play()
}


func applicationWillResignActive(application: UIApplication) {
    videoPlayer.pause()
}


func applicationDidBecomeActive(application: UIApplication) {
    videoPlayer.play()
}


func applicationDidEnterBackground(application: UIApplication) {
    videoPlayer.pause()
}


func applicationWillEnterForeground(application: UIApplication) {
    videoPlayer.play()
}





override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }



}

这是我用来修复它的方法。

import UIKit
import AVKit
import AVFoundation



class ViewController: UIViewController, UIApplicationDelegate {



var videoPlayer: AVPlayer!
var playerLayer: AVPlayerLayer?



override func viewDidLoad() {
    super.viewDidLoad()
    let path = NSBundle.mainBundle().pathForResource("video", ofType:"mp4")
    let url = NSURL(fileURLWithPath: path!)
    let playerItem = AVPlayerItem(URL: url)

    self.videoPlayer = AVPlayer(playerItem: playerItem)
    self.playerLayer = AVPlayerLayer(player: self.videoPlayer)
    self.playerLayer!.frame = self.view.frame
    self.videoPlayer!.play()

    self.view.layer.addSublayer(self.playerLayer!)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("continueVideo:"), name: "continueVideo", object: nil)


    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("playerDidReachEnd:"), name: AVPlayerItemDidPlayToEndTimeNotification, object:nil)
}


func playerDidReachEnd(notification: NSNotification) {
    self.videoPlayer.seekToTime(kCMTimeZero)
    self.videoPlayer.play()
}

func continueVideo(notification: NSNotification) {
    self.videoPlayer.play()
}

func applicationWillResignActive(application: UIApplication) {
    videoPlayer.pause()
}


func applicationDidBecomeActive(application: UIApplication) {
    videoPlayer.play()
}


func applicationDidEnterBackground(application: UIApplication) {
    videoPlayer.pause()
}


func applicationWillEnterForeground(application: UIApplication) {
    videoPlayer.play()
}





override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }



}

和应用委托(delegate)部分

import UIKit


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    return true
}

func applicationWillResignActive(application: UIApplication) {


}

func applicationDidEnterBackground(application: UIApplication) {


}

func applicationWillEnterForeground(application: UIApplication) {
    NSNotificationCenter.defaultCenter().postNotificationName ("continueVideo", object:nil)
}

func applicationDidBecomeActive(application: UIApplication) {

}

func applicationWillTerminate(application: UIApplication) {

}


}

最佳答案

这是一个简单的修复,您需要创建一个 NSNotification 并从您的 AppDelegate 调用它:

func applicationWillEnterForeground(application: UIApplication) {
     NSNotificationCenter.defaultCenter().postNotificationName("continueVideo", object: nil)
}

SWIFT3 更新

func applicationWillEnterForeground(_ application: UIApplication) {
    // NOTE: Notification.Name raw value should be global 
    NotificationCenter.default.post(name: Notification.Name(rawValue: "com.yourappname.continueVideo"), object:self)
}

然后以正常方式接收该通知:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "<funcName>:", name: "continueVideo", object: nil)

SWIFT3 更新

NotificationCenter.default.addObserver(self, selector: #selector(<funcName>), name: Notification.Name(rawValue: "com.yourappname.continueVideo"), object: nil)

关于xcode - AVPlayer 一旦移动到背景就卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33667226/

有关xcode - AVPlayer 一旦移动到背景就卡住的更多相关文章

  1. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  2. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

  3. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  4. ruby-on-rails - 使用 Rmagick 或 ImageMagick 在背景上放置标题 - 2

    我有一张背景图片,我想在其中添加一个文本框。我想弄清楚如何将标题放置在其顶部的正确位置。(我使用标题是因为我需要自动换行功能)。现在,我只能让文本显示在左上角,但我需要能够手动定位它的开始位置。require'RMagick'require'Pry'includeMagicktext="Loremipsumdolorsitamet"img=ImageList.new('template001.jpg')img 最佳答案 这是使用convert的ImageMagick命令行的答案。如果你想在Rmagick中使用这个方法,你必须自己移植

  5. ruby-on-rails - 如何重命名或移动 Rails 的 README_FOR_APP - 2

    当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?

  6. ruby-on-rails - rbenv:从 RVM 移动到 rbenv 后,在 Jenkins 执行 shell 中找不到命令 - 2

    我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions

  7. ruby-on-rails - 如何使用 Xcode 4.5.1 在 OSX Lion 10.8.2 上编译 EventMachine gem - 2

    我找遍了所有我能找到的地方,但似乎找不到解决这个问题的办法。我在Lion10.8.2上使用Xcode4.5.1,并尝试为Rails项目运行bundle,但它一直卡在这上面。我正在为Heroku使用Thingem。Bolanos@Jeremys-Mac-mini⦿-1.9.3fishfarm$sudogeminstalleventmachinePassword:Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingeventmachine:ERROR:Failedtobuildgemnativeextens

  8. ruby-on-rails - 在所有页面上使用 Prawn 的背景图像 - 2

    我在View中有这段代码prawn_document(:page_size=>"A4",:top_margin=>80,:bottom_margin=>40,:background=>"public/uploads/1.png")do|pdf|creation_date=Time.now.strftime('%d-%m-%Y')posts=@posts.eachdo|post|pdf.pad(10)dopdf.textpost.titlepdf.textpost.textendendpdf.page_count.timesdo|i|pdf.go_to_page(i+1)pdf.draw

  9. ruby - 在 ruby​​ Selenium 中移动鼠标(move_to) - 2

    我正在尝试使用Ruby中的SeleniumWebDriver2.4模拟鼠标移动如果我运行测试,是否应该看到鼠标在我的屏幕上移动?我很困惑。我试过很多不同的方法示例代码:require'selenium-webdriver'driver=Selenium::WebDriver.for:firefoxdriver.navigate.to'http://www.google.com'element=driver.find_element(:id,'gbqfba')那我试过了driver.action.move_to(element).performdriver.mouse.move_to(e

  10. ruby - 256 种颜色,前景和背景 - 2

    这是两个脚本的故事,与previousquestion有关.这两个脚本位于http://gist.github.com/50692.ansi.rb脚本在所有256种背景颜色上显示所有256种颜色。ncurses.rb脚本显示所有256种前景颜色,但背景显示基本的16种颜色,然后似乎循环显示各种属性,如闪烁和反向视频。那么是什么给了?这是ncurses中的错误,它使用带符号的整数来表示颜色对吗?(即'tputcolors'表示256但'tputpairs'表示32767而不是65536)似乎如果是这种情况,颜色对的前半部分会正确显示但后半部分会重复或进入属性作为int包裹。

随机推荐