2016-10-11 7 views
-2

ボタンが1つしかない単純なプログラムがあり、その唯一の機能はサウンド出力をトリガーすることです。Swift 3 - AVAudioPlayerがサウンドを再生していません

iOSシミュレータでテストするとアプリは完璧に動作しますが、iPhoneでテストすると、アプリはサウンドを再生していません。私は2つのiPhoneでこれをテストしました。

これは一度に動作していました。 iOS 10にアップデートすることで問題が発生したかどうかはわかりません。

//Code 
import UIKit 
import AVFoundation 

var audioPlayer = AVAudioPlayer() 


class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 


     let music = Bundle.main.path(forResource: "sound", ofType: "wav") 

     do { 
      audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: music!)) 
     } 
     catch{ 
      print(error) 
     } 

    } 


    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func playSound(_ sender: UIButton) { 

     audioPlayer.play() 
    } 

} 

はどんな答えを感謝し、次のようになります。

は、ここに私のコードです。おかげさまで

+1

たiPhoneのあなたの音量レベル(未着信音) – Alex

答えて

0
override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 


     let music = Bundle.main.path(forResource: "sound", ofType: "wav") 

     do { 
      audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: music!)) 
audioPlayer.delegate = self 
audioPlayer.preparedToPlay() 
     } 
     catch{ 
      print(error) 
     } 

    } 
0
import UIKit 
import AVFoundation 

class ViewController: UIViewController { 

var btnSound: AVAudioPlayer! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    //locate resource 
    let path = Bundle.main.path(forResource: "sound", ofType: "wav") 
    let music = URL(fileURLWithPath: path!) 

    do{ 
     try btnSound = AVAudioPlayer(contentsOf: music) 
     btnSound.prepareToPlay() 
    } catch let err as NSError{ 
     print(err.debugDescription) 
    } 
} 
} 

func playSound(){ 
    if btnSound.isPlaying{ 
     //stop playing sound file if already playing 
     btnSound.stop() 
    } 
    //play sound 
    btnSound.play() 
} 

@IBAction func playSound(sender: UIButton){ 
    //call playSound function on pressing the button you attach this function to 
    playSound() 
} 
+0

あなたのコードを教えてくださいを確認してください –

関連する問題