0
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()
}
}
}
私はこれをどのように修正すればよいのですか? あなたの助けに感謝!!!!!
:
は、以下のコードを検索します。だから、 'audioPlayer1'の目的は何ですか(添字の付いた変数名は混乱するかもしれません)? – vadian