Explorar o código

1.后台保活

huangxiaodong hai 1 ano
pai
achega
096a717acc

+ 20 - 0
创维盒子/双子星云手机.xcodeproj/project.pbxproj

@@ -142,6 +142,9 @@
 		6B3F963A2AD2AD0B008E349E /* newWorkInterface.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B3F96392AD2AD0B008E349E /* newWorkInterface.h */; };
 		6B7B65582AD9227700BE8CB3 /* PlayerViewController+otherDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 6B7B65562AD9227700BE8CB3 /* PlayerViewController+otherDelegate.h */; };
 		6B7B65592AD9227700BE8CB3 /* PlayerViewController+otherDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 6B7B65572AD9227700BE8CB3 /* PlayerViewController+otherDelegate.m */; };
+		6BBB35D92AE7645200E4A59B /* RunInBackground.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = 6BBB35D62AE7645200E4A59B /* RunInBackground.mp3 */; };
+		6BBB35DD2AE76AD800E4A59B /* AudioSessionObject.h in Headers */ = {isa = PBXBuildFile; fileRef = 6BBB35DB2AE76AD800E4A59B /* AudioSessionObject.h */; };
+		6BBB35DE2AE76AD800E4A59B /* AudioSessionObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 6BBB35DC2AE76AD800E4A59B /* AudioSessionObject.m */; };
 		6BF0F3D02AD391D8000AA133 /* connectDeviceManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 6BF0F3CE2AD391D8000AA133 /* connectDeviceManager.h */; };
 		6BF0F3D12AD391D8000AA133 /* connectDeviceManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 6BF0F3CF2AD391D8000AA133 /* connectDeviceManager.m */; };
 		6BF0F3D52AD3937B000AA133 /* DeviceThirdIdModel.h in Headers */ = {isa = PBXBuildFile; fileRef = 6BF0F3D32AD3937B000AA133 /* DeviceThirdIdModel.h */; };
@@ -464,6 +467,9 @@
 		6B7B656E2ADEAE3D00BE8CB3 /* ja */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ja; path = ja.lproj/HWAboutViewController.strings; sourceTree = "<group>"; };
 		6B7B656F2ADEAE3D00BE8CB3 /* ja */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ja; path = ja.lproj/HWHistoryViewController.strings; sourceTree = "<group>"; };
 		6B7B65702ADEAE3D00BE8CB3 /* ja */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ja; path = ja.lproj/Localizable.strings; sourceTree = "<group>"; };
