How to activate a function after a given time? [closed] - ios

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I would like to activate a function after a sound is played and complete. I assume that NSTimer needs to be included, but I am not sure of how to implement in order to make it. It is helpful if you could give me some code that works.

You should post code that you tried, and then someone then can help you make the code work. In this case, you don't need a timer for what you're doing. The AVAudioPlayerDelegate protocol on the player was designed for what you are trying to do:
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAudioPlayerDelegate {
var audioPlayer:AVAudioPlayer?
func playSound(fileName:NSString)
{
let path = NSBundle.mainBundle().pathForResource(fileName, ofType:"wav")
let url = NSURL.fileURLWithPath(path!)
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: url)
audioPlayer?.delegate = self
audioPlayer?.play()
} catch {
print("Player not available")
}
}
//MARK : AVAudioPlayerDelegate
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
print("finished playing, do something else here")
}
}

Related

How to detect when media player is done playing? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
This may me a simple question, but how do I detect when MPMediaPlayer is done playing in Swift 4?
You can track this using NSNotificationCenter.
func play(url: NSURL) {
let item = AVPlayerItem(URL: url)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: item)
let player = AVPlayer(playerItem: item)
player.play()
}
func playerDidFinishPlaying(note: NSNotification) {
// Your code here
}

Using the Apple Music API to create playlist? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm working on an iOS app(swift) that provides a faster way for users to create playlists from their Apple Music library. After reading the documentation I still can't figure out how to get acces to the users library.
Is there a way I can get accesss to all the songs in the users library and add the song ids to an array?
To access Apple's music library, you will need to add the "Privacy - Media Library Usage Description" to your info.plist. Then you need to make your class conform to MPMediaPickerControllerDelegate. To display the Apple Music library, you present the MPMediaPickerController. To add the songs to an array, you implement the didPickMediaItems method of MPMediaPickerControllerDelegate.
class MusicPicker:UIViewController, MPMediaPickerControllerDelegate {
//the songs the user will select
var selectedSongs: [URL]!
//this method is to display the music library.
func getSongs() {
var mediaPicker: MPMediaPickerController?
mediaPicker = MPMediaPickerController(mediaTypes: .music)
mediaPicker?.delegate = self
mediaPicker?.allowsPickingMultipleItems = true
mediaPicker?.showsCloudItems = false
//present the music library
present(mediaPicker!, animated: true, completion: nil)
}
//this is called when the user selects songs from the library
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
//these are the songs that were selected. We are looping over the choices
for mpMediaItem in mediaItemCollection.items {
//the song url, add it to an array
let songUrl = mpMediaItem.assetURL
selectedSongs.append(songURL)
}
//dismiss the Apple Music Library after the user has selected their songs
dismiss(animated: true, completion: nil)
}
//if the user clicks done or cancel, dismiss the Apple Music library
func mediaPickerDidCancel(mediaPicker: MPMediaPickerController) {
dismiss(animated: true, completion: nil)
}
}

playAtTime is not working, but play() is with AVAudioPlayer() in Swift [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Whenever I run my app with just musicPlayer.play() there is no issue, but I want the app to start at a specific point and when I use playAtTime(timeToPlay) the audio never plays. I have attached the top portion of my code (which I believe is the only relevant portion). Any help would be awesome!
import UIKit
import AVFoundation
import Foundation
class RHCPViewController: UIViewController {
var backgroundMusicPlayer = AVAudioPlayer()
let gradientLayer = CAGradientLayer()
var isPlaying: Bool = false
var timer: NSTimer?
var timeToPlay: NSTimeInterval = 0.00
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.sharedApplication().statusBarHidden = true
}
#IBAction func playButtonWasClicked(sender: AnyObject) {
if isPlaying == false {
let song = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("CantStop", ofType: "mp3")!)
do {
backgroundMusicPlayer = try AVAudioPlayer(contentsOfURL: song, fileTypeHint: nil)
backgroundMusicPlayer.numberOfLoops = -1
backgroundMusicPlayer.prepareToPlay()
backgroundMusicPlayer.playAtTime(timeToPlay)
isPlaying = true
}
catch let error as NSError {
print(error.description)
}
You're passing 0.0 to playAtTime, but the time parameter must be greater than the audio device's current time. Use the deviceCurrentTime property in computing the time parameter as follows:
backgroundMusicPlayer.playAtTime(backgroundMusicPlayer.deviceCurrentTime + timeToPlay)
See also the API docs.

I want to make a sound play starting from the 15th second [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to make a sound play starting from the 15th second.
How can I do it programmatically?
Thanks!
As I can understand you are trying to start your audio not from the beginning. In this case:
You need to use seekToTime
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
let audioUrl = URL(string: "https://www.ringtonemobi.com/storage/upload/user_id_1/iphone-5-alarm-2016-08-21-01-49-25.mp3")!
player = AVPlayer(url: audioUrl)
player.seek(to: .init(seconds: 15, preferredTimescale: 1))
player.play()
}
}

iPhone call from app in Swift Xcode 6 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I checked and tried previous answers on how to make call directly from the app but I keep getting errors. I just made a button in the storryboard and hooked it up to the view controller. It keeps giving me the ERROR Excpected ',' separator but that doesn't solve anything. Do I need to add code to the AppDelegate or anything else? I really would like to know the way to do this in Swift.
import UIKit
class ViewController: UIViewController {
#IBAction func theCallMeButtonMethod(sender: AnyObject) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"telprompt:0123456789"]]
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Swift 3
This code will help you :
let phone = "tel://982374234"
let url = URL(string: phone)!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
OR
let url = URL(string: "tel://9809088798")!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
This is how you access the application object and open a URL in swift.
Replace number below:
UIApplication.sharedApplication().openURL(NSURL(string:"telprompt:0123456789"))

Resources