线程1:EXC坏访问0×48
问题描述:
here is the image of the error on Xcode线程1:EXC坏访问0×48
当我推说应该停止音频按钮,它只是停止整个应用程序和崩溃。随时给我一些帮助解决这个问题。 代码的这个地区是该按钮被迷上了,
@IBAction func stop(_ sender: UIButton) {
if audioPlayer1.isPlaying { //error is here on this line.
audioPlayer1.stop()
} else {
self.audioPlayer1.play()
}
这里是代码
//
// ViewController.swift
// app21
//
// Created by Jared Evan Miller on 8/14/17.
// Copyright © 2017 Jared Evan Miller. All rights reserved.
//
import UIKit
import AVFoundation
class ViewController: UIViewController {
let soundFilenames = ["5","8","7","4","6","1","3","2"]
var audioPlayers = [AVAudioPlayer]()
var lastAudioPlayer = 0
var audioPlayer = AVAudioPlayer()
var audioPlayer1 = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// set up audio players
for sound in soundFilenames{
do {
// Try to do somerhing
let url = URL(fileURLWithPath: Bundle.main.path(forResource: sound, ofType: "wav")!);
let audioPlayer = try AVAudioPlayer(contentsOf:url)
audioPlayers.append(audioPlayer)
}
catch {
// Catch the error that is thrown
audioPlayers.append(AVAudioPlayer())
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonTapped(_ sender: UIButton) {
// Get the audioPlayer that corresponds to the button that they tapped
let lastPlayer = audioPlayers[lastAudioPlayer]
lastPlayer.stop();
lastAudioPlayer = sender.tag;
lastPlayer.currentTime = 0;
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.currentTime = 0;
audioPlayer.play()
}
@IBAction func buttonTapped2(_ sender: UIButton) {
let lastPlayer = audioPlayers[lastAudioPlayer]
lastPlayer.stop();
lastAudioPlayer = sender.tag;
lastPlayer.currentTime = 0;
let audioPlayer = audioPlayers[sender.tag]
audioPlayer.currentTime = 0;
audioPlayer.play()
}
@IBAction func stop(_ sender: UIButton) {
if audioPlayer1.isPlaying { //error is here on this line.
audioPlayer1.stop()
} else {
self.audioPlayer1.play()
}
}
}
我该如何解决这个问题? 感谢您的帮助!!!!!
答
请检查下面一行:
let audioPlayer = audioPlayers[sender.tag]
应该像下面。因为你已经初始化了audioPlayer。
audioPlayer = audioPlayers[sender.tag]
找到下面的代码:
@IBAction func buttonTapped(_ sender: UIButton) {
// Get the audioPlayer that corresponds to the button that they tapped
let lastPlayer = audioPlayers[lastAudioPlayer]
lastPlayer.stop();
lastAudioPlayer = sender.tag;
lastPlayer.currentTime = 0;
audioPlayer = audioPlayers[sender.tag]
audioPlayer.currentTime = 0;
audioPlayer.play()
}
@IBAction func buttonTapped2(_ sender: UIButton) {
let lastPlayer = audioPlayers[lastAudioPlayer]
lastPlayer.stop();
lastAudioPlayer = sender.tag;
lastPlayer.currentTime = 0;
audioPlayer = audioPlayers[sender.tag]
audioPlayer.currentTime = 0;
audioPlayer.play()
}
@IBAction func stop(_ sender: UIButton) {
if audioPlayer.isPlaying { //Here you are trying on audioPlayer1 and you are not initialized this anywhere
audioPlayer.stop()
} else {
self.audioPlayer.play()
}
}
你正在创建使用默认初始化''audioPlayer1'()'但没有'data'也不'contentsOfURL'播放。那么'audioPlayer1'的目的是什么(带有索引号的变量名可能会让人困惑)? – vadian