+		6BBB35D62AE7645200E4A59B /* RunInBackground.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = RunInBackground.mp3; sourceTree = "<group>"; };
+		6BBB35DB2AE76AD800E4A59B /* AudioSessionObject.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AudioSessionObject.h; sourceTree = "<group>"; };
+		6BBB35DC2AE76AD800E4A59B /* AudioSessionObject.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AudioSessionObject.m; sourceTree = "<group>"; };
 		6BF0F3CE2AD391D8000AA133 /* connectDeviceManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = connectDeviceManager.h; sourceTree = "<group>"; };
 		6BF0F3CF2AD391D8000AA133 /* connectDeviceManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = connectDeviceManager.m; sourceTree = "<group>"; };
 		6BF0F3D32AD3937B000AA133 /* DeviceThirdIdModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DeviceThirdIdModel.h; sourceTree = "<group>"; };
@@ -1025,6 +1031,16 @@
 			path = netWork;
 			sourceTree = "<group>";
 		};
+		6BBB35D42AE7645200E4A59B /* BackgroundKeepAlive */ = {
+			isa = PBXGroup;
+			children = (
+				6BBB35D62AE7645200E4A59B /* RunInBackground.mp3 */,
+				6BBB35DB2AE76AD800E4A59B /* AudioSessionObject.h */,
+				6BBB35DC2AE76AD800E4A59B /* AudioSessionObject.m */,
+			);
+			path = BackgroundKeepAlive;
+			sourceTree = "<group>";
+		};
 		6BF0F3CD2AD391BE000AA133 /* connectDeviceManager */ = {
 			isa = PBXGroup;
 			children = (
@@ -1095,6 +1111,7 @@
 		A003F6B727D841FF00715CBF /* Vendor */ = {
 			isa = PBXGroup;
 			children = (
+				6BBB35D42AE7645200E4A59B /* BackgroundKeepAlive */,
 				6BF52C8F2AD5546500A617DB /* pingTool */,
 				18664FEA2AC28A0E008F8A61 /* UDP */,
 				18BA24852AA5C57900BA01EE /* RSATool.h */,
@@ -1416,6 +1433,7 @@
 			files = (
 				184C8AD02A94980D00F26650 /* CustomerWebViewController.h in Headers */,
 				184C8ADA2A949DAC00F26650 /* HaveNewVersionView.h in Headers */,
+				6BBB35DD2AE76AD800E4A59B /* AudioSessionObject.h in Headers */,
 				183AE6D12A8CA98C00B11CB0 /* CloudPhoneAPI.h in Headers */,
 				1868201D2AB97BE1005702A6 /* TipsQRCodeViewController.h in Headers */,
 				181E85002A8F6B1800292B96 /* LogoSelectView.h in Headers */,
@@ -1582,6 +1600,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				18438BEB2A04DD7D00981C34 /* HWBookmarkViewController.xib in Resources */,
+				6BBB35D92AE7645200E4A59B /* RunInBackground.mp3 in Resources */,
 				18438BE62A04B3AB00981C34 /* Localizable.strings in Resources */,
 				A003F6C527D8423600715CBF /* loadingWhiteGif.gif in Resources */,
 				A08A94FF27E9A4E400C544BB /* FMDBInfo.plist in Resources */,
@@ -1767,6 +1786,7 @@
 				A031DD7A27EC713A00909527 /* HWSetViewController.m in Sources */,
 				183AE6022A89CE3C00B11CB0 /* JSONValueTransformer.m in Sources */,
 				183AE6522A8A2CF000B11CB0 /* AFAutoPurgingImageCache.m in Sources */,
+				6BBB35DE2AE76AD800E4A59B /* AudioSessionObject.m in Sources */,
 				181E84FC2A8F57A500292B96 /* ChangeLogoViewController.m in Sources */,
 				A031DD7F27EC78C800909527 /* HWAgreementViewController.m in Sources */,
 				183AE64F2A8A2CF000B11CB0 /* UIImageView+AFNetworking.m in Sources */,

+ 5 - 1
创维盒子/双子星云手机/AppDelegate/AppDelegate.m

@@ -8,7 +8,7 @@
 #import "AppDelegate.h"
 #import <CoreMotion/CoreMotion.h>
 #import "ShearDeviceUDPManager.h"
-
+#import "AudioSessionObject.h"
 @interface AppDelegate (){
     CMMotionManager *cmManager;
 }
@@ -71,6 +71,10 @@
 //    [[ShearDeviceUDPManager shareInstance] startShearchDevice];
 //    [[ShearDeviceUDPManager shareInstance] shearchDeviceLoop];
     
+    
+    //启动后台保活
+    [AudioSessionObject shareManager];
+    
     return YES;
 }
 

+ 1 - 0
创维盒子/双子星云手机/Info.plist

@@ -101,6 +101,7 @@
 	<key>UIBackgroundModes</key>
 	<array>
 		<string>audio</string>
+		<string>processing</string>
 	</array>
 	<key>UIViewControllerBasedStatusBarAppearance</key>
 	<false/>

+ 29 - 0
创维盒子/双子星云手机/Vendor/BackgroundKeepAlive/AudioSessionObject.h

@@ -0,0 +1,29 @@
+//
+//  AudioSessionObject.h
+//  隐私保护
+//
+//  Created by xd h on 2023/10/24.
+//
+
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface AudioSessionObject : NSObject
+/// 创建单利
++ (AudioSessionObject *)shareManager;
+
+/// 创建音乐播放器
+- (void)creatAVAudioSessionObject;
+
+/// 开始播放音乐
+- (void)startPlayAudioSession;
+
+/// 停止播放音乐
+- (void)stopPlayAudioSession;
+@end
+
+NS_ASSUME_NONNULL_END
+
+
+

+ 105 - 0
创维盒子/双子星云手机/Vendor/BackgroundKeepAlive/AudioSessionObject.m

@@ -0,0 +1,105 @@
+//
+//  AudioSessionObject.m
+//  隐私保护
+//
+//  Created by xd h on 2023/10/24.
+//
+
+#import "AudioSessionObject.h"
+
+#import <AVFoundation/AVFoundation.h>
+#import <AudioToolbox/AudioToolbox.h>
+
+@interface AudioSessionObject()
+@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
+@property (strong,nonatomic) NSTimer *timer;
+@property (nonatomic, assign) UIBackgroundTaskIdentifier backgrounTask;
+
+
+@end
+
+@implementation AudioSessionObject
+
+/// 创建单利
++ (AudioSessionObject *)shareManager{
+   static AudioSessionObject *manager = nil;
+   static dispatch_once_t onceToken;
+   dispatch_once(&onceToken, ^{
+       manager = [[AudioSessionObject alloc]init];
+   });
+   return manager;
+}
+
+- (instancetype)init {
+    self = [super init];
+    if (self) {
+        [self addNoti];
+        [self creatAVAudioSessionObject];
+    }
+    return self;
+}
+
+- (void)addNoti {
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
+    
+}
+
+- (void)appWillEnterForeground {
+    NSLog(@"%@ appWillEnterForeground",NSStringFromClass([self class]));
+    [self stopPlayAudioSession];
+    
+    [self.timer invalidate];
+}
+
+- (void)appDidEnterBackground {
+    NSLog(@"%@ appDidEnterBackground",NSStringFromClass([self class]));
+    [self startPlayAudioSession];
+    
+    
+    self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(applyToSystemForMoreTime) userInfo:nil repeats:YES];
+    [self.timer setFireDate:[NSDate distantPast]];
+}
+
+
+/// 创建音乐播放器
+- (void)creatAVAudioSessionObject{
+   //设置后台模式和锁屏模式下依然能够播放
+   [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
+   [[AVAudioSession sharedInstance] setActive: YES error: nil];
+   //初始化音频播放器
+   NSError *playerError;
+   NSURL *urlSound = [[NSURL alloc]initWithString:[[NSBundle mainBundle]pathForResource:@"RunInBackground" ofType:@"mp3"]];
+   _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:urlSound error:&playerError];
+   _audioPlayer.numberOfLoops = -1;//无限播放
+   _audioPlayer.volume = 0;
+}
+
+/// 开始播放声音
+- (void)startPlayAudioSession{
+    NSLog(@"%@ startPlayAudioSession",NSStringFromClass([self class]));
+   [_audioPlayer play];
+}
+
+/// 停止播放声音
+- (void)stopPlayAudioSession{
+    NSLog(@"%@ stopPlayAudioSession",NSStringFromClass([self class]));
+   [_audioPlayer stop];
+}
+
+ 
+ 
+#pragma mark - 定时器设置判断后台保活时间,如果将要被后台杀死,重新唤醒
+- (void)applyToSystemForMoreTime {
+    if ([UIApplication sharedApplication].backgroundTimeRemaining < 30.0) {//如果剩余时间小于30秒
+        [[UIApplication sharedApplication] endBackgroundTask:self.self.backgrounTask];
+        self.backgrounTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
+            [[UIApplication sharedApplication] endBackgroundTask:self.self.backgrounTask];
+            self.backgrounTask = UIBackgroundTaskInvalid;
+        }];
+    }
+}
+
+
+@end
+

BIN=BIN
创维盒子/双子星云手机/Vendor/BackgroundKeepAlive/RunInBackground.mp3