Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
Mientras mantengo la muñeca hacia abajo, ¿cómo sigo reproduciendo audio en la aplicación iWatch? - reloj OS

Estoy tratando de crear una aplicación de audio para Apple Watch. Pero el problema es que cada vez que mantengo las manos hacia abajo, el audio deja de reproducirse. También he activado el modo de fondo. ¿Puede alguien por favor ayudarme con esto? Estoy atascado en esta parte. Aquí está el código que he usado para reproducir audio.

 func play(url : URL) { do { if #available(watchOSApplicationExtension 4.0, *) { WKExtension.shared().isFrontmostTimeoutExtended = true } else { // Fallback on earlier versions } self.player = try AVAudioPlayer(contentsOf: url) player!.prepareToPlay() player?.delegate = self player?.play() print("-----------------") print("Playing Audio") print("*****************\nCurrent Time \(String(describing: self.player?.currentTime))") } catch let error as NSError { self.player = nil print(error.localizedDescription) } catch { print("*************************") print("AVAudioPlayer init failed") } }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Asegúrese de que está intentando jugar con Audio Data , no con Audio URL de audio y ha agregado policy: .longFormAudio en la configuración de su categoría. Según la documentación de Apple , estas dos configuraciones deben configurarse para que el audio se reproduzca en segundo plano.

 // Set up the session. let session = AVAudioSession.sharedInstance() do { try session.setCategory( .playback, mode: .default, policy: .longFormAudio ) } catch let error { fatalError("*** Unable to set up the audio session: \(error.localizedDescription) ***") } // Set up the player. let player: AVAudioPlayer do { player = try AVAudioPlayer(data: audioData) } catch let error { print("*** Unable to set up the audio player: \(error.localizedDescription) ***") // Handle the error here. return } // Activate and request the route. session.activate(options: []) { (success, error) in guard error == nil else { print("*** An error occurred: \(error!.localizedDescription) ***") // Handle the error here. return } // Play the audio file. player.play() }

He probado este código y funciona solo con conectividad Bluetooth en la aplicación Watch, no en el altavoz del reloj.

over 4 years ago · Santiago Trujillo Report

0

Simplemente activar el modo de fondo no es suficiente. También necesita activar AVAudioSession .
Todo está bien documentado por Apple aquí: Reproducción de audio de fondo .

Configurar y activar la sesión de audio

Antes de poder reproducir audio, debe configurar y activar la sesión de audio.

 session.setCategory(AVAudioSession.Category.playback, mode: .default, policy: .longForm, options: [])

A continuación, active la sesión llamando al método activate(options:completionHandler:).

 session.activate(options: []) { (success, error) in // Check for an error and play audio. }

Referencia: https://developer.apple.com/documentation/watchkit/playing_background_audio


Ejemplo:

 var player: AVAudioPlayer? let session: AVAudioSession = .sharedInstance() func prepareSession() { do { try session.setCategory(AVAudioSession.Category.playback, mode: .default, policy: .longForm, options: []) } catch { print(error) } } func play(url: URL) { do { player = try AVAudioPlayer(contentsOf: url) } catch { print(error) return } session.activate(options: []) { (success, error) in guard error == nil else { print(error!) return } // Play the audio file self.player?.play() } }

Prueba sencilla:

 prepareSession() if let url = Bundle.main.url(forResource: "test", withExtension: "mp3") { play(url: url) } else { print("test.mp3 not found in project: put any mp3 file in and name it so") }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!