AVPlayer - Play from Local URL - ios

Surprisingly i have not been able to find an answer to this despite many searching.
I have a file that has been cached, i can retrieve the cache directory and file name which i believe to be the url?
this is code to get the file and then play it
let cachedFilePath = getCachedFilePath.result as! String
audioFileURL = cachedFilePath
self.audioPlayerItem = AVPlayerItem(url: NSURL(string: audioFileURL!) as! URL)
This is what is returned if i print audioFileURL:
/Users/Genie/Library/Developer/CoreSimulator/Devices/A2FB00CE-B018-4FDF-9635-35FD6678DF8D/data/Containers/Data/Application/E18B5E89-B973-4277-AA5C-1378C69D80CD/Library/Caches/Parse/PFFileCache/7e4a8fc0eb37b655b859e4959f986da2_The%20Name%20of%20the%20Wind%2017-92.mp3
The player loads but it never plays, It just sits there spinning. so i am wondering if i am passing the url to the player correctly?
i read on one thread to use file:// before it but that didn't work.

In the place where you're creating an NSURL, you have the replace the argument name string to fileURLWithPath
Change
NSURL(string: audioFileURL!)
to
NSURL(fileURLWithPath: audioFileURL!)
Also, why are you casting NSURL to URL ? Why not use URL directly?

try like this,i hope it will work for you.
self.audioPlayerItem = AVPlayerItem(url:NSURL(fileURLWithPath: audioFileURL!) as! URL)

AVPlayerItem(url:URL(fileURLWithPath: Bundle.main.path(forResource: "filename", ofType: "mp4")!))
And make sure your video is added to the bundle:
project root > your Target > Build Phases > Copy Bundle Resources.

Related

In my own SDK, how do I play a mp3 of this SDK?

I use swift to write the SDK myself. There is an mp3 file in the SDK, and I want to play the mp3 in the SDK. I realized that it is no longer possible to use:
Bundle.main.url(forResource: "sound_tap", withExtension: "mp3")
On the following sites I found it seems to work:
Bundle.module.url(forResource: "sound_tap", withExtension: "mp3")
apple developer
But:
"Bundle.module" cannot be used because "Type 'Bundle' has no member
'module'".
So how do I play the mp3 of this SDK in my own SDK?
Thanks for any help.
Swift 5, iOS11
You need to create a Bundle target for files like video, images or xib, then link your mp3 file to this bundle target.
How to create a bundle target
Use following code to access the file:
let bundlePath = Bundle.main.path(forResource: "YOUR-BUNDLE-NAME", ofType: "bundle")
let bundle = Bundle(path: bundlePath)
let filePath = bundle.path(forResource: "YOUR-FILE-NAME", ofType:"mp3")
Play video file:
let player = AVPlayer(url: URL(fileURLWithPath: filePath))
....
player.play()

How to get metadata from mp3 file URL coming from backend to show ArtWork Image?

I am facing a problem while showing artwork image from .mp3 file which is coming from back-end server. I am getting empty metadata from that URL.I am doing this way.
let urlString = "myMp3FileURL"
let serverURL = NSURL(string: urlString)!
if let asset = AVAsset.assetWithURL(serverURL) as? AVAsset {
let metaData = asset.metaData() //Getting empty list
}
Thanks in advance.
Investigate AVAsynchronousKeyValueLoading to be notified when the requested metaData property becomes available while the asset is loaded from the server. Especially with mp3s I won't trust AVAsset to block if some requested property is not yet available.
https://developer.apple.com/documentation/avfoundation/avasynchronouskeyvalueloading

How to load an html file into a webView for multiple targets

Inside my app I have a html credits file which I want to load into a webView. The code I'm using works perfectly fine for my main target. However later on I created a second target: the pro-version of the app but for this one the code doesn't work because url is nil.
Both targets are selected on Target Membership for credits.html
I have tried changing the Location of the file in the identity inspector to all possibilities with no success.
I guess the file is somehow saved with a path only the main target can access.
if let urlPath = Bundle.main.path(forResource: "credits", ofType: "html") {
if let url = URL(string: urlPath) {
webView.loadRequest(URLRequest(url: url))
}
}
You need to use Bundle(identifier:) to get a bundle for a specific target. You can learn the Bundle identifier for each specific target by going to the Build settings of your target and looking at the Product Bundle Identifier variable.
if let targetBundle = Bundle(identifier: "yourBundleId"), let filePath = targetBundle.url(forResource: "credits", withExtension: "html") {
webView.loadRequest(URLRequest(url: url))
}

Get local URL of downloaded data instead of providing server url

I would like to play a video file in my ViewController which is loaded in every page of my PageViewController. As you will be able to see I use a plugin called Carlos to cache the videos (which initially need to be downloaded from a server) so that they do not have to be downloaded every time the user hits a new page. However, I can't figure a way out how to play this downloaded file (NSData). Thus, I would really like to know how I can get the URL of the downloaded file so that I can play it using AVPlayer.
Code (still using URL from server)
let omniCache = videoCache.cache
let request = omniCache.get(URL(string: video!)!)
request
.onSuccess { videoFile in
print("The file..." )
print(videoFile)
//How can I get the local URL here instead of my server url
if let videoURL = URL(string: self.video!){
if self.player == nil {
let playerItemToBePlayed = AVPlayerItem(url: videoURL as URL)
self.player = AVPlayer(playerItem: playerItemToBePlayed)
let playerLayer = AVPlayerLayer(player: self.player)
playerLayer.frame = self.view.frame
self.controlsContainerView.layer.insertSublayer(playerLayer, at: 0)
}
}
}
.onFailure { error in
print("An error occurred :( \(error)")
}
Look at this code of yours:
videoFile in
print("The file..." )
print(videoFile)
if let videoURL = URL(string: self.video!){
So in the first line you print videoFile, which turns out to be the data of the file. But then you ignore it! You never mention videoFile again. Why do you ignore it? That is the data, you already have the data. Now play it!
If the data is a file, get its file URL and play it. If it is in memory — it definitely should not be, because a video held entirely in memory would crash your program — save it, and get that file URL and play it.
[I have to ask, however, why you are interposing this cache plug-in between yourself and such a simple task. Why don't you just download the remote video to disk, yourself?]

How to extract an web page archive and display that on UIWebView?

I'm downloading the archive from ardaka.com/koa.zip. After downloading I need to extract it but I cannot. Which module that need to use? I tried to use Simple Unzipper and SSZipArchive, but it didn't helped. My second question is how to display the html content that I extracted? You can see the code for webView that I used but I cannot access the html file which is located(I think I can extract it in documents) in Documents folder. Maybe I can use other archive formats if there is useful extractor.
Web View Code:
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
let localfilePath = NSBundle.mainBundle().URLForResource(documentsPath.index, withExtension: "html", subdirectory: "koa") //It's not working
let myRequest = NSURLRequest(URL: localfilePath!);
webView.loadRequest(myRequest);

Resources