gpt4 book ai didi

ios - AVFoundation - 音乐 iOS

转载 作者:行者123 更新时间:2023-11-28 06:28:37 27 4
gpt4 key购买 nike

我一直在尝试将音乐添加到我的应用程序中,但没有特别成功。我一直在尝试使用 AVFoundation,我的代码如下:

    //MUSIC

var audioPlayer = AVAudioPlayer()
let audioPath = Bundle.main.path(forResource: "happyDays", ofType: "wav")


do {

try audioPlayer = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!))

}
catch {
// process error

}

audioPlayer.play()

我没有收到任何错误,但我的应用程序启动并且没有播放音乐。谁能帮忙?

最佳答案

swift 3

import UIKit
import AVFoundation

class ViewController: UIViewController{

let var avPlayer: AVAudioPlayer!

@IBAction func btnPlayPauseAudio(_ sender: UIButton){

if self.avPlayer != nil{

if let myAudioUrl = Bundle.main.url(forResource: "myAudioFile", withExtension: "mp3"){

if self.avPlayer.isPlaying{

self.avPlayer.pause()
}
else{

self.avPlayer = try AVAudioPlayer(contentsOf: myAudioUrl)

self.avPlayer.prepareToPlay()

self.avPlayer.play()
}
}
}
}
}

关于ios - AVFoundation - 音乐 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41048368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com