2015-09-01 6 views
9

मैंने स्प्राइटकिट और स्विफ्ट का उपयोग करके एक्सकोड 7 बीटा पर एक गेम बनाया, मैंने ऑडियो डालने की कोशिश की लेकिन यह सकारात्मक नहीं है क्योंकि एक्सकोड में 3 त्रुटियां मिलीं, मैं शुरुआत कर रहा हूं और मुझे नहीं पता कि उन्हें कैसे ठीक किया जाए।स्विफ्ट 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() 
} 

कोड की त्रुटि यहां हैं:

कोड 1:

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

त्रुटि 1:

Extra argument 'error' in call

कोड 2:

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

त्रुटि 2:

Extra argument 'error' in call

कोड 3:

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

त्रुटि 3:

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

आपका योगदान मेरी मदद करेगा। धन्यवाद।

+0

संभव डुप्लिकेट [स्विफ्ट करते-कोशिश-पकड़ वाक्य रचना] (http://stackoverflow.com/questions/30720497/swift-do-try-catch-syntax) – nalply

उत्तर

6

स्विफ्ट 2.0 में हैंडलिंग त्रुटियां बदल गई हैं। फाउंडेशन और अन्य सिस्टम ढांचे अब नए प्रकार की त्रुटि हैंडलिंग का उपयोग करते हैं जो कोशिश और पकड़ तंत्र का उपयोग करता है।

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.

आप कार्यों की परिभाषा को देखें, तो आप आप देख सकते हैं कि वे अब फेंक का उपयोग करें। उदाहरण के लिए:

public func setActive(active: Bool) throws 

आप देख सकते हैं कि फ़ंक्शन फेंकता त्रुटि के बाद कोई त्रुटि पैरामीटर नहीं है। यह एनएसईआरआरआर हैंडलिंग दर्द को बहुत कम करता है और इससे आपको लिखने के लिए आवश्यक कोड की मात्रा भी कम हो जाती है।

तो जहां भी आप फ़ंक्शन में अंतिम पैरामीटर के रूप में त्रुटि देखते हैं, इसे हटाएं और प्रयास करें! इसके सामने।

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

के बाद से त्रुटि हैंडलिंग इस सवाल के दायरे में नहीं है, तो आप इसे here के बारे में अधिक पढ़ सकते हैं: एक उदाहरण यह है।

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

do catch अंदर कार्यों का प्रयोग करें और फेंकने वालों के लिए try का उपयोग करें::

इस कोड को अपने द्वारा लिखा गया के संस्करण में सुधार किया जाए

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) 
    } 
} 

पूरा स्विफ्ट 2 त्रुटि के लिए this answer देखें स्पष्टीकरण संभालना

+0

यह काम कर रहा है लेकिन आप से पता चला नहीं था ऑडियो को हर समय स्वयं को फिर से चलाने के लिए कैसे करें। –

+1

मुझे अपने आप से समाधान मिला। और दोहराने के लिए बस जोड़ने की आवश्यकता है: audioPlayer.numberOfLoops = -1। आपके योगदान के लिए धन्यवाद। –

0

विचार AVAudioPlayer का विस्तार करना है और किसी भी वर्ग में कॉल करने के लिए एक विधि का उपयोग करना है।

1) 'AppExtensions' नामक खाली स्विफ्ट क्लास बनाएं। निम्नलिखित जोड़ें।

// 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) किसी भी कक्षा में विस्तार विधि को कॉल करें।

var audioPlayer1 = AVAudioPlayer() 
audioPlayer1.playMusic(audioFileName:"tik",audioFileType:"wav") 
की
संबंधित मुद्दे