在我的Codename One应用程序中,我需要将音频捕获的持续时间限制为几秒钟。我为此调用了class TextBubble: UIView {
var bubble: EmptyBubble!
var checkbox: Checkbox!
var textLength: Double!
var label: UILabel!
var isNcalc = false
var checkboxConstraint: [NSLayoutConstraint]?
var maximumCheckboxSize: CGFloat = 0
init(text: String, isNcalc: Bool) {
super.init(frame: CGRect.zero)
self.alpha = 0
self.isNcalc = isNcalc
bubble = EmptyBubble()
self.addSubview(bubble)
checkbox = Checkbox()
self.addSubview(checkbox)
self.maximumCheckboxSize = Checkbox.checkedImage?.size.width ?? 0
if checkboxConstraint == nil {
print("add")
checkboxConstraint = checkbox.constrainWidth(0)
} else {
print("HUZAAAA!!")
}
checkbox.alpha = 0
label = UILabel()
label.numberOfLines = 0
label.textAlignment = .natural
label.adjustsFontSizeToFitWidth = true
if isNcalc {
label.textAlignment = LangUtil.getTextLang() == "he" ? .right : .left
}
label.textColor = UIColor(hex: 0x586C76)
label.text = text
bubble.content.addSubview(label)
label.stretchToBoundsOfSuperView()
textLength = Double(text.characters.count)
if LangUtil.isDeviceHebrew() {
checkbox.constrainToRightOfSuperView(0)
} else {
checkbox.constrainToLeftOfSuperView(0)
}
checkbox.centerVerticallyTo(self)
checkbox.constrainToTopOfSuperViewLessThan()
checkbox.constrainToBottomOfSuperViewLessThan()
bubble.anchorToRightGreaterThan(checkbox, padding: 0)
bubble.centerVerticallyTo(self)
bubble.constrainWidthLessThanSuperView(0.8)
bubble.constrainToTopOfSuperViewLessThan()
bubble.constrainToBottomOfSuperViewLessThan()
if isNcalc {
bubble.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(bubbleTapped(_:))))
bubble.isUserInteractionEnabled = true
}
}
@discardableResult
func snapCheckbox() -> Bool {
if let currSize = checkboxConstraint?.first?.constant {
let show = 1.5 * currSize > maximumCheckboxSize
showCheckbox(show)
return show
}
return false
}
func setCheckboxSize(_ delta: CGFloat) {
if let currSize = checkboxConstraint?.first?.constant {
let divider = delta > 0 ? 6 : 5
let newSize = currSize + (delta / divider)
if newSize >= 0 && newSize <= maximumCheckboxSize * 1.9 {
checkboxConstraint?.first?.constant = newSize
self.checkbox.alpha = newSize / maximumCheckboxSize
}
}
}
func bubbleTapped(_ tap: UITapGestureRecognizer) {
if checkbox.isVisible() {
checkbox.sendActions(for: .touchUpInside)
}
}
func showCheckbox(_ show: Bool) {
let duration = bubble.isVisible() ? Constants.ANIMATION_DURATION : 0
UIView.animate(withDuration: Constants.ANIMATION_DURATION, delay: 0, options: [.curveEaseOut, .beginFromCurrentState, .allowUserInteraction], animations: {
self.checkboxConstraint?.first?.constant = show ? self.maximumCheckboxSize : 0
self.checkbox.alpha = show ? 1 : 0
if duration > 0 {
self.superview?.layoutIfNeeded()
}
} , completion: nil)
}
override func didMoveToSuperview() {
super.didMoveToSuperview()
if superview != nil {
self.constrainWidthLessThanSuperView(1)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
,并且似乎没有等同于Capture.captureAUdio()
可以将捕获的音频绑定到给定的持续时间。
到目前为止我所做的是测量音频捕获持续时间,如果它太长,则要求用户再次启动它们。但我发现它有点麻烦。
还有什么比这更好的了吗?
任何提示赞赏,
答案 0 :(得分:3)
没有
对于音频虽然您可以使用MediaManager
中的媒体录制功能,但不如Capture
那么直观,但允许更多“细粒度”控制。