How to add a video that plays behind your controls in iOS

Having a video playing behind the controls of your app’s main menu immediately catches the eye of your users and people around them.

This feature is not difficult to implement; it’s actually very simple. Let me show you how to do it. The video at the bottom of this page shows the result. If you want to download a project that uses this technique, click here.

A canvas to draw on

Objects of type UIView are the visible components in iOS. Every UIView has a layer property of type CALayer. This is where the visual content exists.

CALayers can be used to draw shapes, particles, text, gradients, video, etc. For the specific case of video, a CALayer subclass called AVPlayerLayer simplifies the process of video playback.

To display video on a UIView object, the recipe is as follows:

1-Create an AVPlayerLayer object.

2-Assign a video player object (AVPlayer) to the AVPlayerLayer.player property.

3-Set the frame of your AVPlayerLayer object

4-Add the AVPlayerLayer as a sublayer of your view’s layer property.

5-Start playback by calling play() on the video player object.

This is the code:


A helper class

The example project at the top contains a helper UIView class to make our lives easier when implementing the video feature. It’s called PlayVideoView. In it, an object of type AVPlayerLooper is used to loop the video.

This is how to use the helper class:
1-Add a PlayVideoView object to your view controller’s main view, either programmatically or via Interface Builder.

2- In your view controller’s viewDidLoad method, call the setVideo method of the PlayVideoView object to set the video to play

3-In your view controller’s viewWillAppear method, get the player associated to the PlayVideoView object and call play() on it.

4- In your view controller’s viewWillDisappear method, get the player associated to the PlayVideoView object and call pause() to stop playback.

Here’s how to implement it:

Interruption handling

The above code pauses/resumes playback when the view is shown or hidden.

But what happens when you minimize the app and then open it again (keep in mind viewWillDisappear won’t be called when the app is minimized)?

Well, it depends. In iOS 13, video will resume playing automatically, as expected.

In iOS 12 and less, however, it won’t resume playing automatically. That’s not good.

To fix this glitch, your view controller must respond to the following notifications:

  • UIApplication.didBecomeActiveNotification
  • UIApplication.willResignActiveNotification

When you receive the didBecomeActiveNotification notification, call play() on the video player. For the willResignActiveNotification notification, call pause().

It doesn’t hurt if you implement these and your app runs in iOS 13; it won’t break anything. The example project implements interruption handling.

The fine print

Video playback is a demanding task, for this reason it’s recommended to use short videos and to limit this feature to the sections where it’s really needed.

Depending on the type of app you’re developing, you might want to play the video silently or with music. A game I worked on some time ago played a video with music in the main menu, which was appropriate for this type of app. Another app I worked, for a very different type of audience, solemnly displayed a silent video in the main menu.

 

A popular Peppa Pig song in English, French, Spanish, Italian

A Peppa Pig multilanguage song in English, French, Spanish, Italian.

One of the most popular Peppa Pig songs is Grandpa’s Little Train. This song seems to be based on the popular children song The Wheels On The Bus.

Below you can find a video of the song with subtitles and its lyrics. All this in English, French, Spanish and Italian.

English

Grandpa’s little train goes choo choo choo,

choo choo choo, choo choo choo.

Grandpa’s little train goes choo choo choo,

all day long.

And the piggies on the train go oink, oink, oink,

oink, oink, oink, oink, oink, oink.

And the piggies on the train go oink, oink, oink,

all day long.

Français

Le petit train de papi fait tchou tchou tchou,

tchou tchou tchou, tchou tchou tchou.

Le petit train de papi fait tchou tchou tchou,

tout le temps.

Les porcelets dans le train font groin groin groin,

groin groin groin, groin groin groin.

Les porcelets dans le train font groin,

tout le temps.

Español

El tren del abuelo hace chu cu chú,

chu cu chú, chu cu chú.

El tren del abuelo hace chu cu chú,

todo el día.

Los cerditos en el tren hacen oink oink oink,

oink oink oink, oink oink oink,

