We will make a fake snore sound app.
Create a new project. Drag mp3 file, icons
file in to tree folder. You can go to http://www.freesound.org/ to download free sounds.
Import library.
import AVFoundation
Add to class declaration.
AVAudioPlayerDelegate
Declare button, imageView, variables
to above viewDidLoad.
var player = AVAudioPlayer()
var bu1:UIButton!
var bu2:UIButton!
var bu3:UIButton!
var bu4:UIButton!
var bu5:UIButton!
var bu6:UIButton!
let im1 = UIImage(named:
"nn");
let im2 = UIImage(named:
"cb")
let im3 = UIImage(named:
"bb");
let im4 = UIImage(named:
"bc")
var ima1: UIImageView!
var ima2: UIImageView!
var po=0;
var po2=0;
var po3=0;
var count=0
var count2=0
var count3=0
Set position, add to view. Copy in
to viewDidLoad.
ima1 = UIImageView(frame: CGRect(x:
0, y: 10,
width: 340, height: 340))
ima2 = UIImageView(frame: CGRect(x:
45, y: 420,
width: 240, height: 40))
ima1.image = im1
ima2.image = im2
ima1.center = CGPoint(x: view.center.x, y:180)
ima2.center = CGPoint(x: view.center.x, y:390)
bu1 = UIButton(frame: CGRect(x:80, y: 440, width: 100,
height: 42))
bu2 = UIButton(frame: CGRect(x:200, y: 440, width: 100,
height: 42))
bu1.setBackgroundImage(im3,
forState: .Normal)
bu2.setBackgroundImage(im4,
forState: .Normal)
bu1.setTitle("Snore",forState: .Normal)
bu1.setTitleColor(UIColor.blueColor(), forState: .Normal)
bu2.setTitle("Pause",forState: .Normal)
bu2.setTitleColor(UIColor.blueColor(), forState: .Normal)
bu3 = UIButton(frame: CGRect(x:80, y: 500, width: 100,
height: 42))
bu4 = UIButton(frame: CGRect(x:200, y: 500, width: 100,
height: 42))
bu3.setTitle("Nút bấm",forState: .Normal)
bu4.setTitleColor(UIColor.blueColor(), forState: .Normal)
bu3.setTitle("Snore",forState: .Normal)
bu3.setTitleColor(UIColor.blueColor(), forState: .Normal)
bu5 = UIButton(frame: CGRect(x:80, y: 560, width: 100,
height: 42))
bu6 = UIButton(frame: CGRect(x:200, y: 560, width: 100,
height: 42))
bu4.setTitle("Pause",forState: .Normal)
bu6.setTitleColor(UIColor.blueColor(), forState: .Normal)
bu5.setTitle("Snore",forState: .Normal)
bu5.setTitleColor(UIColor.blueColor(), forState: .Normal)
bu6.setTitle("Pause",forState: .Normal)
bu3.setBackgroundImage(im3,
forState: .Normal)
bu4.setBackgroundImage(im4,
forState: .Normal)
bu5.setBackgroundImage(im3,
forState: .Normal)
bu6.setBackgroundImage(im4,
forState: .Normal)
view.backgroundColor = UIColor.whiteColor()
// Do any additional setup after
loading the view.
view.addSubview(bu1)
view.addSubview(bu2)
view.addSubview(bu3)
view.addSubview(bu4)
view.addSubview(bu5)
view.addSubview(bu6)
view.addSubview(ima1)
view.addSubview(ima2)
Run to see screen like this.
Copy function play music to above
last close bracket.
func play(songPath: String){
let songUrl = NSURL.fileURLWithPath(songPath)
do {
tryplayer = AVAudioPlayer(contentsOfURL: songUrl)
player.delegate = self
player.prepareToPlay()
player.play()
player.numberOfLoops = -1
}
catch (let error asNSError) {
//print("audioPlayer error:
\(error)")
}
player.play()
}
Copy funtions for first two buttons.
func but1(sender: UIButton){
count=1
count2=0
count3=0
po=0
let songPath = NSBundle.mainBundle().pathForResource("ac", ofType: "WAV")!
play(songPath)
}
func but2(sender: UIButton){
po=po+1
if(count==1){
if(po%2==1){
player.pause()
}
else{
player.play()
}
}
}
Let look at code.
Button 1 we set count=1 mean music
playing, play music and set po=0 mean not in pausing. Other count variables = 0
mean not playing.
Button 1 we plus po=po+1 and get
modulo 2 to set play when user click one time, set pause if click two times and
so on.
Another buttons the same.
Copy functions for other buttons and
function to hide status bar.
func but3(sender: UIButton){
count2=1
count=0
count3=0
po2=0
let songPath = NSBundle.mainBundle().pathForResource("a", ofType: "wav")!
play(songPath)
}
func but4(sender: UIButton){
po2=po2+1
if(count2==1){
if(po2%2==1){
player.pause()
}
else{
player.play()
}
}
}
func but5(sender: UIButton){
count3=1
count=0
count2=0
po3=0
let songPath = NSBundle.mainBundle().pathForResource("ab", ofType: "mp3")!
play(songPath)
}
func but6(sender: UIButton){
po3=po3+1
if(count3==1){
if(po3%2==1){
player.pause()
}
else{
player.play()
}
}
}
overridefunc prefersStatusBarHidden() ->Bool {
returntrue
}
Connect functions to buttons in
viewDidLoad.
bu1.addTarget(self, action: #selector(but1(_:)), forControlEvents: UIControlEvents.TouchUpInside)
bu2.addTarget(self, action: #selector(but2(_:)), forControlEvents: UIControlEvents.TouchUpInside)
bu3.addTarget(self, action: #selector(but3(_:)), forControlEvents: UIControlEvents.TouchUpInside)
bu4.addTarget(self, action: #selector(but4(_:)), forControlEvents: UIControlEvents.TouchUpInside)
bu5.addTarget(self, action: #selector(but5(_:)), forControlEvents: UIControlEvents.TouchUpInside)
bu6.addTarget(self, action: #selector(but6(_:)), forControlEvents: UIControlEvents.TouchUpInside)
Run app to test buttons.
Add Quit button with command player.stop() to stop music when user
exit app.
No comments:
Post a Comment