视频滚动到视图中时自动播放MPMoviePlayerController

时间:2015-06-04 07:03:55

标签: ios swift video mpmovieplayercontroller

我正在制作一个带有Feed的ios应用,人们可以发布视频,视频会显示在类似于instagrams的Feed上。我想让目前看到的视频立即开始自动播放,并且一次只能播放一个视频。我目前在ios模拟器中遇到问题,其中一次播放多个视频,甚至在屏幕上看不到某些视频。

这是我的代码:

import UIKit
import Parse
import ParseUI
import MediaPlayer
import MobileCoreServices

class UsersTableViewController: PFQueryTableViewController {

    let cellIdentifier:String = "UserCell"
    var objMoviePlayerController: MPMoviePlayerController = MPMoviePlayerController()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.pullToRefreshEnabled = true
        self.paginationEnabled = false
        self.objectsPerPage = 25
        self.tableView.rowHeight = 422
        self.tableView.allowsSelection = false

        let font = UIFont(name: "Apple SD Gothic Neo", size: 24.0) ?? UIFont.systemFontOfSize(24)
        var nav = self.navigationController?.navigationBar
        nav?.barStyle = UIBarStyle.Default
        nav?.barTintColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        nav?.titleTextAttributes = [NSFontAttributeName : font, NSForegroundColorAttributeName : UIColor(red: 1.0, green: (203.0/255.0), blue: (38.0/255.0), alpha: 1.0)]
    }

    override func viewWillAppear(animated: Bool) {
        self.navigationItem.hidesBackButton = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func queryForTable() -> PFQuery {
        var query : PFQuery = PFUser.query()!
        var currentUser = PFUser.currentUser()!
        let currentUserGender : String = currentUser["gender"] as! String
        let acceptableGenderPrefs = ["Both", currentUserGender]
        let currentUserGenderPref : String = currentUser["genderPref"] as! String
        query.whereKey("username", notEqualTo: currentUser.username!)
        query.whereKey("genderPref", containedIn: acceptableGenderPrefs)

        if (currentUserGenderPref != "Both") {
            query.whereKey("gender", equalTo: currentUserGenderPref)
        }
        return query
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

        var cell : UsersTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as? UsersTableViewCell

        if(cell == nil) {
            cell = UsersTableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: cellIdentifier)
        }

        if let pfObject = object {
            cell?.userNameLabel?.text = pfObject["username"] as? String
            var profilePic = pfObject["profilePicture"] as? PFFile
            var video1 = pfObject["video1"] as? PFFile

            profilePic?.getDataInBackgroundWithBlock({
                (result, error) in
                if (result != nil) {
                    cell?.userProfilePicture?.image = UIImage(data: result!)
                }
            })
            let video1URL = NSURL(string: (video1?.url)!)
            objMoviePlayerController = MPMoviePlayerController(contentURL: video1URL)
            objMoviePlayerController.movieSourceType = MPMovieSourceType.Unknown
            objMoviePlayerController.view.frame = (cell?.userVideo1.bounds)!
            objMoviePlayerController.scalingMode = MPMovieScalingMode.AspectFit
            objMoviePlayerController.controlStyle = MPMovieControlStyle.None
            objMoviePlayerController.repeatMode = MPMovieRepeatMode.One
            objMoviePlayerController.shouldAutoplay = true

            cell?.userVideo1.addSubview(objMoviePlayerController.view)

            objMoviePlayerController.prepareToPlay()
            objMoviePlayerController.play()
        }
        return cell
    }

    // Mark: Actions
    @IBAction func signOutAction(sender: AnyObject) {
        PFUser.logOut()
        self.navigationController?.popToRootViewControllerAnimated(false)


    }
}

谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

可能更好的方法是

  • 不要玩,直到滚动停止
  • 验证单元格是否可见(至少3/4高度位于可见区域)
  • 即使您想在快速滚动时播放,也可以将音频默认设置为静音。在视频顶部提供一些可点击的图标以启用/禁用音频

这些仅仅是关于Instagram可能在做什么的观察。