Play dialler tone on ear-speaker - ios

I'm currently working on a VOIP based application. In which when a user calls another person I need to play a dialler tone on the ear-speaker. I've searched a lot, but couldn't find any solution yet. I've used the following code to play the dialler tone:
// This method plays the ringtone for incoming call
func playRingtone()
{
let path = Bundle.main.path(forResource: "ringing.mp3", ofType:nil)!
let url = URL(fileURLWithPath: path)
do
{
ringtonePlayer = try AVAudioPlayer(contentsOf: url)
ringtonePlayer?.numberOfLoops = 5
ringtonePlayer?.play()
}
catch let error
{
print(error.localizedDescription)
}
}
But it's playing on the loud speaker, not on the ear-speaker.
Additional Info: I'm using TokBox SDK
Can anyone please help me to solve this issue? Thanks in advance.

Related

Limit audio duration when played

I'm trying to build an iOS application using Swift 4 that would play different sounds. Part of what I need to do is to limit the duration of the sound file played depending on some settings.
What I'd like to know is if it's possible to set the duration of the sound prior to playing it so it can stop after the set duration. If so, how?
I'm currently using Swift's AVAudioPlayer but I don't know if it can do it. My current code is shown below:
// resName set depending on settings
url = Bundle.main.url(forResource: resName, withExtension: "mp3")
do{
audioPlayer = try AVAudioPlayer(contentsOf: url!)
audioPlayer.prepareToPlay()
audioPlayer.currentTime = 0
} catch let error as NSError{
print(error.debugDescription)
}
audioPlayer.play()
Thanks in advance for the help :)
Well, since you are in control of playing the sounds, one way how to deal with it would be running a Timer when you start playing that would after the given time period stop playing:
let timeLimit = 1.6 // get it from settings
player.play()
Timer.scheduledTimer(withTimeInterval: timeLimit, repeats: false) { (timer) in
player.stop()
}
And in case you need to cancel it, you can keep a reference to the timer and cancel it using timer.invalidate().
As an audio engineer, I recommend editing your audio to be exactly how you want it to avoid any unwanted artifacts that can happen from coding.

swift 3 using speech recognition and AVFoundation together

I am successfully able to use Speech (speech recognition) and I can use AVFoundation to play wav files in Xcode 8/IOS 10. I just can't use them both together. I have working speech recognition code where I import Speech. When I import AVFoundation into the same app and use the following code, there is no sound and no errors are generated:
var audioPlayer: AVAudioPlayer!
func playAudio() {
let path = Bundle.main.path(forResource: "file.wav", ofType: nil)!
let url = URL(fileURLWithPath: path)
do {
let sound = try AVAudioPlayer(contentsOf: url)
audioPlayer = sound
sound.play()
} catch {
//handle error
}
}
I assume it is because both use audio. Can anyone suggest how to use both in the same app? I also find that I cannot use speech recognition and text-to-speech together in the same app.
I just bumped into the same problem, and here is how I solved,
add the following line when Speech Recognition is done. What it does is basically setting the audio session back to AVAudioSessionCategoryPlayback category.
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
try audioSession.setActive(false, with: .notifyOthersOnDeactivation)
} catch {
// handle errors
}
hope it helps.
You should change this line:
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
on to:
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
This should work ;-)
It seems that AVAudioPlayer stops playing the sample if you're using AVAudioSession to record the microphone as in Apple's speech recognition example.
However, I've managed to circumvent this by using AVCaptureSession to capture audio as described in this answer.

Play a sound before opening an URL

Explanation
I'm having a problem when calling an action before opening App Store to rate my game. The sound won't be played. I've seen a game in which it worked. Am I doing anything wrong?
Code
func rateGame(){
let appIDString = "375380948"
let reviewsURLString = "itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?pageNumber=0&sortOrdering=1&type=Purple+Software&mt=8&id=\(appIDString)"
let reviewsURL = NSURL(string: reviewsURLString)
clickSoundFX()
UIApplication.sharedApplication().openURL(reviewsURL!)
}
func clickSoundFX(){
self.runAction(SKAction.playSoundFileNamed("buttonClick.mp3", waitForCompletion: false))
}
Thanks in advance,
Luiz.
When you launch clickSoundFX() and after you call:
UIApplication.sharedApplication().openURL(reviewsURL!)
your game start to make an action to play a simple sound but right after all the app going in applicationDidEnterBackground due to open an external url: in this exact moment all your game going in paused mode so you can't do any action.
A safe way to do your code is to make a pause sequence after your sound and check the completion like this:
let wait = SKAction.SKAction.waitForDuration(1.0)
let sound = SKAction.playSoundFileNamed("buttonClick.mp3")
let seq = SKAction.sequence([sound,wait])
self.runAction(seq,completion: {
UIApplication.sharedApplication().openURL(reviewsURL!)
})

Playing Audio With Swift 2 in Xcode 7

I am trying to play background music on an app I am creating, but none of the solutions I have seen online work. I have experimented with about 5 different blocks of code that others said worked for them but have been short of success on all the attempts. My current code is down below(it is also not working even though there are no errors), I would greatly appreciate it if anyone could give me an explanation as to what I am doing wrong. Thanks!
func playBackgroundMusic() {
do {
self.audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("cool_song", ofType: "mp3")!))
self.audioPlayer.play()
} catch {
print("Error")
}
}

Why isn't anything playing when the rate is 1.0?

I am trying to play a song from a link using the avkit, but nothin is being played. I used an online mp3 file to link converter to generate a link. If anyone has a better method to convert an mp3 file to a link I am all ears. Apart from that the real problem I'm having is why I can't hear anything. I have a feeling it's the link and it is not supported. Keep in mind that playedSong is being called in the viewDidLoad. Link to the song I am trying to play
var urlPath = "http://picosong.com/ANQY"
func playedSong(){
let url: NSURL = NSURL(string: urlPath)!
player = AVPlayer(URL: url)
player.play()
player.rate = 1.0
if(player.rate == 1.0){
print(url)//prints correct url
print("playing")//prints
}else{
print("not playing")
}
}

Resources