有用的代码

分享2个宏,放到pch文件里面,你懂的

//use dlog to print while in debug model
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define DLog(...)
#endif

//another one
#if DEBUG
#define NSLog(FORMAT, ...) fprintf(stderr,"%s:%d\t%s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define NSLog(FORMAT, ...) nil

#endif




后台下载,发出来给大家参考参考,貌似这样只能下10分钟

//crespo add 1101
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // 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.
  
  //crespo add 1109 to close broadcast when enter background
  if (onlookerUDP)
  {
    [self stopAndClearTimer];
    [onlookerUDP release];
    onlookerUDP = nil;
  }
  
  //crespo add 1031 for back fground download
  UIDevice* device = [UIDevice currentDevice];
  BOOL backgroundSupported = NO;
  if ([device respondsToSelector:@selector(isMultitaskingSupported)])
  {
    backgroundSupported = device.multitaskingSupported;
  }
  if (backgroundSupported == YES)
  {
    //crespo add 1108
    if ([[self GetCurrntNet]isEqualToString:@"3g"])
    {
      NSUserDefaults* userDefault = [NSUserDefaults standardUserDefaults];
      if ([userDefault boolForKey:@"3gBackgroudDownload"] == NO)
      {
        return;
      }
    }

    
    __block UIBackgroundTaskIdentifier bgTask;
    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
      // Clean up any unfinished task business by marking where you.
      // stopped or ending the task outright.
      //如果系统觉得我们还是运行了太久,将执行这个程序块,并停止运行应用程序
      for (int i = 0; i < [TCDownloadManager sharedManager].tasks.count; i++)
      {
        TCDownloadTaskVideo* t = [[TCDownloadManager sharedManager].tasks objectAtIndex:i];
        if (t.status == DownloadTaskStatusDownloading)
        {
          //stop downloading
          DLog(@"stop downloading after 10 minutes later!");
          [t cancel];
        }
      }
      [application endBackgroundTask:bgTask];
      bgTask = UIBackgroundTaskInvalid;
    }];
    
    //UIBackgroundTaskInvalid表示系统没有为我们提供额外的时候
    if (bgTask == UIBackgroundTaskInvalid) {
      DLog(@"Failed to start background task!");
      return;
    }
    
    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      
      // Do the work associated with the task, preferably in chunks.
      //do not use NSEnumerator,has bug
//      for (TCDownloadTaskVideo* t in [TCDownloadManager sharedManager].tasks)
//      {
//        ...
//      }
      
      for (int i = 0; i < [TCDownloadManager sharedManager].tasks.count; i++)
      {
        TCDownloadTaskVideo* t = [[TCDownloadManager sharedManager].tasks objectAtIndex:i];
        if (t.status == DownloadTaskStatusDownloading)
        {
          //keep on downloading
          DLog(@"start background task for downloading!");
          [t cancel];
          [[TCDownloadManager sharedManager]restartTask:t];
        }
      }
       // 任务做完以后通过下面的代码通知系统该任务结束了
      //[application endBackgroundTask:bgTask];
      //bgTask = UIBackgroundTaskInvalid;
    });
  }
  
}

刚刚有的同学问我,同时录音和播放就没声音了,以下是我在stackoverflow找到的方法,我用过可以的,如果有别的欢迎补充:
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone || UIUserInterfaceIdiomPad)
        {
            AVAudioSession *audioSession = [AVAudioSession sharedInstance];
            NSError *error;
            if ([audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error])
            {
                if ([audioSession setActive:YES error:&error])
                {
                    //        AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
                }
                else
                {
                    NSLog(@"Failed to set audio session category: %@", error);
                }
            }
            else
            {
                NSLog(@"Failed to set audio session category: %@", error);
            }
            UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
            AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof(audioRouteOverride),&audioRouteOverride);

        }


ios持续震动&解决震动后电影/音乐无声音bug的方法

持续震动代码:
#import < AudioToolbox/AudioToolbox.h>
#import < UIKit/UIKit.h>


void MyAudioServicesSystemSoundCompletionProc (SystemSoundID ssID,void *clientData)
{
BOOL* iShouldKeepBuzzing = clientData;
if (*iShouldKeepBuzzing)
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
else
{
//Unregister, so we don't get called again...
AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);
}
}

-(void)infinityVibrate
{
BOOL iShouldKeepBuzzing = YES;
AudioServicesAddSystemSoundCompletion (
kSystemSoundID_Vibrate,
NULL,
NULL,
MyAudioServicesSystemSoundCompletionProc,
&iShouldKeepBuzzing );
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}

这么调用就行了撒:[self infinityVibrate];

播放电影的时候震动,电影无声音,解决办法:
//add by crespo to fix a bug ,when start to play movie vibrate won't disable the movie voice
AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值