You would like to build an iPhone app that has many videos but you don't want to have 1 video per language. This will take up a lot of disk space! One solution is to have 1 video and n audios (1 per language). This allows you to compose the PlayerView of the video and the language audio the user selected.
Before we get into the AVFoundation framework, there are few steps you need to take:
1. The Videos (without the audio) need to be created.
2. Create the Audio files - 1 per language
3. Localize Audio Files
Add Audio file to Xcode
Select Audio file and localize
Select Utilities View on top right
Add languages you require
Copy the language audio to the appropriate directory e.g. es.lproj directory will be for the Spanish Audios
Say Hello to AVFoundation
PlayerViewController (UIViewController) will contain the PlayerView (UIView). The PlayerView will contain the AVPlayer.
The classes we will be discussing are the AVPlayer, AVPlayerItem, AVMutableCompositionTrack, and AVAssetTrack. These classes will help us put together a Video and Audio.
1. Load the AVURLAsset
3. Load Audio Asset for the app language. See code below.
To summarize the main "container" is the _languageVideoComposition (AVMutableComposition). We use the _languageVideoComposition to create an AVMutableCompositoinTrack (one for AVMediaTypeVideo and one for AVMediaTypeAudio). Next, the AVAssetTrack for the video and for the audio is created. Each AVAssetTrack is set on the corresponding AVMutableCompositionTrack.
The AVPlayerItem is created with the _languageVideoComposition. The AVPlayer is created using the AVPlayerItem. Finally the playerView is set with the AVPlayer.
The sample code can be found here: iOS AVPlayer - Localize your videos and audios sample1 Comment