2017-09-05 42 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

你正在創建使用默認初始化''audioPlayer1'()'但沒有'data'也不'contentsOfURL'播放。那麼'audioPlayer1'的目的是什麼(帶有索引號的變量名可能會讓人困惑)? – vadian

回答

0

請檢查下面一行:

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() 
    } 
}