I'm trying to play an m4a file on an iOS device programatically. The file is downloaded from a server and stored. When the user presses a button, it is played. The file, when copied from the device to OSX does play fine. I am using the code
var player:AVAudioPlayer = AVAudioPlayer()
func playAudio(sender: UIButton!) {
let audioPath = FileUtils.getPath(audioPaths[sender.tag])
print("PATH " + audioPath)//The printed path IS DEFINETLY correct
do{
player = try! AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: audioPath))
player.prepareToPlay()
player.play()
}
}
The path is correct so I'm not sure why it won't work. Could it be the format (even though it plays on OSX)?
An example file: https://www.dropbox.com/s/csewwg6n9vzan5z/131015-08%3A13%3A30.m4a?dl=0
That file won't play on iOS because it's encoded in AMR NarrowBand, which is no longer supported.
I would try re-encoding the m4a files as AAC, or switch to mp3.
It's likely just a feature of the way this m4a file is compressed. The simple fact is that not every m4a file, even if it is a perfectly valid file, will play on your device. (For example, perhaps this file is very compressed.) And to make things more complicated, it differs from device to device! And to make things even more complicated, the file might play in the Music app but not in an AVAudioPlayer!!
To test this theory, recode the file as a more middle-of-the-road m4a and see if that plays.
Related
My app uses remote notifications with a NotificationService Extension in which I edit the notification before displaying it.
I would like to let the user upload a custom sound file which should be played instead of the default sound. For this I use an shared AppGroup, which the app and the extension have access to.
The uploaded sound files are stored in the "Library/Sounds" directory as follows (my code for testing, without much error handling):
.....
let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.xxx.xxx")
let soundsURL = containerURL!.appendingPathComponent("Library/Sounds/", isDirectory: true)
if !FileManager.default.fileExists(atPath: soundsURL.path) {
try! FileManager.default.createDirectory(atPath: soundsURL.path, withIntermediateDirectories: true)
}
if FileManager.default.fileExists(atPath: soundsURL.path) {
do {
try FileManager.default.copyItem(at: sourceURL, to: soundsURL.appendingPathComponent(sourceURL.lastPathComponent))
} catch {
// Exception
}
}
In the Notification Extension I change the sound of the notification to the name of the uploaded file:
bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "test.wav"))
This is working fine as long as the iPhone is not locked. But if the iPhone is locked, there is no vibration and no sound is played (also no default sound). But I don't know why - according to apples documentation UNNotificationSound looks in "Library/Sounds" of the app shared group container directories. If I store the file directly in the main bundle, it works.
Does anyone have any idea what could be causing this?
Ok, now i figured out what the problem is.
My files were created with "NSFileProtectionComplete" data protection by default.
"NSFileProtectionComplete — The file is only accessible while the device is unlocked."
After changing the data protection of my sound files to "NSFileProtectionNone", it finally works!
I'm working on a project where I have an instance of AVPlayer capable of playing different audio content that I retrieve from a backend, from podcast to music and streamings. Every content has two types of urls: one with mp3 and another with a m3u8 file. All the mp3 files work good. However some m3u8 files work fine and others don't. In particular, those who don't work cause the AVPlayer to crash with the error:
Error Domain=AVFoundationErrorDomain Code=-11819 "Cannot Complete Action"
UserInfo={NSLocalizedRecoverySuggestion=Try again later.,
NSLocalizedDescription=Cannot Complete Action.}
I don't understand what the problem is. According to this answer it is a wrong Manifest file, which in my case is - for example - the following:
#EXTM3U
#EXT-X-MEDIA:TYPE=AUDIO,URI="_64/index.m3u8",GROUP-ID="2#48000-64000",NAME="AAC 64",DEFAULT=NO,AUTOSELECT=NO
#EXT-X-MEDIA:TYPE=AUDIO,URI="_80/index.m3u8",GROUP-ID="2#48000-80000",NAME="AAC 80",DEFAULT=NO,AUTOSELECT=NO
#EXT-X-MEDIA:TYPE=AUDIO,URI="_96/index.m3u8",GROUP-ID="2#48000-96000",NAME="AAC 96",DEFAULT=NO,AUTOSELECT=NO
#EXT-X-STREAM-INF:BANDWIDTH=133336,CODECS="mp4a.40.2",AUDIO="2#48000-96000"
_96/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=100641,CODECS="mp4a.40.2",AUDIO="2#48000-64000"
_64/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=116989,CODECS="mp4a.40.2",AUDIO="2#48000-80000"
_80/index.m3u8
On the Apple forum, I found this answer which says iOS 14+ is on fault. Unfortunately I cannot test with an iOS 13 physical device.
Do you have any suggestion?
Tested on Xcode 13.1 with iPhone 7plus with iOS 15.0.2.
Finally I found a solution for this issue. What worked for me was this. I believe the problem was that my manifest files were structured like the following:
#EXT-X-MEDIA:TYPE=AUDIO,URI="_64/index.m3u8", GROUP-ID="1#48000-64000",NAME="Audio 64",DEFAULT=NO,AUTOSELECT=NO
In particular they had DEFAULT=NO,AUTOSELECT=NO. Therefore before calling replaceCurrentItem I now do the following:
let asset = AVAsset(url: url)
let playerItem = AVPlayerItem(asset: asset)
for characteristic in asset.availableMediaCharacteristicsWithMediaSelectionOptions {
if let group = asset.mediaSelectionGroup(forMediaCharacteristic: AVMediaCharacteristic.audible) {
if let option = group.options.first {
playerItem.select(option, in: group)
}
}
}
This makes all my HLS audio playable by the AVPlayer.
I dont see version in your .m3u8. Try adding #EXT-X-VERSION:03 into your playlist. AVPlayer does need to have version included in playlist (Android EXO player does not need it). Here is example of playlist that might work:
#EXTM3U
#EXT-X-VERSION:03
#EXT-X-MEDIA:TYPE=AUDIO,URI="_64/index.m3u8",GROUP-ID="2#48000-64000",NAME="AAC 64",DEFAULT=NO,AUTOSELECT=NO
#EXT-X-MEDIA:TYPE=AUDIO,URI="_80/index.m3u8",GROUP-ID="2#48000-80000",NAME="AAC 80",DEFAULT=NO,AUTOSELECT=NO
#EXT-X-MEDIA:TYPE=AUDIO,URI="_96/index.m3u8",GROUP-ID="2#48000-96000",NAME="AAC 96",DEFAULT=NO,AUTOSELECT=NO
#EXT-X-STREAM-INF:BANDWIDTH=133336,CODECS="mp4a.40.2",AUDIO="2#48000-96000"
_96/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=100641,CODECS="mp4a.40.2",AUDIO="2#48000-64000"
_64/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=116989,CODECS="mp4a.40.2",AUDIO="2#48000-80000"
_80/index.m3u8
I am trying to play an hls live stream from a url that looks like this: "http://ip.address:port/my%20video.m3u8". Whenever the url does not involve any spaces the video plays fine otherwise it does not play. There is no error logged from the player but the player itself is just a black screen. I enabled "Allow Arbitrary Loads" and still no dice. When I try loading the urls with spaces in safari the video will play. Here is my code to load and play the video:
DispatchQueue.main.async {
var player = AVPlayer(url: url!)
var playerLayer: AVPlayerLayer!
playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resize
self.videoView.layer.addSublayer(playerLayer)
playerLayer.frame = self.videoView.bounds
player.play()
}
What I also find odd is when I try sending the stream to a AVPlayerViewController the player pops up but does not play. Here is how I am sending it to the view controller:
DispatchQueue.main.async {
let player = AVPlayer(url: urlTwo)
let playerController = AVPlayerViewController()
playerController.player = player
present(playerController, animated: true) {
player.play()
}
}
Please check the m3u8 file may be the content in m3u8 have some directory issue. .ts segment and .key file was mentioned in m3u8 file. And when you pass m3u8 to avplayer it fetch the key and .ts file automatically and decrypt the .ts with the mentioned key. Just cross check you m3u8 for key and .ts file.
Also, .key and .ts files need to be in same folder along with m3u8 otherwise it will not fetch and player keep looking for the key and ts.
#EXTM3U
#EXT-X-TARGETDURATION:12
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-VERSION:4
#EXT-X-KEY:METHOD=AES-128,URI="title.key"
#EXTINF:10.0100,
#EXT-X-BYTERANGE:943584#0
title.ts
Something like this.
Note: Sometimes m3u8 and key file have additional encryption which you need to decrypt on run time. I suggest open it in text editor and see if you can see something like given structure above. If it has something unreadable then you need to decrypt it first on run time. If you are using local server and loading file from cache then save the m3u8 file after decryption with Encoding.utf8 and .key file with Encoding.macOSRoman. Otherwise it will not gonna work. Cheers.
I am in the process of developing a game for iOS 9+ using Sprite Kit and preferably using Swift libraries.
Currently I'm using a Singleton where I preload my audio files, each connected to a separate instance of AVAudioPlayer.
Here's a short code-snipped to get the idea:
import SpriteKit
import AudioToolbox
import AVFoundation
class AudioEngine {
static let sharedInstance = AudioEngine()
internal var sfxPing: AVAudioPlayer
private init() {
self.sfxPing = AVAudioPlayer()
if let path = NSBundle.mainBundle().pathForResource("ping", ofType: "m4a") {
do {
let url = NSURL(fileURLWithPath:path)
sfxPing = try AVAudioPlayer(contentsOfURL: url)
sfxPing.prepareToPlay()
} catch {
print("ERROR: Can't load ping.m4a audio file.")
}
}
}
}
This Singleton is initialised during app start-up. In the game-loop I then just call the following line to play a specific audio file:
AudioEngine.sharedInstance.sfxPing.play()
This basically works, but I always get glitches when a file is played and the frame rate drops from 60.0 to 56.0 on my iPad Air.
Someone any idea how to fix this performance issue with AVAudioPlayer ?
I also watched out for 3rd party libraries, namely:
AudioKit [Looks very heavy-weighted]
ObjectAL [Last Update 2013 ...]
AVAudioEngine [Based on AVAudioPlayer, same problems ?]
Requirements:
Play a lot of very short samples (like shots, hits, etc..)
Play some motor effects (thus pitching would be nice)
Play some background / ambient sound in a loop
NO nasty glitches / frame rate drops !
Could you recommend any of the above mentioned libraries for my requirements or point out the problems using the above code ?
UPDATE:
Playing short sounds with:
self.runAction(SKAction.playSoundFileNamed("sfx.caf", waitForCompletion: false))
does indeed improve the frame rate. I exported the audio files with Audiacity to the .caf format (Apple's Core Audio Format). But in the tutorial, they export with "Signed 32-bit PCM" encoding which led to disturbed audio playback in my case. Using any of the other encoding options (32-bit float, U-Law, A-Law, etc..) worked fine for me.
Why using caf format? Because it's uncompressed and thus loaded faster into memory with less CPU overhead compared to compressed formats like m4a. For short sound effects played a lot in short intervals, this makes sense and disk usage is not affected much for short audio files consuming few kilobytes. For bigger audio files, like ambient and background music, using compressed formats (mp3, m4a) is obviously the better choice.
According to your question, if you develop a game for iOS 9+, you can use the new iOS 9 library SKAudioNode (official Apple doc):
var backgroundMusic: SKAudioNode!
For example you can add this to didMoveToView():
if let musicURL = NSBundle.mainBundle().URLForResource("music", withExtension: "m4a") {
backgroundMusic = SKAudioNode(URL: musicURL)
addChild(backgroundMusic)
}
You can also use to play a simple effect:
let beep = SKAudioNode(fileNamed: "beep.wav")
beep.autoplayLooped = false
self.addChild(beep)
Finally, if you want to change the volume:
beep.runAction(SKAction.changeVolumeTo(0.4, duration: 0))
Update:
I see you have update your question speaking about AVAudioPlayer and SKAction. I've tested both of them for my iOS8+ compatible games.
About AVAudioPlayer, I personally use a custom library maked by me based from the old SKTAudio.
I see your code, about AVAudioPlayer init, and my code is different because I use:
#available(iOS 7.0, *)
public init(contentsOfURL url: NSURL, fileTypeHint utiString: String?)
I don't know if fileTypeHint make the difference, so try and fill me about your test.
Advantages about your code:
With a shared instance audio manager based to AVAudioPlayer you can control volume, use your manager wherever you want, ensure compatibility with iOS8
Disadvantages about your code:
Everytime you play a sound and you want to play another sound, the previous is broken, especially if you have launch a background music.
How to solve? According with this SO post to work well without issues seems AVFoundation is limited to 4 AVAudioPlayer properties instantiables, so you can do this:
1) backgroundMusicPlayer: AVAudioPlayer!
2) soundEffectPlayer1: AVAudioPlayer!
3) soundEffectPlayer2: AVAudioPlayer!
4) soundEffectPlayer3: AVAudioPlayer!
You could build a method that switch through the 3 soundEffect to see if is occupied:
if player.playing
and use the next free player. With this workaround you have always your sound played correctly, even your background music.
I am having trouble playing a local video on my computer. I used a library called Player which is quite straight forward, and in the example project, it's using a Vine video with supplying a link at the top, and it's working:
let videoUrl = NSURL(string: "https://v.cdn.vine.co/r/videos/AA3C120C521177175800441692160_38f2cbd1ffb.1.5.13763579289575020226.mp4")!
Thus, in ViewDidLoad, self.player.setUrl(videoUrl) works.
I tried to download the Vine video to my local. I saved it as aaa.mp4.
I dragged the mp4 file in xCode project.
I added the file in Copy Bundle Resources
Then I used;
let path = NSBundle.mainBundle().pathForResource("aaa", ofType:"mp4")!
let videoUrl = NSURL(string: path)!
viewDidLoad() {
// print(path)
// print(videoUrl)
self.player.setUrl(videoUrl)
}
The video doesn't get played, but
print(path) - gives me:
/Users/sentiasa/Library/Developer/CoreSimulator/Devices/67160785-1BDB-4046-BD58-A8C448938A4F/data/Containers/Bundle/Application/5456FF89-904F-4AE6-A90C-747B6A371745/Player.app/aaa.mp4
and print(videoUrl) - gives me:
/Users/sentiasa/Library/Developer/CoreSimulator/Devices/67160785-1BDB-4046-BD58-A8C448938A4F/data/Containers/Bundle/Appl ... /aaa.mp4
Please note that, if I give the path a filename that doesn't exists, it throws an error, thus I know the file is there (and exists) in my case.
What may be the problem? What am I missing? I don't receive any warnings or errors
Use
NSURL(fileURLWithPath: path)!
instead.