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
}
Related
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 3 years ago.
Improve this question
I'm developing an iOS application using Swift 5 that will convert plain text to pdf format. And I want to share the pdf file with Whatsapp application, but I couldn't find how to save the pdf file before and then the url of the file I saved with Whatsapp. how can I do that?
Edit: I’m converting the plain text to pdf format I have pdf data in my hand and I want to save it to local and get the url of it
You must use a UIDocumentInteractionController.
var interactionController: UIDocumentInteractionController!
And then via button (for example) you can open the controller.
#IBAction func savePDF(_ sender: Any) {
self.interactionController = UIDocumentInteractionController(url: "http://website.com/title.pdf")
self.interactionController.delegate = self
self.interactionController.presentOpenInMenu(from: button.frame, in: view, animated: true)
}
UPDATE:
let title = "PDF Title"
guard let outputURL = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent(title).appendingPathExtension("pdf")
else { fatalError("URL not created") } // PDF url
pdfData.write(to: outputURL, atomically: true) // pdfData is your PDF file
self.interactionController = UIDocumentInteractionController(url: outputURL)
self.interactionController.delegate = self
self.interactionController.presentOpenInMenu(from: button.frame, in: view, animated: true)
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 5 years ago.
Improve this question
I have no idea how to select a video from the camera roll using Swift in iOS. I am making an instagram app clone and I want users to only be able to select videos from their camera roll.
I have searched this question on google and only found tutorials explaining how to select images from the camera roll.
You can use this:
func getMovie() {
let selecionadorDeFoto = UIImagePickerController()
selecionadorDeFoto.delegate = self
selecionadorDeFoto.mediaTypes = [kUTTypeMovie as String]
selecionadorDeFoto.allowsEditing = false
selecionadorDeFoto.sourceType = .photoLibrary
present(selecionadorDeFoto, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// Your code here
}
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")
}
}
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()
}
}
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"))