2015-04-05 76 views
0

我跟隨udacity的課程嘗試學習如何使用xcode和swift製作iPhone應用。當我點擊應用程序中的按鈕時,我正在播放音頻文件的部分。每次點擊它時,我都會收到以下錯誤消息:嘗試播放iPhone應用中的音頻時出錯

in recordAudio 
2015-04-05 14:42:18.124 Pitch Perfect[18544:1141273] -[Pitch_Perfect.PlaySoundsViewController slowAudio:]: unrecognized selector sent to instance 0x7f8174001760 
2015-04-05 14:42:18.135 Pitch Perfect[18544:1141273] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Pitch_Perfect.PlaySoundsViewController slowAudio:]: unrecognized selector sent to instance 0x7f8174001760' 
*** First throw call stack: 
(
    0 CoreFoundation      0x000000010ab57a75 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x000000010c6afbb7 objc_exception_throw + 45 
    2 CoreFoundation      0x000000010ab5ed1d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 
    3 CoreFoundation      0x000000010aab69dc ___forwarding___ + 988 
    4 CoreFoundation      0x000000010aab6578 _CF_forwarding_prep_0 + 120 
    5 UIKit        0x000000010b3eba22 -[UIApplication sendAction:to:from:forEvent:] + 75 
    6 UIKit        0x000000010b4f2e50 -[UIControl _sendActionsForEvents:withEvent:] + 467 
    7 UIKit        0x000000010b4f221f -[UIControl touchesEnded:withEvent:] + 522 
    8 UIKit        0x000000010b431b68 -[UIWindow _sendTouchesForEvent:] + 735 
    9 UIKit        0x000000010b432493 -[UIWindow sendEvent:] + 683 
    10 UIKit        0x000000010b3fefb1 -[UIApplication sendEvent:] + 246 
    11 UIKit        0x000000010b40c227 _UIApplicationHandleEventFromQueueEvent + 17700 
    12 UIKit        0x000000010b3e723c _UIApplicationHandleEventQueue + 2066 
    13 CoreFoundation      0x000000010aa8cc91 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
    14 CoreFoundation      0x000000010aa82b5d __CFRunLoopDoSources0 + 269 
    15 CoreFoundation      0x000000010aa82194 __CFRunLoopRun + 868 
    16 CoreFoundation      0x000000010aa81bc6 CFRunLoopRunSpecific + 470 
    17 GraphicsServices     0x000000010eb2da58 GSEventRunModal + 161 
    18 UIKit        0x000000010b3ea580 UIApplicationMain + 1282 
    19 Pitch Perfect      0x000000010a61d42e top_level_code + 78 
    20 Pitch Perfect      0x000000010a61d46a main + 42 
    21 libdyld.dylib      0x000000010ce8b145 start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 

這裏是我必須播放音頻的代碼。我似乎無法弄清楚什麼是錯的。我一直在嚴格遵循所有步驟。

// PlaySoundsViewController.swift 

// Pitch Perfect 

import UIKit 

import AVFoundation 

class PlaySoundsViewController: UIViewController { 

    var audioPlayer:AVAudioPlayer! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view. 
     if var filePath = NSBundle.mainBundle().pathForResource("movie_quote",ofType: "mp3"){ 
      var filePathUrl = NSURL.fileURLWithPath(filePath) 
      audioPlayer = AVAudioPlayer(contentsOfURL: filePathUrl, error: nil) 

     } else{ 
      println("the filepath is empty") 
     } 
    } 

    @IBAction func playSlowAudio(sender: UIButton) { 
     // Play audio slowly 
     audioPlayer.play() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

} 

有人可以幫我找到問題嗎?我對應用程序開發非常陌生。

回答

1

看來你有一個叫slowAudio的方法:某處,但是沒有在你的控制器中實現該方法;也許連接到IB的按鈕?右鍵單擊故事板(或xib)中的按鈕以調出黑色窗口,查看是否存在與名爲slowAudio的方法的連接:如果有,通過點擊小「x」刪除連接。

+1

工作。非常感謝你。請問你如何確定這是問題所在? – 2015-04-05 19:44:59

+0

@ J-Munney錯誤消息的開頭說: - [Pitch_Perfect.PlaySoundsViewController slowAudio:]。這意味着您正試圖在Pitch_Perfect應用程序的PlaySoundsViewController上調用slowAudio方法。錯誤消息的形式就像在objective-c,[object message]中的方法調用一樣。由於您沒有在您的控制器中實施該方法,故事板中出現錯誤的明顯選擇是。 – rdelmar 2015-04-05 20:10:03

相關問題