AVAudioPlayer立即停止播放ARC

我试图通过AVAudioPlayer播放MP3,我认为这很简单。 不幸的是,这不是工作。 这是我所做的一切: 为了testing,我在Xcode中创build了一个新的iOS应用程序(Single View)。 我将AVFoundation框架添加到项目中,并将#import <AVFoundation/AVFoundation.h>到ViewController.m 我添加了一个MP3文件到应用程序的文档文件夹。 我改变了ViewControllers viewDidLoad:如下: 码: – (void)viewDidLoad { [super viewDidLoad]; NSString* recorderFilePath = [NSString stringWithFormat:@"%@/MySound.mp3", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]]; AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:recorderFilePath] error:nil]; audioPlayer.numberOfLoops = 1; [audioPlayer play]; //[NSThread sleepForTimeInterval:20]; } 不幸的是,audio开始播放后显然停止。 如果我取消注释sleepForTimeInterval它播放20秒,然后停止。 只有使用ARC进行编译时才会出现此问题,否则,该工作将完美无瑕。

我如何限制一个iOS应用程序只有4英寸的屏幕设备?

Xcode中有一个设置让我声明我的应用程序只支持4“屏幕设备(iPhone 5和最新的iPod Touch)?

为什么使用UINavigationController时无法强制横向?

我发现很多问题强制UIViewController风景作为默认值: 如何强制UIViewController纵向在iOS 6中试试这个: – (BOOL)shouldAutorotate { return YES; } -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } – (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeLeft; } 但是当我在UINavigationController里面使用UIViewController的时候,我不能强迫使用landscape.Please help! *工作不好 – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController]; [navControl setNavigationBarHidden:YES]; self.window.rootViewController = navControl; [self.window makeKeyAndVisible]; […]

使用CoreMotion框架在后台接收加速度计更新

我正在使用下面的代码来获取加速计数据(使用CoreMotion框架): CMMotionManager *motionManager = [[CMMotionManager alloc] init]; motionManager.accelerometerUpdateInterval = 1.0 / 60.0; [motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) { NSLog(@"ACCELEROMETER DATA = %@",accelerometerData); }]; 当应用程序处于前景模式时,我正在接收日志,但是当它进入后台时,只有当应用程序中的音乐正在播放时才会收到日志。 我已经将以下内容添加到应用程序信息plist文件中: – Required background modes – App registers for location updates – App plays audio or streams audio/video using AirPlay 问题是:当音乐不播放时,如何在后台接收加速度计更新?

截图显示空白 – 斯威夫特

嗨,我正在做一个游戏,我已经添加了一个分享button的游戏。 我希望用户能够在一条消息中共享消息,URL和屏幕截图。共享方面工作正常,除了屏幕截图本身显示空白以外,所有内容都显示出来。 这是我用来截图的代码: let layer = UIApplication.sharedApplication().keyWindow!.layer let scale = UIScreen.mainScreen().scale UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale); layer.renderInContext(UIGraphicsGetCurrentContext()) let screenshot = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil) println("screenshot") 请帮我解决这个问题,请确保在Swift语言中这样做。 此外,我使用SpriteKit技术,如果这有所作为。 我是新来的编码,所以请非常清楚。 非常感谢你!

在c ++中使用OpenCV 2.4计算凸性缺陷

我使用OpenCV 2.4来计算图像的凸包。 我也在做一些处理,从图像中去除一些噪音,这与问题无关。 计算凸面的代码如下: … cv::Mat sourceImage; // assume something is already here please cv::vector<cv::Vec4i> hierarchy; std::vector<std::vector<cv::Point> > contours; cv::findContours( sourceImage, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE,cv::Point(0, 0)); // Find the convex hull object for each contour vector<cv::vector<cv::Point> >hull( contours.size() ); for (int i = 0; i < contours.size(); i++) { convexHull( contours[i], hull[i], false ); } […]

用块创build代表

我喜欢积木,当我不能使用它时,这让我很难过。 尤其是,每当我使用委托时,都会发生这种情况(例如:使用UIKit类,主要是预块function)。 所以我想知道…有没有可能 – 使用ObjC的疯狂的力量,做这样的事情? // id _delegate; // Most likely declared as class variable or it will be released _delegate = [DelegateFactory delegateOfProtocol:@protocol(SomeProtocol)]; _delegate performBlock:^{ // Do something } onSelector:@selector(someProtocolMethod)]; // would execute the given block when the given selector is called on the dynamic delegate object. theObject.delegate = (id<SomeProtocol>)_delegate; // Profit! performBlock:onSelector: 如果YES […]

应用程序终止时执行后台任务

我正在为我的学校创build一个应用程序,如果网站上有新的标记,应该每n分钟检查一次。 要在用户首次login时执行此操作,实际标记的编号将保存在“UserDefaults”中。 当应用程序被终止时,在n分钟之后,标记的数量被重新logging并与前一个比较,并且在数字被改变的情况下发送通知。 我想知道是否有办法执行这个任务。 我已经尝试在-applicationWillTerminate中创build一个计时器,但它只被触发一次。 这是我试过的: func applicationWillTerminate(_ application: UIApplication) { DispatchQueue.main.async { self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(AppDelegate.findMark), userInfo: nil, repeats: true) self.timer.fire() } } select器findMark是任务。 提前致谢!

iOS协议/委托混淆?

这一切都是我的第一篇文章,我会尽可能地精确。 我已经阅读了许多关于iOS的协议/委托实现的文章,但是所有的例子都失败了。 假设我有A和B控制器,并希望从A发送数据到BAh @protocol exampleprot <NSObject> @required -(void) exampledmethod:(NSString *) e1; @end @interface ViewController { __weak id <exampleprot> delegate } – 我正在尝试一些程序 [delegate examplemethod:@"test"] BH @interface test2 : UiViewcontroller <exampleprot> 并在Bm中实现方法 – (void)exampledmethod:(NSString *)e1; 所以我做错了什么?

如何将符合相关types的协议的不同types添加到集合中?

作为学习练习,我正在用Swift重写我的validation库 。 我有一个ValidationRule协议,它定义了什么样的个人规则: protocol ValidationRule { typealias InputType func validateInput(input: InputType) -> Bool //… } 关联的typesInputType定义了要validation的input的types(例如String)。 它可以是明确的或通用的。 这里有两条规则: struct ValidationRuleLength: ValidationRule { typealias InputType = String //… } struct ValidationRuleCondition<T>: ValidationRule { typealias InputType = T // … } 在其他地方,我有一个函数用一个ValidationRule集合来ValidationRuleinput: static func validate<R: ValidationRule>(input i: R.InputType, rules rs: [R]) -> ValidationResult { let errors = […]