2015-09-01 14 views
9

Tôi đã thực hiện trò chơi trên Xcode 7 beta bằng SpriteKit và Swift, tôi đã cố gắng đưa Audio vào nó nhưng nó không thể xác định được vì Xcode tìm thấy 3 lỗi, tôi mới bắt đầu và tôi không biết cách sửa chúng.Cách sử dụng ứng dụng âm thanh trong iOS với Swift 2?

import AVFoundation 

    var audioPlayer = AVAudioPlayer() 


    func playAudio() { 
     // Set the sound file name & extension 
     var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Flip 02", ofType: "wav")!) 

     // Preperation 
     AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil) 
     AVAudioSession.sharedInstance().setActive(true, error: nil) 

     // Play the sound 
     var error: NSError? 
     audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error) 
     audioPlayer.prepareToPlay() 
     audioPlayer.play() 
} 

Lỗi mã đang ở đây:

Mã 1:

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil) 

Lỗi 1:

Extra argument 'error' in call

Mã 2:

AVAudioSession.sharedInstance().setActive(true, error: nil) 

Lỗi 2:

Extra argument 'error' in call

Mã 3:

audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error) 

Lỗi 3:

Cannot find an initializer for type 'AVAudioPlayer' that accepts an argument list of type '(contentsOfURL: NSURL, error: inout NSError?)'

Đóng góp của bạn có thể giúp tôi. Cảm ơn.

+0

thể trùng lặp của [Swift làm-try-catch cú pháp] (http://stackoverflow.com/questions/30720497/swift-do-try-catch-syntax) – nalply

Trả lời

6

Lỗi xử lý trong Swift 2.0 đã thay đổi. Nền tảng và các khung hệ thống khác hiện nay sử dụng loại xử lý lỗi mới sử dụng cơ chế try và catch.

In Cocoa, methods that produce errors take an NSError pointer parameter last parameter, which populates its argument with an NSError object if an error occurs. Swift automatically translates Objective-C methods that produce errors into methods that throw an error according to Swift’s native error handling functionality.

Nếu bạn xem định nghĩa các hàm bạn sử dụng, bạn có thể thấy chúng hiện đang ném. Ví dụ:

public func setActive(active: Bool) throws 

Bạn có thể thấy rằng không có thông số lỗi do chức năng phát ra lỗi. Điều này làm giảm đáng kể sự đau đớn xử lý NSError và nó cũng làm giảm số lượng mã bạn cần phải viết.

Vì vậy, bất cứ nơi nào bạn thấy lỗi làm thông số cuối cùng trong hàm, hãy xóa nó và viết thử! trước mặt nó. Một ví dụ là:

try! AVAudioSession.sharedInstance().setActive(true) 

Vì xử lý lỗi không phải là phạm vi của câu hỏi này, bạn có thể đọc thêm về nó here.

này được khắc phục phiên bản mã bạn đã viết:

import AVFoundation 

var audioPlayer = AVAudioPlayer() 

func playAudio() { 
    // Set the sound file name & extension 
    let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Flip 02", ofType: "wav")!) 

    // Preperation 
    try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: []) 
    try! AVAudioSession.sharedInstance().setActive(true) 

    // Play the sound 
    do { 
     try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound) 
     audioPlayer.prepareToPlay() 
     audioPlayer.play() 
    } catch { 
     print("there is \(error)") 
    } 
} 
24

Sử dụng các chức năng bên trong do catch và sử dụng try cho những người ném:

var audioPlayer = AVAudioPlayer() 

func playAudio() { 
    do { 
     if let bundle = NSBundle.mainBundle().pathForResource("Flip 02", ofType: "wav") { 
      let alertSound = NSURL(fileURLWithPath: bundle) 
      try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 
      try AVAudioSession.sharedInstance().setActive(true) 
      try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound) 
      audioPlayer.prepareToPlay() 
      audioPlayer.play() 
     } 
    } catch { 
     print(error) 
    } 
} 

Xem this answer cho hoàn chỉnh lỗi Swift 2 xử lý các giải thích.

+0

Đó là làm việc nhưng bạn đã không cho thấy làm thế nào để làm cho âm thanh để phát lại bởi nó tự tất cả các thời gian. –

+1

Tôi tự tìm ra giải pháp. Và để lặp lại, chỉ cần thêm: audioPlayer.numberOfLoops = -1. Cảm ơn sự đóng góp của bạn. –

0

Ý tưởng là tạo phần mở rộng của AVAudioPlayer và sử dụng một phương thức để gọi trong bất kỳ lớp nào.

1) Tạo lớp trống nhanh có tên 'AppExtensions'. Thêm những điều sau.

// AppExtensions.swift 

    import Foundation 
    import UIKit 
    import SpriteKit 
    import AVFoundation 

    //Audio 
    var audioPlayer = AVAudioPlayer() 

    extension AVAudioPlayer { 

     func playMusic(audioFileName:String,audioFileType:String) 
     {  
      do { 

      let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(audioFileName, ofType: audioFileType)!) 

      // Removed deprecated use of AVAudioSessionDelegate protocol 
      try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 
      try AVAudioSession.sharedInstance().setActive(true) 
      try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound) 
      audioPlayer.prepareToPlay() 
      audioPlayer.play() 

      } 
      catch { 
       print(error) 
      } 
    } 
    } 

2) Gọi phương thức mở rộng trong bất kỳ lớp nào.

var audioPlayer1 = AVAudioPlayer() 
audioPlayer1.playMusic(audioFileName:"tik",audioFileType:"wav") 
Các vấn đề liên quan