IOS 9 Audio App play audio first iOS9 audio Instance 2

Source: Internet
Author: User

IOS 9 Audio App play audio first iOS9 audio Instance 2iOS9 Audio App Association

iOS9 Audio Apps for views or controls added on the main view, they must be associated with a socket variable when they are used. The IOS9 socket variable is actually an alias for a view or control in the main view, similar to an instantiated object. Associates the play button control in the main iOS9 view with the socket variable PlayButton. The following steps are described:

(1) Use the three view mode icon in the Settings editor, 2.14 to adjust the Xcode interface to the effect shown in 2.15.

Figure 2.14 An icon for the three view mode of the editor

Figure 2.15 Adjusting the interface

(2) Holding down the CTRL key while dragging the play button object in the main view, a blue line appears, dragging the blue line to the Viewcontroller.swift file, as shown in 2.16.

Figure 2.16 a blue line appears

(3) After releasing the mouse, a dialog box will pop up, showing 2.17.

Figure 2.17 Popup dialog box

(4) In the Name text box, type playbutton,2.18 as shown.

Figure 2.18 Entering a name

Note: The name of this entry is arbitrary.

(5) Select the Connect button to close the dialog box, and automatically generate a line of code in the Viewcontroller.swift file, as shown in 2.19.

Figure 2.19 Action Variables

Note: The generated code is called the socket variable.

Associates the more Settings view view in the main viewing with the socket variable Moresettingsview. Associate the Play button control in the main view with the action Playaudio (the action is actually a method, typically used in controls such as buttons, switches, sliders, and so on). The following steps are described:

(1) Use the icons in the three view mode of the settings editor to adjust the Xcode interface to the same effect as in Figure 2.15.

(2) Holding down the CTRL key while dragging the play button object in the main view, a blue line appears, dragging the blue line to the Viewcontroller.swift file, as shown in 2.20.

Figure 2.20 Ctrl + Drag the button object in the interface

(3) When the mouse is released, a dialog box is displayed that declares the associated socket variable. Set the connection option to action, which indicates that an action is associated, and the name is set to Playaudio, indicating that the associated action name is playaudio,2.21.

Figure 2.21 Filling out a dialog box

(4) Set the connection option to action, which indicates that an action is associated, and the name is set to Playaudio, indicating that the associated action name is playaudio,2.22.

Figure 2.22 Action

At this point, when the user taps the play button, a method called Playaudio () is triggered.

IOS 9 Audio App feature codes

The final part of the code implementation that plays the audio file is the one that needs to be covered in section 2.1. Open the Viewcontroller.swift file and write the code that implements the function of audio playback. The code is as follows:

  • Import UIKit
  • Import Avfoundation
  • Class Viewcontroller:uiviewcontroller {
  • @IBOutlet weak var playbutton:uibutton!
  • @IBOutlet weak var moresettingsview:uiview!
  • var Audioeffect:avaudioplayer?=nil
  • Override Func Viewdidload () {
  • Super.viewdidload ()
  • Additional setup after loading the view, typically from a nib.
  • Moresettingsview.hidden=true
  • Let Path=nsbundle.mainbundle (). Pathforresource ("Liekkas", OfType: "MP3")
  • Let Pathurl=nsurl (fileurlwithpath:path!)
  • Open an audio file
  • Do {
  • Audioeffect=try Avaudioplayer (Contentsofurl:pathurl)
  • } catch _ {
  • Audioeffect = Nil
  • }
  • audioeffect?. Preparetoplay () //Add audio files to the cache
  • }
  • @IBAction func Playaudio (sender:anyobject) {
  • No play
  • audioeffect?. Play () //Play Sound
  • Playbutton.setbackgroundimage (UIImage (named: "Pauseimage.png"), ForState:UIControlState.Normal)
  • }
  • Override Func didreceivememorywarning () {
  • Super.didreceivememorywarning ()
  • Dispose of any resources the can be recreated.
  • }
  • }

When you run the program, you see the effect shown in 2.23. When the developer taps the Start button, the audio file is played, and the Start button changes to the Pause button, as shown in 2.24.

Figure 2.23 Initial state figure 2.24 playing music

This article is selected from:IOS 9 Audio Application Development Basic Tutorial University PA Internal information, reproduced please indicate the source, respect the technology respect the IT person!

IOS 9 Audio App play audio first iOS9 audio Instance 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.