How to use AVFoundation to show subtitle while showing a video? - ios

I'm building an app where I show a video, I need to show a subtitle inside the video. I couldn't find a way to do it. Is this doable using AVFoundation ? Or should I hock-up something around it.
player = AVAudioPlayer(contentsOfURL: fileURL, error: nil)
player.numberOfLoops = -1 // play indefinitely
player.prepareToPlay()
player.play()

The only way is create a separate subtitles layer (CATextLayer) and add as a sublayer to the player layer. You can set up a periodic time observer to trigger every second to update the subtitles.
You can find a sample project here SubRipForCocoa. Please note that, you have to write the same concept in Swift.

Related

AVPlayer on Swift app shows a black screen but plays audio when playing video

New to Swift! I am trying to have the app play a video from my Resources upon pressing a button. I have this method in my ViewController:
if let path = Bundle.main.path(forResource: "Cypress", ofType: "mov") {
let player = AVPlayer(url: URL(fileURLWithPath: path))
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
// Modally present the player and call the player's play() method when complete.
self.present(controller, animated: true) {
player.play()
}
}
However, when the video begins to play, it is a black screen but the audio is playing properly. Are there any quick fixes to this? I am using Xcode version 12.2. Any advice would be greatly appreciated thanks :)
The problem is with the video file itself / the encoding.
Deceptively, the Quicktime ".mov" format is not a video codec, but rather audio/video container format that can contain video and audio (and some other things) in any number of compression codecs (h.264, mpeg2, ProRes, MJPEG, AAC, mp3, etc.) … Your files don't work because they include video compressed with a codec which iOS does not support.
From #alexkent in this SO answer on another post.
I don't think a .mov is necessarily not going to work for you, but you'd have to make sure it was encoded in a way that is iOS-compatible. So as you mentioned in the comments, using .mp4 ensures the video is encoded in an iOS-compatible fashion.

Audio Start Delay For The First Time - ios Swift

I am creating an application with five buttons. When I click on each button each audio will play. It is working. Now my problem is when I click first button audio play with 1 second delay(App stuck for 1 second) and play. Next time clicks a button audio play without any delay. What could be the issue here?
I am using the following code to play an audio
var currentAudio = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sample_audio", ofType: "mp3")!));
currentAudio!.stop()
currentAudio!.currentTime = 0
currentAudio!.play();
Please someone help me to finds this issue.
You could use AVAudioPlayer's .prepareToPlay() method to preload the player's buffers, it will increase AVAudioPlayer's performance (faster start).
The idea is to prepare the player some time before actually playing it:
currentAudio?.prepareToPlay()
then later, in your play function, it will start immediately:
currentAudio?.play()

How to play a video in uiview Xcode obj C

I Need to play a video when the view load but, other code i have try to do the video player comes up. I want to play the video in the UIVIEW. Can someone please help!
You will need to use an AVPlayer, specifying an AVPlayerItem. This should allow you to use multiple AV items within a single view.
Basically, everything is explained here:
https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/02_Playback.html

AVFoundation and video

I am trying to use AVFoundation, AVPlayer and AVPlayerViewController to show a video. Does anyone know of any good tutorials out there that does NOT use MPMoviePlayer?
Please let m eknow!
thank you!
AVPlayerViewController
AVPlayerViewController is the AVKit ViewController that hides away most of the complexity involved with dealing directly with AVFoundation. Its very simple and intuitive, if you check the AVPlayerViewController Class Reference you will notice that it does not hold that many properties, if you want to initialise one and attribute a video to it, you have three options ( that I can recall ).
Initialise it in code and set the frame of it;
Subclass it and call it when you wanna use it;
Use Storyboards / nibs to allocate a AVKit Player View Controller.
Either way its up to you and this is a common pattern among iOS developers, so it shouldn't strike as a surprise to you unless you are a complete beginner. Please note that for any of these ways of creating an AVPlayerViewController, you have to initialise the AVPlayer object with an AVPlayerItem or an URL carrying the video you want to display.
Subclassing AVPlayerViewController-Swift
Obs. This is the swift example, but the Objective-C one follows the same pattern, the only change is the syntax of each language.
class AVKitPlayerViewController: AVPlayerViewController {
override func viewDidLoad() {
super.viewDidLoad()
// get the video URL
let videoURL = NSBundle.mainBundle().URLForResource("Test", withExtension: ".mov")
// Initialise with URL
player = AVPlayer(URL: videoURL!)
// Initialise with AVPlayerItem
let playerItem = AVPlayerItem(URL: videoURL!)
player = AVPlayer(playerItem: playerItem)
// If you want custom controls showsPlaybackControls should be false
// Then you must create your own buttons
//showsPlaybackControls = false
}
}
AVFoundation
AVFoundation is a very large and interesting framework, you can do a lot of advanced and interesting stuff with it. I would encourage you to go read the docs and checkout these WWDC-2015 sample code:
AVFoundationPiPPlayer: Picture-in-Picture Playback with AVKit
AVFoundationQueuePlayer-iOS: Using a Mixture of Local File Based Assets and HTTP Live Streaming Assets with AVFoundation
AVFoundationSimplePlayer-iOS: Using AVFoundation to Play Media
AVFoundation Programming Guide
AVFoundation Programming Guide
I hope this helps you. Happy Coding. :smile:

Play Song at specific index of MPMediaItemCollection in Swift

I try to make my own Musicplayer with Swift. I need to jump to a specific song/index of my MPMediaItemCollection and start to play it but i can only find methods like skipToNextItem() and skipToPreviousItem(). Is there any other way to do that than with a loop?
let player = MPMusicPlayerController.systemMusicPlayer()
player.setQueueWithItemCollection(mediaCollection)
player.play()
According to the documentation, we use the nowPlayingItem property.
To specify that playback should begin at a particular media item in the playback queue, set this property to that item while the music player is stopped or paused.
So, it sounds like you should stop or pause the player, set the nowPlayingItem, and then call play again.
player.nowPlayingItem = mediaCollection.items[selectedIndex]
player.play()

Resources