Los cerditos en el tren hacen oink oink oink,

todo el día.

Italiano

Il treno del nonno fa ciuf ciuf ciuf,

ciuf ciuf ciuf, ciuf ciuf ciuf.

Il treno del nonno fa ciuf ciuf ciuf,

tutto il dì

I maialini fanno oink oink oink,

oink oink oink, oink oink oink.

I maialini fanno oink oink oink,

tutto il dì.

 

Custom Controls not working in AVPlayerViewController in iOS 12

I recently stumbled across a bug in the AVPlayerViewController class. If you’re not familiar with this AVKit class, it’s a handy object used to play videos in iOS.

The bug happens when you use custom playback controls. Under some circumstances, your controls will stop responding. The bug is fixed in the upcoming iOS 13, but the app I was developing will be used in devices that currently have iOS 12 and can’t be upgraded. So finding a solution was our only option. It turned out that solution was easy to implement. Let me explain the details of the bug and how to fix it.

System-supplied and custom playback controls

By default, an AVPlayerViewController object shows system-supplied playback controls. You can combine or replace these controls with your own custom controls.

The showsPlaybackControls property of AVPlayerViewController objects controls whether you want the system-supplied playback controls.

To provide your custom controls, access the contentOverlayView property and add your views using addSubview

Custom controls not responding

For my case, I needed the following:

1-Start playback with custom playback controls.

2-At some point, hide the custom controls and show the system-supplied playback controls.

3-After some time, go back to the custom playback controls.

The problem was that, after hiding the system-supplied playback controls (point 3, above), the custom controls stopped working. See the video below for more details.

The view hierarchy explains it all

If you examine the view hierarchy you will find that the system-supplied playback controls are contained in a view of type AVPlaybackControlsView (this class is private and was introduced in iOS 11).

In iOS 12, starting playback with showsPlaybackControls = false will result in an object with no AVPlaybackControlsView view, as shown in Image 1 below.

Image 1 – Starting playback with showsPlaybackControls = false will result in an object with no AVPlaybackControlsView view

If you later set showsPlaybackControls = true, a view of type AVPlaybackControlsView will be created. However, if later on you deactivate the system-supplied playback controls using showsPlaybackControls = false, its view will stay at the top of the hierarchy, thus preventing input from reaching your custom controls. See Image 2 below.

Image 2 – Even if you deactivate the system-supplied playback controls, its view (of type AVPlaybackControlsView) will stay at the top of the hierarchy, thus preventing input from reaching your custom controls.

The fix

Since an AVPlaybackControlsView view is blocking the input, we can either remove it or hide/disable it.

I have found that, if removed, it won’t be added again when setting showsPlaybackControls = true. So the best fix simply consists in hiding/disabling it. Of course, if later on you want to show the system controls again, you will need to unhide/enable it.

The video below shows the result after the above fix is applied.

A demo project

You can download a project which implements the fix.

 

Using DispatchSemaphore to make synchronous calls in Swift

Modern-day CPUs are capable of running multiple execution threads simultaneously. This capability is called multi-threading and allows executing multiple tasks at the same time.

In programming, this capability is closely related to what is called synchronous (sync) calls and asynchronous (async) calls. A synchronous call will block the execution of the thread until the call is completed.

An asynchronous call, on the other hand, will not block the current thread because it will be executed in a different thread.

Nowadays, many API calls are asynchronous. When the call completes, the result is passed to the calling thread. That’s the current state of things, and we’re happy and grateful for that.

Nevertheless there are some cases where you might want to wait, that is, block execution, until a number of async calls complete. To accomplish this, you can use a semaphore. The following playground code shows how:

import UIKit

func synchronousTask(taskID : Int) {
    let semaphore = DispatchSemaphore(value: 0) // see note below
    print("Waiting for task \(taskID) to complete")

    // dispatch work to a different thread
    DispatchQueue.global(qos: .background).async {
        print("Starting task \(taskID)")
        sleep(3)    // simulate the duration of the task
        print("Ending task \(taskID)")
        semaphore.signal()
    }
    semaphore.wait()        // block until the task completes
    print("Task \(taskID) is completed")
}

