1.首先開發插件:因為我的項目前需要所以要做(根據情況)
在項目的plugins文件中新建obj c文件。如
Demo,此時會產生出Demo.h和Demo.m兩個文件。
.h文件主要就是定義一些方法,類似java中的接口.(要繼承CDVPlugin)
.m文件是對h文件夾的實現,在插件執行時會進入相應的函數,切記:此函數要易執行長時的內容,此時uithread處于阻塞狀態。不用我們可以啟動一個線程在函數中,啟動線的的的函數如下:
NSThread *thread=[[NSThread alloc]initWithTarget:selft selector:@selector(doInBackground:)object:argumetns];//doInBackground是在新得線程中要執行的方法[thread start];
我這里簡單很一些code:
#import<Foundation/Foundation.h>#import<Cordova/CDVPlugin.h>@Interface DisplayNumber:CDVPlugin-(void) setNumber:(CDVInvokeURLCommand) command;@end;
2.在config.xml中啟用插件
添加<feature name="Demo">
<param name='ios-package' value='Demo'/>
</feature>
這里說明一下:value值是我們前面定義的類名,面feature中的name指得是我們前面再寫js時,要調用的插件的名子,如果不明白,寫個寫成同一個名也行。(我就是這樣做的)
3 編輯寫插件js
var Demo=function(){ } Demo.prototype={ method:function(fun1,fun2,params){cordova.exec(fun1//成功時調用,fun2,'插件名','插件的方法名',[params//參數數組]);}}
若我們想使用Demo插件,簡單的可以寫成new Demo().method(fun1,fun2,params);//很簡單
說明一下:我們也可以在插件的js里的new Demo()給一個變量,我們再調用時就不用再new 一個。
關于后臺無限運行的解決(網上也有很多解決方案)
1. Info.plist文件中新增:Required Background modes (是一個數組形式的建值),在item0后的value設置成為 App plays audio or streams audio/video using AirPlay。
2.在Classes文件夾下找到MainViewController.h,
#import <Cordova/CDVViewController.h>#import <Cordova/CDVCommandDelegateImpl.h>#import <Cordova/CDVCommandQueue.h>#import <AVFoundation/AVFoundation.h>@interface MainViewController : CDVViewController{ AVAudioPlayer *audioPlayer;}@property(nonatomic) AVAudioPlayer * audioPlayer;@end@interface MainCommandDelegate : CDVCommandDelegateImpl@end@interface MainCommandQueue : CDVCommandQueue@end
接著修改MainViewController.m文件,找到viewDidLoad方法,修改為:
- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view from its nib. dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(dispatchQueue, ^(void) { NSError *audioSessionError = nil; AVAudioSession *audioSession = [AVAudioSession sharedInstance]; if ([audioSession setCategory:AVAudioSessionCategoryPlayback error:&audioSessionError]){ NSLog(@"Successfully set the audio session."); } else { NSLog(@"Could not set the audio session"); } NSBundle *mainBundle = [NSBundle mainBundle]; NSLog(@"%@",mainBundle); NSString *filePath = [mainBundle pathForResource:@"love" ofType:@"wav"]; NSData *fileData = [NSData dataWithContentsOfFile:filePath]; NSError *error = nil; NSLog(@"AA%@",filePath); self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&error]; if (self.audioPlayer != nil){ self.audioPlayer.delegate = self; [self.audioPlayer setNumberOfLoops:-1]; if ([self.audioPlayer prepareToPlay] && [self.audioPlayer play]){ NSLog(@"Successfully started playing..."); } else { NSLog(@"Failed to play."); } } else { NSLog(@"Failed to play."); } }); }
說明:love.wav文件是other Sources下的文件。
接著修改AppDelegate.m文件,新增方法:
-(void) applicationDidEnterBackground:(UIApplication *)application{// [NSRunLoop currentRunLoop];// // UIApplication *app=[UIApplication sharedApplication];// __block UIBackgroundTaskIdentifier bgTask;// bgTask=[app beginBackgroundTaskWithExpirationHandler:^{// dispatch_async(dispatch_get_main_queue(), ^{// if(bgTask!=UIBackgroundTaskInvalid){// bgTask=UIBackgroundTaskInvalid;// }// });// }];// // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{// dispatch_async(dispatch_get_main_queue(), ^{// if(bgTask!=UIBackgroundTaskInvalid){// bgTask=UIBackgroundTaskInvalid;// }// });// });// // [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{// NSLog(@"KeepAlive");// }]; MainViewController *mvc=[[MainViewController alloc] init]; [mvc viewDidLoad];}
網上也有很多,發現在模擬器下可以長時間運行,但在真實機下并不能運行。發現還是長時間播放一個無聲的音頻文件好一點.
-------------------如果有什么不好的地方,請指教。
新聞熱點
疑難解答