AVPlayer unable to play video from NSDocuments Directory on iOS8 - ios

I'm trying to get my app to play a video that has been downloaded to the Documents Directory.
My code works when tested on the iOS 7.1 simulator but not on iOS 8.0 devices or the iOS 8.0 simulator. There isn't any error, AVPlayer does not play the video and shows a Black View.
I also checked the Document's folder of my device with XCode's Organizer and the file appears to be successfully downloaded.
Here is a simplified version of my code.
// file_path is a string that stores the path to the video file a println of it shows
// "/var/mobile/Containers/Data/Application/83C7837C-59B5-4767-A579-7CE758A93C6F/Documents/og4gr.mp4"
var path:NSURL = NSURL.fileURLWithPath(file_path, isDirectory: false);
_player = AVPlayer(URL: path);
Has anyone else encountered this? Thank you for your time!

So the problem was that the Application Documents Folder Path changes every time you launch the app and I was storing the absolute path of the video file.
According to this article here:
http://pinkstone.co.uk/where-is-the-documents-directory-for-the-ios-8-simulator/, this is how it is now in iOS 8, which is why while my method worked in iOS7 it was failing on iOS8 devices and Simulators. Thank you #RoboticCat for pointing me in the right direction!

Related

No sound on UIWebView iOS10

I load on UIWebView a HTML game from local files.
iOS 10.2 / XCode 8 / Swift 3
override func viewDidLoad() {
super.viewDidLoad()
if let path = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "game") {
webView.loadRequest(URLRequest(url : URL(fileURLWithPath: path)))
}
}
Everything seems fine, but I cant hear any sound on game.
I open the same files (game) from localhost server via Mobile's Safari browser and sound seems OK. So i'm sure that html/javascript files & connection is OK and issue has to do with iOS.
Added "Required background modes" -- > "App plays audio or streams audio/video" in info.plist but nothing changed. No sound on device.
You should run this line before webview.loadRequest(...)
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
I answer my own question just in case someone has the same problem. I found a solution, by running my app on a localserver. I used GCDWebServer and everything went OK.
Not sure why happened this sound issue and solved by that way, but i suppose it's a matter of javascript "security" on iOS or something similar.
This post game me the answer.

iOS UIDocumentInteractionController, presentOptionsMenuFromRect fails, but presentOpenInMenuFromRect works

Whenever I try show the UIDocumentInteractionController with the method presentOptionsMenuFromRect no file is attached in the opening app and I get this in the console:
Couldn't get file size for (null): (null)
But if I test with presentOpenInMenuFromRect it works perfectly fine. I also use presentPreviewAnimated with its delegates and this works too. Also tested successfully switching to an UIActivityViewController. The file I try to open is a PDF stored in the local documents directory
let documentInteractionController = UIDocumentInteractionController.init(URL: url)
documentInteractionController.presentOptionsMenuFromRect(self.view.bounds, inView: self.view, animated: true)
Xcode: 7.3.1 (Swift)
iOS: 9.3 tested with simulator and device, deployment target is 8.0
Thanks
This happens because in your code, the UIDocumentInteractionController instance might be being released by reaching the end of a function, while it's needed to let the other app handle the file properly after selecting an option from the menu.
To avoid this problem, we need to store a strong reference to our document interaction controller in a place that will exist for the lifetime of the object's owner (i.e. a global variable) in order to keep it alive.
Note: Apparently, this behavior does not occur with some of the options from the Open In menu nor by sharing with AirDrop.

AVAssetExportSession exportSessionWithAsset:presetName: failing on iPad Pro Simulator

