ios - 尝试在 iPhone 应用程序中播放音频时出错

标签 ios xcode swift

我正在学习 udacity 的类(class),尝试学习如何使用 xcode 和 swift 制作 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.
    }
    */

}

有人可以帮我找到问题吗?我是应用程序开发的新手。

最佳答案

您似乎在某处有一个名为 slowAudio: 的方法,但尚未在您的 Controller 中实现该方法;也许连接到 IB 中的按钮?右键单击 Storyboard(或 xib)中的按钮以调出黑色窗口以查看是否存在与名为 slowAudio: 的方法的连接。如果有,请单击小“x”删除连接。

关于ios - 尝试在 iPhone 应用程序中播放音频时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29461076/

相关文章:

xcode - 预期标识符或 '(' - xCode

iOS NSUserDefaults 加载缓慢

ios - 使用CloudKit共享时如何正确使用数据库订阅

swift - 具有动态和固定单元格的 TableView Swift

ios - ios项目结构是否有任何最佳实践或约定

swift - ios Swift 中 Paytm 支付缺失参数问题

ios - 添加为另一个 View 的 subview 时未调用 touchesBegan 方法

swift - 使用 Swift 在 Spritekit 中暂停 SKAction

c# - Xamarin.Forms - slider 不更新 iOS 中的属性分配,适用于 Android

ios - 将按钮添加到 uitableviewcell 并没有出现