With the swift language for iOS development, you can use the AV foundation framework if you want to add background music and simply control it, and you use the Avaudioplayer class for compressed audio files, or for more than 30 seconds of sound files.
Add background music to your app
? First, add the line code at the top of the Viewcontroller.swift file:
Import Avfoundation
Introduction of a new avfoundation framework
? Next, under the code that defines other instance variables in the class, add the line code:
var audioplayer:avaudioplayer!
Add a player variable of type Avaudioplayer
? To add a new method:
Func Playbgmusic () {
Let Musicpath = Nsbundle.mainbundle (). Pathforresource ("Bgmusic", OfType: "MP3")
Specify the music path
Let URL = Nsurl (fileurlwithpath:musicpath!)
Audioplayer = Avaudioplayer (Contentsofurl:url, Error:nil)
Audioplayer.numberofloops =-1
Set the number of music plays, 1 for loop playback
Maudioplayer.volume = 1
Set the music volume to a usable range of 0~1
Audioplayer.preparetoplay ()
Audioplayer.play ()
}
For more information about the Avaudioplayer class, see Avaudioplayer class reference.
? Call the function at the appropriate location:
Override Func Viewdidload () {
Super.viewdidload ()
Playbgmusic ()
}
Call function every time the view controller is loaded
Or
Override func Viewwillappear (Animated:bool) {
Playbgmusic ()
}
Call a function every time the view controller's view appears
Viewwillappear is the code that executes every time the view controller's view appears. And Viewdidload is the code that executes every time the view controller is loaded. For example, when a view controller views the first occurrence is two to execute, but when a is push after a pop back, only viewwillappear execution.
Use the music switch button to control music playback
Performs a play, pause, or stop operation on a Avaudioplayer object, which can be checked by the playing property to see if it is currently playing. The following method implements the play/pause control of the music using UIButton, and updates the picture display of the button.
? To define a button variable in a class:
@IBOutlet weak var voicebutton:uibutton!
? Add an action to Voicebutton in Viewdidload ():
Voicebutton.addtarget (Self, Action: "Voice", forControlEvents:.) Touchupinside)
? Add the Voice method:
Func Voice () {
If!audioplayer.playing {
Audioplayer.play ()
Voicebutton.setimage (UIImage (named: "[email protected]"), Forstate:. Normal)
If the background music does not play, so that it starts playing and displays the button as a play picture
}else{
Audioplayer.stop ()
Voicebutton.setimage (UIImage (named: "[email protected]"), Forstate:. Normal)
If the background music is playing, so that it stops playing and displays the button as a mute picture
}
}
The above operation can also be directly associated with the button method.
AB Page repeatedly jump, background music only call once
Under normal circumstances, if only the a page background music settings, jump to page B when the music does not stop to play normally, but return to a page will play a layer of music again, you can first Audioplayer is playing music to determine whether:
If!audioplayer.playing {
Audioplayer.play ()
}
If the audioplayer is not playing, it starts playing
a page jumps to the B page, the background music plays normally; b page jumps to c page, background music switch
Can be used to solve the value of the page, for different background music multi-page to the B page to jump, the music does not switch, but the B page to the C page to jump to the situation of music switching.
? In advance for A, B, C page to introduce Avfoundation framework, and add Avcaudioplayer on page A, b page add Bvcaudioplayer, C page Add Bvcaudioplayer and Cvcaudioplayer.
? On the A, B page viewdidload () Add an action to the jump button:
Turnbutton.addtarget (Self, Action: "Turn", forControlEvents:. Touchupinside)
? Add the Turn method to the a page:
Func Turn () {
var sb = Uistoryboard (name: "Main", Bundle:nil)
Use this method to jump using the storyboard authoring page
var BVC = sb.instantiateviewcontrollerwithidentifier ("B") as Bviewcontroller
Bvc.bvcaudioplayer = Self.avcaudioplayer
Page Pass Value
Self.presentviewcontroller (BVC, Animated:true, Completion:nil)
Page Jump
}
The B page jumps to the C page (CVC) method the same.
? On the C page to determine whether the B-page Bvcaudioplayer plays and invoke its own background music.
if (bvcaudioplayer! = nil) {
Bvcaudioplayer.stop ()
Playbgmusic ()
} else {
Playbgmusic ()
}
The Playbgmusic method needs to be defined in advance for the C page, and the player is Cvcaudioplayer
"swift Learning Notes" Use AV foundation framework to add background music to your app and control it with ease