当我的 audioPlayer 的 viewDidLoad 时,我正在尝试执行 if 语句。基本上就是说,如果 audioPlayer 正在播放,则暂停它。像这样:
override func viewDidLoad()
{
super.viewDidLoad()
if audioPlayer.isPlaying {
audioPlayer.pause()
}
}
但是,在第 5 行 (开始 if 语句),我得到了错误
Thread 1: EXC_BAD_ACCESS (code=1, adress=0x48)
我该如何解决这个问题?谢谢您的帮助!
最佳答案
你不能在这里使用默认的 NSObject
初始值设定项 - AVAudioPlayer
不会覆盖它,所以它不会设置 isPlaying
属性(property)(或其任何其他属性(property))。您必须使用它实际实现的初始化器之一:
/* 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
关于ios - audioPlayer.isPlaying 语句上的线程 1 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44578842/