Как зациклить аудио в приложении[РЕШЕНО]


#1

Ребята, как сделать чтобы звук зацикливался, повторялся при окончании ?
И втрой вопрос: Как сделать чтобы эта мелодия при передвижении ползунка в зависимости от его положения играла тише / громче ?

import UIKit
import AVFoundation
import AudioToolbox

class ViewController: UIViewController {

@IBOutlet weak var StartButton: UIButton!


@IBOutlet weak var StopButton: UIButton!

@IBOutlet weak var Slider: UISlider!





let popSound = Bundle.main.url(forResource: "401275__inspectorj__rain-moderate-c", withExtension: "wav")
var audioPlayer = AVAudioPlayer()

override func viewDidLoad() {

    
    Slider.value = 50
    Slider.maximumValue = 100
    Slider.minimumValue = 0
    Slider.layer.cornerRadius = 10
    
    
    StartButton.backgroundColor = .black
    StartButton.layer.cornerRadius = 10
    StartButton.frame = CGRect(x: 45, y: 90, width: 100 , height: 100)
    StartButton.layer.cornerRadius = 10
    StartButton.setTitleColor(#colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), for: .normal)

   
    StopButton.backgroundColor = .blue
    StopButton.frame = CGRect(x: 240, y: 90, width: 100 , height: 100)
    StopButton.layer.cornerRadius = 10
    StopButton.setTitleColor(#colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), for: .normal)
}

@IBAction func playSound(_ sender: UIButton) {
    do {
        audioPlayer = try AVAudioPlayer(contentsOf: popSound!)
        audioPlayer.play()
    } catch {
        print("couldn't load sound file")
    }
    
}

@IBAction func StopPlay(_ sender: UIButton) {
    do {
        audioPlayer = try AVAudioPlayer(contentsOf: popSound!)
        audioPlayer.stop()
    } catch {
        print("couldn't load sound file")
    }
    
}


@IBAction func SliderMovie(_ sender: UISlider) {
    if Slider.value > 50 {
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: popSound!)
            audioPlayer.play()
        } catch {
            print("couldn't load sound file")
        }
        
    } else {
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: popSound!)
            audioPlayer.stop()
        } catch {
            print("couldn't load sound file")
        }
    }
    
}

}


#2

С громкостью разобрался


#3

У плеера есть свойство
musicPlayer.numberOfLoops = -1
Это количество повторений. При отрицательном значении циклично


#4

Спасибо! Все заработало.


#5

Наслаждайтесь! ___________


#6

Дмитрий, может быть еще у вас есть совет по этому вопросу?
https://forum.swiftbook.ru/t/mozhno-li-kak-to-pomenyat-shirinu-treka-na-v-uislider/7415/2


#7

Готового решения у меня нет.


#8

Разобрался. если у кого будут вопросы велкам !