线程 1:exc 访问错误 0 x 48
thread 1: exc bad access 0 x 48
here is the image of the error on Xcode
当我按下一个应该停止音频的按钮时,它只会停止整个应用程序并崩溃。请随时给我一些帮助来解决这个问题。
这部分代码是该按钮连接到的地方,
@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()
}
}
here is the image of the error on Xcode
当我按下一个应该停止音频的按钮时,它只会停止整个应用程序并崩溃。请随时给我一些帮助来解决这个问题。 这部分代码是该按钮连接到的地方,
@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()
}
}