IOS 9 Audio app play audio playback control pause stop forward back settings

Source: Internet
Author: User

IOS 9 Audio app play audio playback control pause stop forward back settings iOS9 audio app playback control

you can see that the Avaudioplayer class has a number of properties and methods in the "IOS 9 audio app plays audio iOS9 audio basic feature" article. This section explains in detail the properties and methods that are commonly used in the Avaudioplayer class.

iOS9 Audio app Pause/stop

There is a button in the music app that will stop the music from playing. When the user taps the button, the music being played stops. You can use the pause () method in the Avaudioplayer class and the Stop () method in iOS to stop the audio that you want to play.

1. Pause

The function of the pause () method is to pause the audio file that is playing, and the audio file is still ready to resume playback from the pause. Its grammatical form is as follows:

    • Func Pause ()

2. Stop

The function of the Stop () method is to stop the audio file that is playing, and to undo the playback settings that you want, that is, if the playback is resumed, the audio file needs to start playing again. Its grammatical form is as follows:

    • Func Stop ()

Note: the Stop () method is the same as the function of pause after Xcode 6.0, but the introduction to stop () in the Help document is still previous.

"Example 2-1" below will be based on the first instance, the ability to pause audio playback on the basis of playing audio. The code is as follows:

  • @IBAction func Playaudio (sender:anyobject) {
  • audioeffect?. Play () //Play Sound
  • Playbutton.setbackgroundimage (UIImage (named: "Pauseimage.png"), ForState:UIControlState.Normal)
  • Playbutton.addtarget (Self, Action: ("Pauseaudio"), ForControlEvents:UIControlEvents.TouchUpInside)
  • }
  • Func Pauseaudio () {
  • audioeffect?. Pause ()//Pause
  • Playbutton.setbackgroundimage (UIImage (named: "Playimage.png"), ForState:UIControlState.Normal)
  • Playbutton.addtarget (Self, Action: ("Playaudio:"), ForControlEvents:UIControlEvents.TouchUpInside)
  • }

Run the program at this point, tap the play button in the emulator interface that appears, the audio file will play, and the play button will change to a pause button, and when the developer taps the pause button, the audio file will be paused and the pause button will become the original play button.

Note: Because the stop control does not appear in most music players, the code is not implemented in this example.

iOS9 Audio app forward/rewind

There is a CurrentTime property in the Avaudioplayer class. This property can be used to set the duration of the currently playing audio file, that is, the playback position of the currently playing audio file. The value of this property is in seconds. Its grammatical form is as follows:

    • var currenttime:nstimeinterval

By setting the CurrentTime property, we can implement the forward and backward function in the music application.

1. Forward

The forward function is to have the audio file being played forward for a few seconds to play. Its implementation is to let the value of the CurrentTime property set to a fixed addition operation.

"Example 2-2 " The following will be based on the first instance to achieve the forward functionality." The following steps are described:

(1) Associate the Forward button buttons in the main view with the action Forwardaudio.

(2) Open the Viewcontroller.swift file, write the code, realize the forward function. The code is as follows:

  • @IBAction func Forwardaudio (sender:anyobject) {
  • Determine if the audio file is playing
  • if (audioeffect!. Playing) {
  • Audio files are playing, enabling progress
  • Let desiredtime= (Self.audioeffect?). currenttime)!+20.0
  • //Determine if the time set is less than the total time of the audio file
  • if (Desiredtime<self.audioeffect?. Duration) {
  • self.audioeffect?. Currenttime=desiredtime //Set the current playback time
  • }
  • }else{
  • Let Alertcontroller = Uialertcontroller (title: "Hint", message: "Music does not start playing", PreferredStyle:UIAlertControllerStyle.Alert )
  • Let action = uialertaction (title: "Yes", Style:UIAlertActionStyle.Default, Handler:nil)
  • Alertcontroller.addaction (Action)
  • Self.presentviewcontroller (Alertcontroller, Animated:true, Completion:nil)
  • }
  • }

After running the program, tap the play button in the emulator interface, and the audio file will start playing, and when the developer taps the forward button, the audio file will fast forward for 20 seconds and then play. (The progress of 20 seconds will be fast for each tap).

Note: If the developer does not tap the play button, but instead taps the forward button directly, the "music does not start playing" Warning view is displayed, as shown in 2.25.

Figure 2.25 Warning View

2. Back

The fallback function is to have the audio file being played back for a few seconds to play. Its implementation is to let the value of the CurrentTime property set a fixed subtraction operation.

"Example 2-3 " The following will be based on the first instance, to achieve the function of back." The following steps are described:

(1) Associate the Backward button buttons in the main view with the action Backwardaudio.

(2) Open the Viewcontroller.swift file, write the code, and implement the fallback function. The code is as follows:

  • @IBAction func Backwardaudio (sender:anyobject) {
  • if (audioeffect!. Playing) {
  • Achieve back
  • Let desiredtime= (Self.audioeffect?). currenttime)!-20.0
  • Determines whether the specified time is 0
  • if (desiredtime<0) {
  • Self.audioeffect?. currenttime=0.0
  • }else{
  • self.audioeffect?. Currenttime=desiredtime
  • }
  • }else{
  • Let Alertcontroller = Uialertcontroller (title: "Hint", message: "Music does not start playing", PreferredStyle:UIAlertControllerStyle.Alert )
  • Let action = uialertaction (title: "Yes", Style:UIAlertActionStyle.Default, Handler:nil)
  • Alertcontroller.addaction (Action)
  • Self.presentviewcontroller (Alertcontroller, Animated:true, Completion:nil)
  • }
  • }
  • }

After running the program, tap the play button in the emulator interface and the audio file will start playing. When the developer taps the back button, the audio file is backed up for 20 seconds and then played back. (The progress of 20 seconds will be backed up every time you pat).

Note : If the developer does not tap the play button, but instead taps the back button directly, the "music does not start playing" alert view pops up.

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 playback control pause stop forward back settings

Related Article

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.