【iPhone开发常用代码积累】单例模式计时器NSTimer播放音频

    技术2022-06-22  49

    单例模式

    //单例模式 + (AVCommon *)sharedAVCommon { static AVCommon *sharedAVCommon; @synchronized(sharedAVCommon) { if (sharedAVCommon == nil) { sharedAVCommon=[[AVCommon alloc]init]; } } return sharedAVCommon; }

     

     

     

    计时器NSTimer

    //.h头文件 NSTimer *timer; //... ... @property (nonatomic, retain) NSTimer *timer; //.m文件 //创建新的计时器实例 - (void)openNewTimer { //这个类方法返回一个计时器实例,@selector中的方法每隔一秒执行一次(时间由第一个参数设定) timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(showTime) userInfo:nil repeats:YES]; }

     

     

     

    播放音频

    //播放音频 - (void)playAudio:(id)sender { NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachePath = [cache objectAtIndex:0] ; NSString *voiceName = [cachePath stringByAppendingPathComponent:[sender stringByAppendingString:@".wav"]]; NSLog(@"%@",voiceName); NSURL *soundFileURL = [NSURL fileURLWithPath:voiceName]; AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:soundFileURL error:nil]; self.player = newPlayer; [newPlayer release]; [self.player prepareToPlay]; self.player.delegate = self; [self.player play]; }

     


    最新回复(0)