Tuesday, May 22, 2018

player

//
//  PlayerViewController.swift
//  Sample
//
//  Created by Neeraj on 5/19/18.
//  Copyright © 2018 Neeraj. All rights reserved.
//

import UIKit
import AVFoundation

class PlayerViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var playerTableView: UITableView!
 
    var playerArray = [String]()
    var player: AVPlayer!
 
    override func viewDidLoad() {
        super.viewDidLoad()
     
        playerArray = ["https://www.rmp-streaming.com/media/bbb-360p.mp4", "https://www.rmp-streaming.com/media/bbb-720p.mp4", "https://www.rmp-streaming.com/media/ed-360p.mp4", "https://www.rmp-streaming.com/media/bbb-360p.mp4" ,"https://www.rmp-streaming.com/media/ed-360p.mp4", "http://gamezs.esy.es/kush/VID-20180518-WA0002.mp4"]
     
    }
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return playerArray.count
    }
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! PlayerTableViewCell
     
        let urlString = playerArray[indexPath.row]
     
     
        let videoURL = URL(string: urlString)
        player = AVPlayer(url: videoURL!)
     
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.videoGravity = .resize
     
        playerLayer.frame = cell.playerView.layer.bounds
        cell.playerView.layer.addSublayer(playerLayer)
     
        return cell
    }
 
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cells = tableView.cellForRow(at: indexPath) {
            if cells.tag == 1 {
                player.play()
                cells.tag = 2
            } else {
                player.pause()
                cells.tag = 1
            }
        }
        tableView.deselectRow(at: indexPath, animated: false)
    }
 
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let assets = AVAsset(url: URL(string: playerArray[indexPath.row])!)
     
        let originalWidth = assets.screenSizes?.width
        let originalHeight = assets.screenSizes?.height
        let deviceWidth = tableView.frame.size.width
        let deviceHeight = (originalHeight!/originalWidth!) * deviceWidth
     
        return deviceHeight
    }

}

class PlayerTableViewCell: UITableViewCell{
 
    @IBOutlet weak var playerView: UIView!
 
}

extension AVAsset {
    var screenSizes: CGSize? {
        if let track = tracks(withMediaType: .video).first {
            let size = __CGSizeApplyAffineTransform(track.naturalSize, track.preferredTransform)
            return CGSize(width: fabs(size.width), height: fabs(size.height))
        }
        return nil
    }
}






func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenParts = deviceToken.map { data -> String in
            return String(format: "%02.2hhx", data)
        }
       
        let token = tokenParts.joined()
        let alertView = UIAlertController(title: "Alert", message: "\(token)", preferredStyle: .alert)
        alertView.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
        self.window?.rootViewController?.present(alertView, animated: false, completion: nil)
            print("Device Token: \(token)")
    }


No comments:

Post a Comment