13.4 C
Canberra
Monday, October 27, 2025

ios – use AVAudioPlayer to play music recordsdata from Storage?


In my undertaking it really works very effectively to play all of the sounds I want for a Meditation Timer. Now I need to let the customers select music from their iPhone storage to then play the music when the timer is working.

I constructed the timer simply utilizing a music file that I dragged into my undertaking and it really works nice as at all times.
Then I attempted placing the URL from the storage file, that the consumer selected. However nothing is enjoying. The timer runs with none drawback and there are not any errors thrown. Is it even attainable to make use of AVAudioPlayer to play media exterior to the undertaking? I noticed some examples the place individuals used it to play media from a Internet URL. So I guessed it ought to work.

Listed here are the required code snippets and likewise a screenshot of the File URL that’s chosen after which saved by the consumer.

import SwiftUI
import SwiftData
import AVFoundation     /// For taking part in any sound
import AVKit            /// For the AirPlay Button
import MediaPlayer      /// For getting the Quantity Slider connected to the units quantity.
import CoreHaptics      /// For making vibrations as soon as the timer is run out

struct TimerTapasMusicView: View {
    @Setting(.dismiss) var dismiss     /// To have the ability to dismiss the view
    
    @State var countdownTimer = 5 * 60      /// The precise seconds of the timer, being counted down/up
    @State var timerRunning = true         /// Var to set and see if timer is working
    @State var gongSound: AVAudioPlayer?    /// The Sound of the timer
    **@State var musicSound: AVAudioPlayer?    /// The Sound of the music**
    
    @State non-public var hours: Int = 0       /// Vars for the picker wheel
    @State non-public var minutes: Int = 0
    @State non-public var seconds: Int = 0
    
    @State non-public var brightnessLow = false/// Var for saving if the brightness button was used
    
    @State non-public var engine: CHHapticEngine?  /// The thing creating the vibrations afterward
    @Setting(.scenePhase) var scenePhase   /// Var to maintain observe of state of app, to restart HapticEngine
    
    /// Setting Objects
    @EnvironmentObject var userStat: StatisticsObject       /// Var to maintain observe of seconds utilizing the Timer
    @EnvironmentObject var envObject: EnvObject             /// For getting FullDarkness, vibrateTimer
    
    /// Getting the Asana
    @State var asana: TapasAsana

    **let path = Bundle.predominant.path(forResource: "bell_basu.mp3", ofType:nil)!
    let musicPath = Bundle.predominant.path(forResource: "authen.mp3", ofType:nil)!
    let volumeView = MPVolumeView()**
    
    
    
    var physique: some View {
        /// For calculating hours, minutes, seconds
        let fashion = Length.TimeFormatStyle(sample: .minuteSecond)
        //let formTimer = Length.seconds(countdownTimer).formatted()
        let formTimerShort = Length.seconds(countdownTimer).formatted(fashion)
        **let url = URL(fileURLWithPath: path)
        let musicUrl = asana.musicFile //URL(fileURLWithPath: musicPath)**
        
        VStack {
            /// The slider for the system quantity
            HStack{
                VolumeSliderView()
                    .body(width: 250, top: 25)
                    .offset(y: 5)
                
                RouteButtonView()
                    .body(width: 50, top: 50)
            }
            .padding(.high, 40)
            
            /// The buttons when the timer is working
            TimerTopButtonsView()
            
            Textual content("(asana.identify)")
                .font(.system(dimension: 50))
                .padding(.backside, -100)
            
            /// The principle Timer If there are not any hours we disguise the hour quantity and make the timer larger on this approach.
            Textual content("(formTimerShort)")
                .onAppear(carry out: prepareHaptics) /// Making ready the Vibration Object
                .onChange(of: scenePhase) {  /// When app will get closed and reopnened, begin Haptic once more
                    prepareHaptics()
                }
                .onReceive(Timer.publish(each: 1, on: .predominant, in: .widespread).autoconnect()) { _ in
                    if countdownTimer > 0 && timerRunning {
                        countdownTimer -= 1
                    }
                    else if countdownTimer == 5 {
                        gongSound?.prepareToPlay()
                    } else {
                        timerRunning = false
                    }
                    /// Performs the gong sound when timer hits 0 after working
                    if countdownTimer == 0 && timerRunning {
                        do {
                            gongSound = attempt AVAudioPlayer(contentsOf: url)
                            gongSound?.play()
                            envObject.fullDarkness = false
                            timerRunning = false
                            timerVibration()
                            
                            /// Including the seconds of lastTime to the TimerUsed stat
                            userStat.timerDuration += asana.length
                            UserDefaults.customary.set(userStat.timerDuration, forKey: "TimerDuration")
                            /// When the Timer is completed we have now achieved the Asana and the Timer is dismissed.
                            asana.doneToday = true
                            asana.recoupCount = 0
                        } catch {
                            //Could not Load File.
                        }
                    }
                }
                .body(width: 500, top: 220)
            /// Change the scale relying if it's only minutes after which solely when timer working
                .font(.system(dimension: 140))
                .monospacedDigit()                      /// Makes the numbers fastened in dimension and place.
                .onTapGesture { gongSound?.cease() }     /// Tapping it stops the sound.
                .padding(.high, 30)
                .padding(.backside, 30)
            /// Prevents machine from sleep on seem and permits sleep on disappear
                .onAppear{
                    UIApplication.shared.isIdleTimerDisabled = true
                    if (asana.recoupCount == 0 ) { countdownTimer = asana.length }
                    else { countdownTimer = asana.length * (asana.recoupCount + 1) }
                    
                    **do {
                        musicSound = attempt AVAudioPlayer(contentsOf: musicUrl)
                        musicSound?.play()
                        musicSound?.numberOfLoops = 10000**
                    } catch {
                        
                    }
                }
                .onDisappear{UIApplication.shared.isIdleTimerDisabled = false}
                .toolbar(.hidden, for: .tabBar)
               .... And extra Code that I feel will not be essential right here.

Right here I’m letting the consumer select a file from their Storage:

} else if asana.executionType == "music" {
                Button("Select Music File") {
                    chooseMusic = true
                }
                .fileImporter(isPresented: $chooseMusic, allowedContentTypes: [.item]) {end in
                    
                    swap end result {
                        case .success(let Fileurl):
                            print(Fileurl)
                            asana.musicFile = Fileurl
                        case .failure(let error):
                            print(error)
                    }
                }
                
                Textual content("File Title: (asana.musicFile)")

And this can be a Screenshot of the URL, possibly I’m saving it in a incorrect approach.

I edited the code to incorporate the Safety Scope in keeping with the instance within the documentation, however nonetheless there is no such thing as a sound enjoying.

.fileImporter(isPresented: $chooseMusic, allowedContentTypes: [.item]) {end in
                    
                    swap end result {
                        case .success(let Fileurl):
                            let gotAccess = Fileurl.startAccessingSecurityScopedResource()
                            print(Fileurl)
                            asana.musicFile = Fileurl
                            Fileurl.stopAccessingSecurityScopedResource()
                        case .failure(let error):
                            print(error)
                    }
                }

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles