ios 中添加frameworok,实现软件声音和振动

第一步:添加framework

1.点击target

2.选择【build phases】

3.点击【link binary with librarie】

4.点击【+】添加framework。


第二步:

实现调用cocoa库的接口,avaudioplayer


.h文件:

@interface FirstViewController :UIViewController<UIWebViewDelegate>

{

    AVAudioPlayer *player;

}


@property(retain)AVAudioPlayer *player;


@end


.m文件:

@implementation FirstViewController

@synthesize textView;


@synthesize webView;


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    if ([self prepPlayer]) {

        [self.playerplay];

        [self.playersetNumberOfLoops:1];

        

        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);


// 声音逐渐减少

for (int i=9; i>=0; i--) {

            

            self.player.volume = i;

            [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1f]];

        }

        

        [self.player pause];


       //  声音中断时处理

        [[NSUserDefaults standardUserDefaults] setFloat:[self.player currentTime] forKey:@"Interruption"];


    }

    

}


@synthesize player;

-(BOOL) prepPlayer{

    

    NSError *error;

    

    NSString *path = [[NSBundlemainBundle] pathForResource:@"apple"ofType:@"mp3"];

    NSLog(@"path%@",path);

    

    if (![[NSFileManagerdefaultManager] fileExistsAtPath:path]) {

        return NO;

    }

    

    self.player = [[AVAudioPlayeralloc] initWithContentsOfURL:[NSURLfileURLWithPath:path] error:&error];

    

    if (!self.player) {

        return NO;

    }

    

    return YES;

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end



你可能感兴趣的:(ios 中添加frameworok,实现软件声音和振动)