gpt4 book ai didi

ios - Xcode- swift ;为启动屏幕添加音效

转载 作者:可可西里 更新时间:2023-11-01 00:57:12 25 4
gpt4 key购买 nike

我很快就想询问一个小问题,我在尝试在我的 swift 应用程序的启动屏幕中播放音效时遇到了这个问题。对于我打算在哪里声明我的 AVAudio 播放器(在我的 View Controller 或 Appdelegate 中),我感到更加困惑;但是这两种方法都没有成功。这是我目前在我的 appDelegate 文件中得到的内容

import UIKit
import AVFoundation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var audioPlayer = AVAudioPlayer()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

self.window = UIWindow(frame: UIScreen.main.bounds)
self.window!.backgroundColor = UIColor(red: 9/255, green: 4/255, blue: 68/255, alpha: 1)
self.window!.makeKeyAndVisible()

// rootViewController from StoryBoard
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = mainStoryboard.instantiateViewController(withIdentifier: "navigationController")
self.window!.rootViewController = navigationController

// logo mask
navigationController.view.layer.mask = CALayer()
navigationController.view.layer.mask!.contents = UIImage(named: "logo.png")!.cgImage
navigationController.view.layer.mask!.bounds = CGRect(x: 0, y: 0, width: 60, height: 60)
navigationController.view.layer.mask!.anchorPoint = CGPoint(x: 0.5, y: 0.5)
navigationController.view.layer.mask!.position = CGPoint(x: navigationController.view.frame.width / 2, y: navigationController.view.frame.height / 2)

// logo mask background view
let maskBgView = UIView(frame: navigationController.view.frame)
maskBgView.backgroundColor = UIColor.white
navigationController.view.addSubview(maskBgView)
navigationController.view.bringSubview(toFront: maskBgView)

// logo mask animation
let transformAnimation = CAKeyframeAnimation(keyPath: "bounds")
transformAnimation.delegate = self as? CAAnimationDelegate
transformAnimation.duration = 1
transformAnimation.beginTime = CACurrentMediaTime() + 1 //add delay of 1 second
let initalBounds = NSValue(cgRect: (navigationController.view.layer.mask!.bounds))
let secondBounds = NSValue(cgRect: CGRect(x: 0, y: 0, width: 50, height: 50))
let finalBounds = NSValue(cgRect: CGRect(x: 0, y: 0, width: 2000, height: 2000))
transformAnimation.values = [initalBounds, secondBounds, finalBounds]
transformAnimation.keyTimes = [0, 0.5, 1]
transformAnimation.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut), CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)]
transformAnimation.isRemovedOnCompletion = false
transformAnimation.fillMode = kCAFillModeForwards
navigationController.view.layer.mask!.add(transformAnimation, forKey: "maskAnimation")

// logo mask background view animation
UIView.animate(withDuration: 0.1,
delay: 1.35,
options: UIViewAnimationOptions.curveEaseIn,
animations: {
maskBgView.alpha = 0.0
},
completion: { finished in
maskBgView.removeFromSuperview()
})

// root view animation
UIView.animate(withDuration: 0.25,
delay: 1.3,
options: UIViewAnimationOptions(),
animations: {
self.window!.rootViewController!.view.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
},
completion: { finished in
UIView.animate(withDuration: 0.3,
delay: 0.0,
options: UIViewAnimationOptions(),
animations: {
self.window!.rootViewController!.view.transform = CGAffineTransform.identity
},
completion: nil
)

do {
self.audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "startup", ofType: "wav")!))
self.audioPlayer.prepareToPlay()
}
catch {
print(error)
}
})

return true

}



func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

最佳答案

我更喜欢使用的方法是声明

var audioPlayer = AVAudioPlayer()

在 AppDelegate 中(就像您在代码中所做的那样)。

还有播放音频的方法

func playSound(file:String, ext:String) -> Void {
do {
let url = URL.init(fileURLWithPath: Bundle.main.path(forResource: file, ofType: ext)!)
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch let error {
NSLog(error.localizedDescription)
}
}

这样就可以调用

playSound(file: "startup", ext: "wav")

在您的应用中随时随地播放此声音(以及其他声音),代码重复最少。

关于ios - Xcode- swift ;为启动屏幕添加音效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43715285/

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