我试图AVPlayer在一个更大的视图内截取屏幕截图.我想只构建一个测试框架,所以私有APIs或任何方法都是好的,因为在发布到AppStore时不会包含框架.
我试过用
UIGetScreenImage() :在模拟器上运行良好但在设备上运行不正常 snapshotviewafterscreenupdates:它显示视图,但我不能创建一个UIImage.drawViewInHierarchy并且renderInContext不会合作AVPlayer AVAssetGenerator用于从视频中获取图像,很难获得良好的坐标或视频播放器作为其他视图的子视图我一直在stackoverflow和网络上寻找一段时间来解决我的视频流问题.我需要以一种方式将从相机捕获的实时视频(无需高质量)从iOS设备流式传输到远程PC,即iOS设备将视频流发送到服务器/ PC但不是相反的.
在一些谷歌搜索和文档浏览后出现的是,可以使用主要的主要标准/协议:
同样,我的要求是iPhone/iPad将流式传输视频.从Apple网站上的内容可以看出,HLS将从服务器端的编码角度和iOS侧的解码角度使用.从RTMP开始,大多数允许iOS流媒体的图书馆都有商业许可证和封闭代码,或者要求您通过他们的P2P基础设施(例如angl.tv或tokbox.com/opentok/quick-start).从HLS开始,iOS方面似乎没有编码库.
所以我的问题是:
我非常感谢你们,亲爱的朋友们.
迈赫迪
如你所见,我正在播放音频广播.但是,当我按下主页按钮退出应用程序流停止或我听不到.如何在后台继续流式传输并从锁定屏幕中进行监听?
ViewController.Swift
import UIKit
import AVFoundation
import MediaPlayer
import GoogleMobileAds
class ViewController: UIViewController, GADInterstitialDelegate {
@IBOutlet weak var exitMapButton: UIButton!
@IBOutlet weak var radarMap: UIWebView!
var interstitial: GADInterstitial!
func createAndLoadInterstitial() -> GADInterstitial {
var interstitial = GADInterstitial(adUnitID: "ca-app-pub-5378899862041789/2782958552")
interstitial.delegate = self
interstitial.loadRequest(GADRequest())
return interstitial
}
func getAd(){
if (self.interstitial.isReady)
{
self.interstitial.presentFromRootViewController(self)
self.interstitial = self.createAndLoadInterstitial()
}
}
@IBOutlet weak var ataturkButton: UIButton!
@IBOutlet weak var sabihaButton: UIButton!
@IBOutlet weak var esenbogaButton: UIButton!
@IBOutlet weak var weatherButton: UIButton!
@IBOutlet weak var statusLabel: …Run Code Online (Sandbox Code Playgroud) 以下用于在Swift 1.2中工作:
var recordSettings = [
AVFormatIDKey: kAudioFormatMPEG4AAC,
AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
AVEncoderBitRateKey : 320000,
AVNumberOfChannelsKey: 2,
AVSampleRateKey : 44100.0]
Run Code Online (Sandbox Code Playgroud)
现在,它给出了错误:
"没有更多的上下文,类型表达式是模糊的".
我正在使用此代码捕获图像
#pragma mark - image capture
// Create and configure a capture session and start it running
- (void)setupCaptureSession
{
NSError *error = nil;
// Create the session
AVCaptureSession *session = [[AVCaptureSession alloc] init];
// Configure the session to produce lower resolution video frames, if your
// processing algorithm can cope. We'll specify medium quality for the
// chosen device.
session.sessionPreset = AVCaptureSessionPresetMedium;
// Find a suitable AVCaptureDevice
AVCaptureDevice *device = [AVCaptureDevice
defaultDeviceWithMediaType:AVMediaTypeVideo];
// Create a device input with the …Run Code Online (Sandbox Code Playgroud) 我正在尝试将视频文件加载到我的iPad应用程序中AVURLAsset,使用异步加载的东西等待它准备好.问题是,当我运行它时,我得到一个完全通用的"失败"错误消息,我不知道该怎么做.如果我把它交给一个视频MPMoviePlayerController,但是AVURLAsset似乎拒绝与它有任何关系.
码:
asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:[docPath stringByAppendingPathComponent:@"video.mov"]] options:nil];
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self composeIfReady];
});
}];
Run Code Online (Sandbox Code Playgroud)
...
- (void)composeIfReady
{
NSError *error = nil;
if([asset statusOfValueForKey:@"tracks" error:&error] == AVKeyValueStatusFailed)
NSLog(@"error loading: %@", [error description]);
if(error == nil)
NSLog(@"okay awesome");
}
Run Code Online (Sandbox Code Playgroud)
输出:
error loading: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation couldn’t be completed. (AVFoundationErrorDomain error -11800.)" UserInfo=0x1696f0 {NSUnderlyingError=0x169a40 "The operation couldn’t be completed. (OSStatus error -12936.)"}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,-11800是"未知错误"的错误代码.有点死路一条.有任何想法吗?在我尝试加载资产之前,我应该设置一些东西吗?
在iOS 10问世之前,我使用以下代码来获取录像机的视频和音频:
for device in AVCaptureDevice.devices()
{
if (device as AnyObject).hasMediaType( AVMediaTypeAudio )
{
self.audioCapture = device as? AVCaptureDevice
}
else if (device as AnyObject).hasMediaType( AVMediaTypeVideo )
{
if (device as AnyObject).position == AVCaptureDevicePosition.back
{
self.backCameraVideoCapture = device as? AVCaptureDevice
}
else
{
self.frontCameraVideoCapture = device as? AVCaptureDevice
}
}
}
Run Code Online (Sandbox Code Playgroud)
当iOS 10终于问世时,我在运行代码时收到了以下警告.请注意,我的录像机仍然可以正常工作约2周.
在iOS 10.0中不推荐使用'devices()':请改用AVCaptureDeviceDiscoverySession.
当我今天早上运行我的代码时,我的录像机停止了工作.xCode8没有给我任何错误,但摄像头捕获的previewLayer是完全白色的.当我开始录制时,我收到以下错误:
错误域= AVFoundationErrorDomain代码= -11800"操作无法完成"UserInfo = {NSLocalizedDescription =操作无法完成,NSUnderlyingError = 0x17554440 {错误域= NSOSStatusErrorDomain代码= -12780"(null)"},NSLocalizedFailureReason = An发生未知错误(-12780)}
我认为这与我使用弃用的方法有关AVCaptureDevice.devices().因此,我想知道如何使用AVCaptureDeviceDiscoverySession?
提前谢谢你的帮助!
在我的应用程序中,我必须播放存储在Web服务器上的音频文件.我正在使用AVPlayer它.我有所有播放/暂停控件和所有代表和观察员在那里工作得非常好.在播放小音频文件时,一切都很棒.
播放长音频文件时,它也会开始正常播放,但几秒后AVPlayer暂停播放(最可能是缓冲它).问题是它不能再自行恢复.它保持暂停状态,如果我再次手动按下播放按钮,它将再次流畅播放.
我想知道为什么AVPlayer不自动恢复,如何在没有用户再次按下播放按钮的情况下再次恢复音频?谢谢.
如何像"相机"那样实时进行人脸检测?
我注意到,AVCaptureStillImageOutput为10.0后弃用,因此我用 AVCapturePhotoOutput代替.但是,我发现我为面部检测保存的图像并不那么令人满意?有任何想法吗?
UPDATE
在尝试了@Shravya Boggarapu之后提到.目前,我AVCaptureMetadataOutput用来检测脸部没有CIFaceDetector.它按预期工作.然而,当我试图画出脸部的边界时,似乎错位了.任何的想法?
let metaDataOutput = AVCaptureMetadataOutput()
captureSession.sessionPreset = AVCaptureSessionPresetPhoto
let backCamera = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .back)
do {
let input = try AVCaptureDeviceInput(device: backCamera)
if (captureSession.canAddInput(input)) {
captureSession.addInput(input)
// MetadataOutput instead
if(captureSession.canAddOutput(metaDataOutput)) {
captureSession.addOutput(metaDataOutput)
metaDataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
metaDataOutput.metadataObjectTypes = [AVMetadataObjectTypeFace]
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.frame = cameraView.bounds
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
cameraView.layer.addSublayer(previewLayer!)
captureSession.startRunning()
}
}
} catch {
print(error.localizedDescription)
}
Run Code Online (Sandbox Code Playgroud)
和
extension CameraViewController: AVCaptureMetadataOutputObjectsDelegate {
func captureOutput(_ captureOutput: …Run Code Online (Sandbox Code Playgroud) 我一直在使用自定义相机,最近我和Swift 3一起升级到Xcode 8 beta.我原来是这样的:
var stillImageOutput: AVCaptureStillImageOutput?
Run Code Online (Sandbox Code Playgroud)
但是,我现在收到警告:
在iOS 10.0中不推荐使用'AVCaptureStillImageOutput':改用AVCapturePhotoOutput
由于这是相当新的,我没有看到很多关于此的信息.这是我目前的代码:
var captureSession: AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var previewLayer: AVCaptureVideoPreviewLayer?
func clickPicture() {
if let videoConnection = stillImageOutput?.connection(withMediaType: AVMediaTypeVideo) {
videoConnection.videoOrientation = .portrait
stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (sampleBuffer, error) -> Void in
if sampleBuffer != nil {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let dataProvider = CGDataProvider(data: imageData!)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
let image = UIImage(cgImage: cgImageRef!, scale: 1, orientation: .right)
}
})
}
} …Run Code Online (Sandbox Code Playgroud)