0

Through music is playing perfectly but I am not able to execute my main programme.

import UIKit
import AVFoundation

print("Hello, Audio!") 

if let url = Bundle.main.url(forResource: "drum01", withExtension: "mp3") {
    do {
        let midi = try AVMIDIPlayer(contentsOf: url, soundBankURL: nil)
        midi.play(nil)
        while midi.isPlaying {
            // Spin (yeah, that's bad!)
        }
    } catch (let error) {
        print("AVMIDIPlayer failed: " + error.localizedDescription)
    }
}

Any help will be appreciated.

  • @AhmadF it does not works in playground it only works in terminal – Gurpreet Paul Mar 29 '17 at 11:38
  • Easy, you can't. The linked duplicate mentions that. – JAL Mar 29 '17 at 23:03
  • @JAL code of duplicate works only in terminal – Gurpreet Paul Mar 30 '17 at 05:35
  • @JAL #!/usr/bin/swift import Cocoa import AVFoundation var error: NSError? println("Hello, Audio!") var url = NSURL(fileURLWithPath: "/Users/somebody/myfile.mid") // Change to a local midi file var midi = AVMIDIPlayer(contentsOfURL: url, soundBankURL: nil, error: &error) if midi == nil { if let e = error { println("AVMIDIPlayer failed: " + e.localizedDescription) } } midi.play(nil) while midi.playing { // Spin (yeah, that's bad!) } **It does not works in playground !** – Gurpreet Paul Mar 30 '17 at 05:37
  • @EricAya Presently now i have come down with this code : import UIKit import AVFoundation print("Hello, Audio!") if let url = Bundle.main.url(forResource: "drum01", withExtension: "mp3") { do { let midi = try AVMIDIPlayer(contentsOf: url, soundBankURL: nil) midi.play(nil) while midi.isPlaying { // Spin (yeah, that's bad!) } } catch (let error) { print("AVMIDIPlayer failed: " + error.localizedDescription) } } – Gurpreet Paul Mar 30 '17 at 11:11
  • @EricAya the only problem i am facing with this code is that the music is working perfectly but main programme is not executing – Gurpreet Paul Mar 30 '17 at 11:13
  • @EricAya Can you please explain how this code can be written with the same functionality without Do-While loop? – Gurpreet Paul Mar 30 '17 at 11:25
  • @EricAya main content means apart from the background music other contents of my programme e.g display etc . Secondly I was doing this with my own code instead of using a library. Could you please give me a hint about how to do this please? :) – Gurpreet Paul Mar 30 '17 at 12:57

1 Answers1

-2

do you add "mySound" to your project?
And what type of "mySound" -> ofType: "mySound"
sample: ofType: "mp3"

Nguyen Hoan
  • 1,583
  • 1
  • 11
  • 18