2017-09-05 10 views
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() 
} 
} 
} 

私はこれをどのように修正すればよいのですか? あなたの助けに感謝!!!!!

+0

audioPlayer = audioPlayers[sender.tag] 

は、以下のコードを検索します。だから、 'audioPlayer1'の目的は何ですか(添字の付いた変数名は混乱するかもしれません)? – vadian

答えて

0

線の下に確認してください。

let audioPlayer = audioPlayers[sender.tag] 

それは以下のようにする必要があります。 AudioPlayerを既に初期化しているためです。あなたは `デフォルトの初期化子を持つ` audioPlayer1`を `()作成されますが、再生する一切` data`も `contentsOfURL`がない

@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() 
    } 
} 
関連する問題