gpt4 book ai didi

ios - 如何在 iOS 11 中实现音量快门?

转载 作者:可可西里 更新时间:2023-11-01 05:40:14 29 4
gpt4 key购买 nike

我想在我的相机应用中实现音量快门。当用户按下音量按钮时,我应该得到一个事件来拍照。

我正在寻找满足以下要求的实现:

  • 即使音量当前处于最大并且用户按下音量增大按钮,它也应该可以工作。
  • 不应有显示音量已更改的屏幕 UI。
  • 应该没有已知的 Apple 拒绝使用此技术的应用程序的案例。

关于此主题存在其他问题和答案,但适用于旧版本的 iOS,因此我想找到适用于 iOS 11 的问题和答案。

ProCamera、ProCam 和 Camera+ 等相机应用具有满足所有这些条件的音量快门,因此这显然是可能的。

最佳答案

下面是满足您所有要求的代码——不过我不确定 Apple 是否会批准。
我从 StackOverflow 上的问题/答案中提取了所有这些代码。

Tested with iOS 10.2 in Xcode 8.3.1

您需要使用 AVFoundationMediaPlayer 框架才能正常工作。

import UIKit
import AVFoundation
import MediaPlayer

class ViewController: UIViewController {

//keeps track of the initial volume the user had set when entering the app
//used to reset the volume when he exits the app
var volume: Float = 0

override func viewDidLoad() {
super.viewDidLoad()

let audioSession = AVAudioSession.sharedInstance()
volume = audioSession.outputVolume-0.1 //if the user is at 1 (full volume)
try! audioSession.setActive(true)
audioSession.addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.new, context: nil)
//prevents the volume hud from showing up
let volView = MPVolumeView(frame: .zero)
view.addSubview(volView)
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
//when the user changes the volume,
//prevent the output volume from changing by setting it to the default volume we specified,
//so that we can continue pressing the buttons for ever
(MPVolumeView().subviews.filter{NSStringFromClass($0.classForCoder) == "MPVolumeSlider"}.first as? UISlider)?.setValue(volume, animated: false)

//implement your photo-capturing function here
print("volume changed")
}
}

更新

如果您想确保您的代码在用户退出应用程序后仍然有效,请在应用程序激活时使用 AppDelegate 安装观察者,如下所示:

AppDelegate

import UIKit
import AVFoundation
import MediaPlayer

var volume: Float = 0.5

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
let audioSession = AVAudioSession.sharedInstance()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
return true
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
(MPVolumeView().subviews.filter{NSStringFromClass($0.classForCoder) == "MPVolumeSlider"}.first as? UISlider)?.setValue(volume, animated: false)

NotificationCenter.default.removeObserver(self)
NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "volumeChanged")))
}

func applicationDidBecomeActive(_ application: UIApplication) {
volume = audioSession.outputVolume
if volume == 0 { volume += 0.1 } else if volume == 1 { volume -= 0.1 }
try! audioSession.setActive(true)
audioSession.addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.new, context: nil)
}

func applicationWillResignActive(_ application: UIApplication) {
audioSession.removeObserver(self, forKeyPath: "outputVolume")
}
}

View Controller

import UIKit
import AVFoundation
import MediaPlayer

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

NotificationCenter.default.addObserver(self, selector: #selector(self.volumeChanged), name: Notification.Name(rawValue: "volumeChanged"), object: nil)
//prevents the volume hud from showing up
let volView = MPVolumeView(frame: .zero)
view.addSubview(volView)
}

func volumeChanged() {
print("volume changed")
}
}

关于ios - 如何在 iOS 11 中实现音量快门?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41618039/

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