TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Help, trying to loop an MP3 file with AVPlayer

Forums > SwiftUI

I'm working on an app to play multiple mp3 files simultaneously - I'm kind of there but I can't get the audio to loop. I really didn't think this wouuld be the hardest part of the code, does anyone have any pointers? Everything I try just crashes or fails.

Here is where I am (this took me so long to just get this far!)

import SwiftUI
import AVFoundation
import AVKit

struct ContentView: View {
    // Declare  audio players
    let player1 = AVPlayer()

    // Load the MP3 files into the players
    func loadFiles() {
        // Declare path to audio file - duplicate for each file
        let file1 = Bundle.main.path(forResource: "heavyrain", ofType: "mp3") ?? ""
        let file1URL = URL(fileURLWithPath: file1)
        let playerItem1 = AVPlayerItem(url: file1URL)
        player1.replaceCurrentItem(with: playerItem1)

    }

    // Declare variables to store the volume levels for each player
    @State private var player1Volume: Float = 1.0
    @State private var isPlaying = false

    var body: some View {
        VStack {
            // Create audio controller - duplicate for each player
            VStack {
                HStack {
                    Text("Heavy rain")
                    Spacer()
                    Button(action: {
                                       if self.isPlaying {
                                           self.player1.pause()
                                           self.isPlaying = false
                                       } else {
                                           self.player1.play()
                                           self.isPlaying = true
                                       }
                                   }) {
                                       if isPlaying {
                                           Image(systemName: "stop.fill")
                                       } else {
                                           Image(systemName: "play.fill")
                                       }
                                   }
                    //Slider to control the volume of player
                    Slider(value: $player1Volume, in: 0...1, step: 0.1) {
                        Text("Player 1 Volume")
                    }
                    .onReceive([self.player1Volume].publisher.first()) { value in
                        self.player1.volume = value
                    }
                }
            }
        }
        .padding()
        .onAppear(perform: loadFiles)
    }
}

1      

@Bnerd  

Have a look here: https://developer.apple.com/documentation/avfaudio/avaudioplayer/1386071-numberofloops

and accordingly I guess you should add to your code the below

func loadFiles() {
        // Declare path to audio file - duplicate for each file
        let file1 = Bundle.main.path(forResource: "heavyrain", ofType: "mp3") ?? ""
        let file1URL = URL(fileURLWithPath: file1)
        let playerItem1 = AVPlayerItem(url: file1URL)
        player1.replaceCurrentItem(with: playerItem1)
        player1.numberOfLoops = -1

    }

1      

Thank you, I changed from AVPlayer to AVAudioPlayer and now can use the numberOfLoops option. Weirdly AVPlayer doesnt have the loop ability.

1      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.