본문 바로가기
도서감상 및 오탈자/만들면서 배우는 Swift

[코드 오류] p22. [코드 3-3] 수정이 필요합니다.

by 푸랭키 2015. 9. 19.


Swift version up으로 인하여 AVAudioPlayer가 책과 같이 사용을 할 수 없습니다. 


AVAudioPlayer의 경우 do-catch 로 정리를 해야 되므로 아래와 같이 수정이 필요합니다.


ref : http://stackoverflow.com/questions/30786877/avaudioplayer-no-longer-working-in-swift-2-0-xcode-7-beta


=> Reported to 한빛 미디어


===================================================


    override func viewDidLoad()

    {

        // 번들에 위치한 사운드 파일의 주소를 가져온다.

  //      var bundleURL: NSURL? = NSBundle.mainBundle().URLForResource("sound", withExtension: "mp3")

        

        // 플레이어를 생성하고 초기화한다.

    

            do {

                try self.audioPlayer = AVAudioPlayer(contentsOfURL: NSURL (fileURLWithPath: NSBundle.mainBundle().pathForResource("sound", ofType: "mp3")!), fileTypeHint:nil)

            } catch {

                //Handle the error

            }

    

//        self.audioPlayer = AVAudioPlayer(contentsOfURL:bundleURL!, error:nil)

        

        // 플레이어의 델리게이트를 현재 ViewController 객체로 설정해준다.

        self.audioPlayer?.delegate = self

        

        // 진행 상황을 체크할 슬라이더의 초기값을 0으로 설정한다.

        self.progressSlider?.value = 0

        

        // 오버라이드했기 때문에 특별한 경우가 아니면 부모 클래스의 메소드 원형도 호출해 준다.

        super.viewDidLoad()

    }