1.首先开发插件:因为我的项目前需要所以要做(根据情况)
   在项目的plugins文件中新建obj c文件。如
   Demo,此时会产生出Demo.h和Demo.m两个文件。
   .h文件主要就是定义一些方法,类似java中的接口.(要继承CDVPlugin)
   .m文件是对h文件夹的实现,在插件执行时会进入相应的函数,切记:此函数要易执行长时的内容,此时uithread处于阻塞状态。不用我们可以启动一个线程在函数中,启动线的的的函数如下:
Java代码
class="Apple-converted-space">   
- NSThread *thread=[[NSThread alloc]initWithTarget:selft selector:@selector(doInBackground:)object:argumetns];  
- [thread start];  
 
我这里简单很一些code:
Java代码
   
- #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
  
Java代码
   
- var Demo=function(){  
-    
-   }  
-   Demo.prototype={  
-   method:function(fun1,fun2,params){cordova.exec(fun1
- }  
- }  
 
若我们想使用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,
 
Java代码
   
- #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方法,修改为:
Java代码
   
- - (void)viewDidLoad  
- {  
-     [super viewDidLoad];  
-     
-     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文件,新增方法:
Java代码
   
- -(void) applicationDidEnterBackground:(UIApplication *)application{  
-     MainViewController *mvc=[[MainViewController alloc] init];  
-     [mvc viewDidLoad];  
-   
- }  
 网上也有很多,发现在模拟器下可以长时间运行,但在真实机下并不能运行。发现还是长时间播放一个无声的音频文件好一点.
 
 -------------------如果有什么不好的地方,请指教。