// semaphores shouldn't be used on the main thread
DispatchQueue.global(qos: .background).async {
    synchronousTask(taskID: 1)
}

Value used for semaphore initialization.

The value used in the DispatchSemaphore initialization is related to its purpose. According to Apple: “Passing zero for the value is useful for when two threads need to reconcile the completion of a particular event.”

Since this is what we want, we initialize the semaphore with a value of zero.

The above code produces the following output:

Waiting for more than one task to complete.

If you need to wait for the completion of more than 1 task, simply write a wrapper function to call the synchronous code, as shown in the following code:

import UIKit

func synchronousTask(taskID : Int) {
    let semaphore = DispatchSemaphore(value: 0)
    print("Waiting for task \(taskID) to complete")

    // dispatch work to a different thread
    DispatchQueue.global(qos: .background).async {
        print("Starting task \(taskID)")
        sleep(1)    // simulate the duration of the task
        print("Ending task \(taskID)")
        semaphore.signal()
    }

    semaphore.wait()        // block until the task completes
    print("Task \(taskID) is completed")
}

func synchronousCall() {
    print("Waiting for tasks to be executed")
    for i in 1...3 {
        synchronousTask(taskID: i)
    }
    print("All tasks were executed")
}

// semaphores shouldn't be used on the main thread
DispatchQueue.global(qos: .background).async {
    synchronousCall()
}

The above code produces the following output:

Cómo crear contenido con tu celular (memes, videos, etc.)

La creación de contenido es la sangre nueva que ayuda a mantener la vitalidad del internet.

Si tienes inclinación hacia el periodismo basado en fotos y videos, o si simplemente quieres hacer reir a la gente con memes, aquí te presento varias apps, todas gratuitas, que te ayudarán a crear contenidos usando tu celular o tableta.



Creación de memes

Si llegaste a esta página buscando una app para hacer memes, no esperes más y descarga Meme Generator (iOS, Android). Hay miles de apps para memes, pero esta es, a mi juicio, una de las fáciles de usar. Al final de este documento encontrarás ejemplos de memes creados con esta app.

Combinar varias fotos

Hay ocasiones en que es más conveniente una foto combinada que publicar varias fotos por separado.

Pic Stitch (iOS, Android) te ayuda a combinar fotos con diferentes diseños. También permite aplicar efectos, calcomanías y agregar texto.

Agregar textos llamativos a tus fotos

¿Aburrido con la forma en que Messenger te permite poner texto en tus fotos? Over (iOS) ofrece una buena cantidad de textos predefinidos, de calidad profesional. También puedes escribir tu propio texto. Esta app es usada por revistas para dar un toque especial a sus imágenes.

Rotación de videos, iOS

Rotate & Flip (iOS) es una app que te permite, fácilmente, rotar tus videos. Increíblemente fácil de usar.

Rotación de videos, Android

Para Android, Google Photos te permite rotar tus videos. Probablemente esta app ya viene instalada en tu dispositivo, pero si no fuera así, puedes instalarla a partir de este enlace: Google Photos

Agregar texto a tus videos

Vont (iOS) es tan fácil de usar que te tomará sólo un par de minutos entenderlo. Y la cantidad de tipos de letra que incluye es impresionante.

Atención periodistas: esta es la app perfecta para agregar una marca de agua a sus videos y evitar que otros se adjudiquen, indebidamente, su autoría.

Recortar y combinar videos

[A partir de septiembre de 2019 esta app ya no está disponible. En su lugar puedes usar Adobe Spark, que también es gratis] Adobe Premiere Clip (iOS, Android) te permitirá recortar tus videos y combinar varios videos en uno solo.
Para usarla, necesitas crear una cuenta de Adobe, la cual es gratuita. Vale la pena.

 



Ejemplos de contenidos creados con estas apps

Importante: Los siguientes archivos fueron manualmente reducidos en calidad para disminuir el ancho de banda usado. 