On the Xcode 7.1.1 iPad Pro simulator, the following AVAssetExportSession method returns nil, given a valid AVAsset:
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:avAsset presetName:AVAssetExportPresetAppleM4A];
This method always succeeds for me on other iOS 8 and 9 devices and simulators.
I don't have an actual iPad Pro, so I'm not sure whether this is specific to the iPad Pro simulator.
Does anyone have any info on this? (I use AVAssetExportSession to trim the ends of audio files.)
Also, is there a way to query for the error associated with a failure for this method (since it doesn't return an error code)?
Here is a link to my sample Xcode project, stored on Dropbox. If anyone has an actual iPad Pro device, could you try running this? In the onExportAudio: method, could you see whether exportSession gets set to nil? (There's also an audio trimming method that gets called if exportSession is valid...I would be curious if the trimming code successfully trims the 4 second source audio file into a 2 second destination audio file.) ... https://www.dropbox.com/s/zy2qpx94h2pltpi/AudioExportTest_711.zip?dl=0
Thank you!
-Allan
I tried reproducing this issue on a real iPad Pro today, and the issue did not occur.
It appears to be Simulator-specific.
-Allan

Play System Sound With Swift In iOS 8 Beta 5

I am trying to play short system sound with swift in my project , but can't make it work. I searched for the answer , unfortunately non of them were worked for me even if they worked for others . Here is my code .
//System Keyboard Tock Sound
func playInputClick() {
let filePath = NSBundle.mainBundle().pathForResource("Tock", ofType: "caf")
let fileURL = NSURL(fileURLWithPath: filePath!)
var soundID:SystemSoundID = 0
AudioServicesCreateSystemSoundID(fileURL, &soundID)
AudioServicesPlaySystemSound(soundID)
}
The objective-c version of this code was working fine , and now the swift version not working for me , when try to play the sound nothing is happening in the simulator , when I tap the button to play on the device it slows down the tapping action and makes no sound.
I can't figure out what causes the problem , is it the betas ? But exact the same code worked on the device for some people in this forum . maybe this is more about a question of calling C methods in swift.
This is from System log:
[MPUSystemMediaControls] Updating supported commands for now playing application.
Failed to inherit CoreMedia permissions from 2743: (null)
Reinstalling X-Code solved my issue , I have no idea what was wrong. The above code only works on real-devices not on simulator, if somebody need to use the code .

Fresh SpeakHere example app has error when recording audio in Simulator/Xcode

I can't record audio using the SpeakHere example app from apple. When I run the app in Simulator from within Xcode, it starts up normally, but when I press the record button, the error "Thread 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)" occurs:
The log message about the missing root view controller at app startup is already there BEFORE the above error occurs and it is probably not connected to my problem.
I have downloaded the SpeakHere example project from the linked website (see top of this question), opened the fresh download in Xcode and directly started the app. I did not modify any setting and not any line of code. I've also searched on google and stackoverflow for this problem and didn't find a solution, although this problem must be very general.
I use Xcode Version 4.5.2 (4G2008a) and a MacBook Pro from late 2009 with Mac OS X 10.8.
I've also had a friend try this on his computer and he has the very same problem. He has the same OS and his XCode version is also 4.5.2.
I would now try older Xcode versions, but right now I don't like to download a few gigabytes for a trial'n'error approach on my connection.
Any help appreciated, including reports like "works for me with Xcode version ...". Thanks!
The problem occurs because in the method AQRecorder::StartRecord(CFStringRef inRecordFile), the function CFURLCreateWithString() fails and returns a pointer to nil. This is not detected and later on the code calls CFRelease() on this nil pointer, which causes the EXC_BREAKPOINT.
The purpose of the method CFURLCreateWithString() basically is to take a url string as input and return a pointer to a CFURL object as output. The problem here is that the input is not a url string. Instead, it's simply a path on the local file system without file:/ or the like as prefix. For this reason, this method fails.
The solution is to remove the not-working call to the method CFURLCreateWithString() and instead call a related method, namely CFURLCreateWithFileSystemPath(), which is prepared to take a local file system path and convert it to a CFURL:
In the method AQRecorder::StartRecord(CFStringRef inRecordFile), replace or comment out the line
url = CFURLCreateWithString(kCFAllocatorDefault, (CFStringRef)recordFile, NULL);
and insert
url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)recordFile, kCFURLPOSIXPathStyle, false);
at its place.
url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)recordFile, kCFURLPOSIXPathStyle, false);
The code above made my xcoce 4.6 compile and run in simulator, but it doesnot record my voice from my usb microphone, I test my microphone in the garash band application and sucessfully record and play my voice, and the dbmeter does not move at all, any way when I port it to the real device it work, it just can't record and play voice in my simulator.

Resources