57

Fade In and Fade Out Music iOS Tutorial

 5 years ago
source link: https://www.tuicool.com/articles/hit/Fr2UjaF
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The AVAudioPlayer class can be used to Fade in and Out Music. In this tutorial a music track will be played, and the user can fade the music in or out using a switch, This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

zIJJvuZ.png!web

For product name, use IOSFadeMusicTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

ZNzIFvv.png!web

For this project we need a music to play.Download the music file. Open the Assets Catalog and drag the file inside te catalog.

Go to the Storyboard and drag  a Label, a Switch and another Label to the top of the main View, positioning them next to each other. Select the left Label and give it a title of "Fade out". Select the right Label and give it a title of "Fade in". Hold the Ctrl key and select all three items. Go to the bottom-right of the Storyboard, click the "Embed in " button and select Stack View.

Y7rqEz3.png!web

Select the Stack View and go to the Attributes inspector. In the Stack View section change the Distribution field to "Equal Spacing".

ZZvMR3E.png!web

go to the bottom-right of the Storyboard and select the Resolve Auto-Layout button and select Reset to suggested Constraints.

eqIJfuJ.png!web

The Storyboard will look like this.

beArEjI.png!web

Select the Assistant Editor and make sure the ViewController.swift is visible next to the Storyboard. Ctrl-drag or right-click-drag from the Button to the class and create the following Action.

YRnU3en.png!web

Go to the ViewController.swift file and import the AVFoundation framework.

import AVFoundation

Add the following property

var player: AVAudioPlayer!

Change the viewDidLoad method to 

override func viewDidLoad() {
    super.viewDidLoad()
        
    guard let asset = NSDataAsset(name: "com-truise-cyanide-sisters") else { print("Error Loading Audio."); return }
        
    do {
        player = try AVAudioPlayer(data: asset.data)
    } catch { print("Error Playing Audio."); return }
          
    player.volume = 0
    player.numberOfLoops = -1
    player.play()   
}

The music track will be played continuously by giving a value of -1  of the numberOfLoops property. The volume of the music track wil be silent at start.

Next, implement the fadeSwitchChanged(_:) action method

@IBAction func fadeSwitchChanged(_ sender: UISwitch) {
    if sender.isOn {
            
        // Fading in
        player.setVolume(1, fadeDuration: 2)
    } else {
            
        // Fading out
        player.setVolume(0, fadeDuration: 2)
    }
}

The start value of the switch will be On by default. In this case it is better to change the start value to Off, because the first switch change will be a fade in action. Go to the Storyboard, select the Switch and go to the Property inspector. Go to the Switch section and change the value to Off.

BfmmMnm.png!web

Build and Runthe project, and change the switch to fade in or fade out the music

fyUz2uQ.png!web

You can download the source code of the IOSFadeMusicTutorial at the ioscreator repository on github .


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK