I am trying to add a sound to my UNNotification. Instead of my own custom sound or the default sound, I would like to use one of the iOS SystemSounds in AVFoundation. I can get the sound by:
import AVFoundation
...
let systemSoundID: SystemSoundID = 1016
AudioServicesPlaySystemSound(systemSoundID)
How can I add this sound to my notification?
Related
I'm developing app for sound effects. I use AudioKit and I got a needed result for recoding voice. Now I want to add sound effect to voice while video is recording. I can record video with default voice, but I don't know how I can add effect to voice from video.
I use AVCaptureSession for video recording and I get audio stream like this:
self.audioDevice = AVCaptureDevice.default(for: AVMediaType.audio)
if let audioDevice = self.audioDevice {
self.audioInput = try AVCaptureDeviceInput(device: audioDevice)
if captureSession.canAddInput(self.audioInput!) {
captureSession.addInput(self.audioInput!)
} else {
throw CameraControllerError.inputsAreInvalid
}
}
But I want to set to captureSession input AVAudioNode, because AudioKit works with it.
I will be glad for any help. Thanks
All of my other audio files play perfectly through the audio player. Most of the files are less than 5 seconds long, and the longest file that still plays is 23 seconds long. For some reason my 1:15 long file "sortSong" is completely silent when it is called.
Here is the code for my audio player:
import Foundation
import AVFoundation
class AudioPlayer {
static let sharedInstance = AudioPlayer()
enum Sound: String {
case sortSongPlay = "sortSong"
case centerButtonRelease = "centerButtonRelease"
case buttonTap = "tapSound"
static var all: [Sound] {
return [.centerButtonRelease, .buttonTap, sortSongPlay]
}
var url: URL {
return URL.init(fileURLWithPath: Bundle.main.path(forResource: self.rawValue, ofType: "mp3")!)
}
}
private var soundMap = [String: SystemSoundID]()
init() {
for sound in Sound.all {
let soundUrl = sound.url
var soundId: SystemSoundID = 0
AudioServicesCreateSystemSoundID(soundUrl as CFURL, &soundId);
soundMap[sound.rawValue] = soundId
}
}
func play(sound: Sound) {
AudioServicesPlaySystemSound(soundMap[sound.rawValue]!)
}
}
The sound is played when this function is called in my view controller.
func successfulSort() {
AudioPlayer.sharedInstance.play(sound: .sortSongPlay)
rotateSortImageOne()
rotateSortImageTwo()
}
This is the action that calls the successfulSort func
if inputText == "hello" && seedArray == [1, 4, 1] {
successfulSort()
}
If I simply change the case sortSongPlay to = "shortSortSong" (the 23 second version) it plays just fine.
All of my sound files have their target memberships checked for this project file, and all of the files have the correct path. The audio will play in the interface builder if I press the play button. There are no compiler errors or warnings, the app never crashes, the audio for sortSong simply isn't playing when it is called in the app.
This is a link containing examples I have tried on my project. The first two sounds play silently in the app while the shorter sounds all play perfectly. https://soundcloud.com/spiffystache
What is causing this to be silent?
You should put a correct title on your question.
Your code does not use AVAudioPlayer, but uses System Sound Services.
The documentation clearly states its limitation:
You can use System Sound Services to play short (30 seconds or
shorter) sounds.
I have never checked if its limitation is exactly 30 seconds, but it matches the description in your question.
Consider using AVAudioPlayer (as in the title) to play longer sound.
In AVKit how it can be used this three lines of codes and also how to mute the music ?
moviePlayer!.scalingMode = MPMovieScalingMode.AspectFill
moviePlayer!.controlStyle = MPMovieControlStyle.None
moviePlayer!.shouldAutoplay = true
You may consider using AVFoundation to mute audio playback using .muted on your AVPlayer.
See https://developer.apple.com/library/content/qa/qa1716/_index.html
Im trying to play a really long audio file in iOS (like 10 minutes long) and it doesn't want to play. I've gotten other files to work with this code just fine but this one just doesn't want to play.
Heres my code:
void SoundFinished (SystemSoundID snd, void* context) {
NSLog(#"finished!");
AudioServicesRemoveSystemSoundCompletion(snd);
AudioServicesDisposeSystemSoundID(snd);
}
- (IBAction)lolol{
NSURL* sndurl = [[NSBundle mainBundle] URLForResource:#"full video" withExtension:#"mp3"];
SystemSoundID snd;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)sndurl, &snd);
AudioServicesAddSystemSoundCompletion(snd, nil, nil, SoundFinished, nil);
AudioServicesPlaySystemSound(snd);
}
The file is too long for System Sound Services to play it as mentioned in the docs -
You can use System Sound Services to play short (30 seconds or shorter) sounds.
I suggest using an alternate way to play the file. Have a look at using AVAudioPlayer class to play the audio file.
I'm trying to add an audio input to my AVCaptureSession() and it works great. However I would also like to support users who wish to play music in the background from other apps such as Spotify and maintain this audioInput for my recording. How is this possible?
let captureSession = AVCaptureSession()
let audioDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)
audioInput = AVCaptureDeviceInput.deviceInputWithDevice(audioDevice, error:&err) as? AVCaptureDeviceInput
if captureSession.canAddInput(videoCapture) {
captureSession.addInput(videoCapture)
// This line Kills spotify playing in the background
captureSession.addInput(audioInput as AVCaptureInput)
}
The category AVAudioSessionCategoryPlayback is one of the few categories that allow for backgrounding. The option AVAudioSessionCategoryOptionMixWithOthers will make sure your audio won’t stop any currently playing background audio and also make sure that when the user plays music in the future, it won’t kick off your background task.
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: .MixWithOthers)
Maybe this will be helpful