Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emulating YouTube Mini Player in iOS

Tags:

youtube

ios

swift

In the YouTube app, I'm able to continue watching videos via a mini-player in the very bottom right-hand corner (see video below entitled "The Chainsmokers") that's layered atop all other view controllers as I navigate the app. No matter which page I'm on, I can watch the video in the mini player.

If you drag up from the mini player, the video's entire view controller will appear. But it's always atop other pages. I'm trying to understand how to copy this method using Swift so I can watch the video from any page.

The way my app is structured is through a UIPageViewController. I'm embedding three unique navigation controllers (for swipe nav purposes (see code below)). I want to understand how to utilize the mini-player. In YouTube, is that like a mini modal VC? How is it structurally laid out, and if you know, how can I incorporate that into my current layout?

class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    private var pages: [UINavigationController]!

    private var currentPageIndex: Int!

    override func viewDidLoad() {
        super.viewDidLoad()
            self.dataSource = self
            self.delegate = self

            self.pages = [
                self.storyboard!.instantiateViewControllerWithIdentifier("Nav1") as! UINavigationController,
                self.storyboard!.instantiateViewControllerWithIdentifier("Nav2") as! UINavigationController,
                self.storyboard!.instantiateViewControllerWithIdentifier("Nav3") as! UINavigationController
            ]

            (self.pages[0].topViewController as! Nav1).parentPageViewController = self
            (self.pages[1].topViewController as! Nav2).parentPageViewController = self
            (self.pages[2].topViewController as! Nav3).parentPageViewController = self


            self.currentPageIndex = 1
            let startingViewController = self.pages[1] as UINavigationController
            self.setViewControllers([startingViewController], direction: .Forward, animated: false, completion: nil)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        let index = (self.pages as NSArray).indexOfObject(viewController)
        self.currentPageIndex = index

        // if currently displaying last view controller, return nil to indicate that there is no next view controller
        return (self.currentPageIndex == self.pages.count - 1 ? nil : self.pages[self.currentPageIndex + 1])
    }
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        let index = (self.pages as NSArray).indexOfObject(viewController)
        self.currentPageIndex = index

        // if currently displaying first view controller, return nil to indicate that there is no previous view controller
        return (index == 0 ? nil : self.pages[index - 1])
    }
func displayPageForIndex(index: Int, animated: Bool = true) {
        assert(index >= 0 && index < self.pages.count, "Error: Attempting to display a page for an out of bounds index")

        // nop if index == self.currentPageIndex
        if self.currentPageIndex == index { return }

        if index < self.currentPageIndex {
            self.setViewControllers([self.pages[index]], direction: .Reverse, animated: true, completion: nil)
        } else if index > self.currentPageIndex {
            self.setViewControllers([self.pages[index]], direction: .Forward, animated: true, completion: nil)
        }

        self.currentPageIndex = index
    }

}

enter image description here

like image 367
slider Avatar asked Jul 23 '15 03:07

slider


People also ask

Can you mini play YouTube on Iphone?

To use picture-in-picture in the YouTube app, just select a video to watch and then exit out of YouTube by swiping upwards from the bottom of the screen. The video will transfer to a mini player that can be moved around the display.

How do I get the YouTube mini player on top of my Iphone?

To use picture-in-picture (PiP), exit the YouTube app while a video is playing. If you have the PiP setting turned on, the video will shrink into a PiP window. The PiP window can be dragged to different parts of the screen, allowing playback to continue on top of other apps.


1 Answers

It is quite easy to do. You will subclass UIWindow, add a mini-player view and make sure it is alway at the top of view hierarchy. First in your AppDelegate add this code to overwrite your window:

/*!
 *  Subclassing UIWindow
 */
-(UIWindow *)window
{
    static YourWindow *customWindow = nil;
    if (!customWindow) customWindow = [[YourWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    return customWindow;
}

And now in YourWindow class, add the miniPlayer:

-(void)awakeFromNib
{
    [self addMiniPlayer];
}

-(void)addMiniPlayer
{
    UIView *miniPlayer = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)]; //change to your frame here
    [self addSubview:miniPlayer];
    miniPlayer.backgroundColor = [UIColor redColor];
    self.miniPlayer = miniPlayer;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    [self bringSubviewToFront:self.miniPlayer];

    // You would like to update your miniplayer frame here.
    //CGRect frame = self.miniPlayer.frame;
    //frame.origin.x = self.frame.size.width - frame.size.width - 5;
    //frame.origin.y = 70;
    //self.appInfoLabel.frame = frame;
} 

Now it is your job to adjust the behavior of the miniPlayer. You can access it from AppDelegate, something like this: self.window.miniPlayer

UPDATED: Here is an example written by Swift: https://github.com/sahara108/TestCustomWindow

like image 134
sahara108 Avatar answered Nov 14 '22 21:11

sahara108