Ejemplo de meme creado con Meme Generator, usando una de mis fotos.
Meme Generator incluye gran cantidad de graciosas fotos que puedes utilizar para crear tus memes
Over agrega texto a tus fotos de manera atractiva. Es usado por revistas y otros medios impresos y digitales.

El siguiente video muestra las capacidades de Vont para agregar texto.

A continuación un video creado combinando varios clips con Adobe Premiere Clip

Recursos gratuitos para aprender inglés

[El siguiente texto fue publicado inicialmente en abril del 2006. Su contenido sigue siendo útil. Se eliminaron dos enlaces que ya no funcionaban y se hicieron pequeños ajustes al texto original. Las imágenes son las publicadas originalmente]

El internet es un valioso recurso para aquellos que están aprendiendo otro idioma. Leer publicaciones y tener acceso a transmisiones de radio o televisión es posible -y sencillo- mediante una conexión a la red de redes.
A continuación compartiré contigo enlaces muy valiosos y útiles. Todos ellos son recursos gratuitos que te ayudarán en tu aprendizaje del idioma de Shakespeare, el más enseñado en el mundo.

Para visitar cualquiera de las páginas que menciono a continuación, simplemente haz clic en el url o sobre la imagen. Todas se abrirán en una ventana nueva.

Dirección: http://dictionary.cambridge.org

Categoría: Diccionarios

En la página de Cambridge Dictionaries encontrarás varios diccionarios. Para los principiantes, existe el diccionario Español/Inglés. Si buscas un diccionario con definiciones en inglés también lo encontrarás allí. Están disponibles 2 versiones: el sencillo (Learner’s) y el avanzado (Advanced Learner’s); éste último comprende términos más especializados. Me agrada particularmente que las definiciones vienen acompañadas de ejemplos ilustrando el uso de término consultado..
Pero el inglés es mucho más que simples palabras. En esta página también se pueden consultar Idioms y Phrasal Verbs, algo valiosísimo para los estudiantes aventajados. Recordemos que los Idioms son aquellas frases propias del lenguaje que no se pueden traducir literalmente. Por ejemplo, un Idiom en español es “creerse la mamá de los pollitos”, cuyo significado es creerse alguien muy importante. Por otra parte, los Phrasal Verbs se componen de un verbo del inglés seguido de una partícula que le da un significado diferente. Ejemplos de Phrasal Verbs son take off (despegar), go on (continuar), look up (buscar), etc.

Dirección: http://www.cnn.com

Categoría: Noticias

La cadena estadounidense CNN tiene una de las mejores páginas de noticias en inglés. El acceso es completamente gratuito. Comprende una sección internacional y una de Estados Unidos. En esta última encontrarás las noticias divididas en secciones como Clima, Negocios, Deportes, etc.
Hace pocos meses se incorporó la sección de video, en donde se incluyen notas muy interesantes, algunas veces tomadas de otras cadenas de televisión.

Dirección: http://www.wamu.org

Categoría: Radio en vivo

WAMU, American University Radio, es una estación de tipo Public Radio. Las estaciones de este tipo funcionan gracias de las contribuciones económicas de sus radioescuchas.
WAMU se localiza en Washington D. C. No hay música, excepto los sábados en un programa de jazz. Algunos programas abarcan temas propios de los Estados Unidos, aunque hay otros que se dedican a noticias internacionales.
Los domingos por la noche Ed Walker* conduce el programa titulado The Big Broadcast, en donde transmite episodios de los más populares programas de la época de oro de la radio. Particularmente recuerdo Tales of the Texas Rangers.
WAMU da mucha importancia a sus transmisiones por internet, las cuales puedes escuchar en vivo. Si deseas escuchar algún programa que ya se transmitió, la mayoría de ellos están disponibles durante una semana después de salir al aire. La calidad del audio es excelente.
*Ed Walker falleció el pasado 26 de octubre de 2015. The Big Broadcast es ahora conducido por Murray Horwitz