audioPlayer.isPlaying Statement上的线程1错误

时间:2017-06-16 00:08:38

标签: ios swift if-statement avaudioplayer

我在使用audioPlayer查看viewDidLoad时尝试执行if语句。基本上说如果正在播放audioPlayer,那么暂停它。像这样:

override func viewDidLoad()
    {
        super.viewDidLoad()

        if audioPlayer.isPlaying {
            audioPlayer.pause()
        }
}

但是,在第5行(启动if语句),我收到错误

  

线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x48)

我该如何解决这个问题?谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您无法在此处使用默认的NSObject初始化工具 - AVAudioPlayer不会覆盖它,因此它不会设置isPlaying属性(或任何其他属性)。您必须使用其实际实现的其中一个初始化器:

/* For all of these init calls, if a return value of nil is given you can check outError to see what the problem was.
 If not nil, then the object is usable for playing
*/

/* all data must be in the form of an audio file understood by CoreAudio */
public init(contentsOf url: URL) throws

public init(data: Data) throws


/* The file type hint is a constant defined in AVMediaFormat.h whose value is a UTI for a file format. e.g. AVFileTypeAIFF. */
/* Sometimes the type of a file cannot be determined from the data, or it is actually corrupt. The file type hint tells the parser what kind of data to look for so that files which are not self identifying or possibly even corrupt can be successfully parsed. */
@available(iOS 7.0, *)
public init(contentsOf url: URL, fileTypeHint utiString: String?) throws

@available(iOS 7.0, *)
public init(data: Data, fileTypeHint utiString: String?) throws