STEP-1 : Create project and add a mp3 file(song.mp3) to project.
STEP - 2 : Add five button and give name according to below image
STEP-3 :
ViewController.swift
//
// ViewController.swift
// AudioPlayer1
//
// Created by Divakar Singh on 4/5/17.
// Copyright © 2017 Divakar Singh. All rights reserved.
//
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player : AVAudioPlayer = AVAudioPlayer()
@IBAction func play(_ sender: Any) {
player.play()
}
@IBAction func pause(_ sender: Any) {
player.pause()
}
@IBAction func replay(_ sender: Any) {
player.currentTime = 0
}
@IBAction func stop(_ sender: Any) {
player.stop()
}
@IBAction func playtime(_ sender: Any) {
player.play(atTime: player.deviceCurrentTime+3.0)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
do{
let audioPath = Bundle.main.path(forResource: "song", ofType: "mp3")
try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
}catch
{
// Error
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
22
No comments:
Post a Comment