2015-02-26 10 views
1

Я хочу воспроизвести звуковой файл с помощью AVAudioPlayer, но моя настройка отправит меня в безопасном режиме.Как использовать заголовок AVAudioPlayer?

Вот мой код:

#import <SpringBoard/SpringBoard.h> 
#import <AVFoundation/AVAudioPlayer.h> 

%hook SpringBoard 

- (void)applicationDidFinishLaunching:(id)application { 

NSString *settingsPath = @"/var/mobile/Library/Preferences/com.ziph0n.vibrateonstart.plist"; 

NSMutableDictionary *prefs = [[NSMutableDictionary alloc] initWithContentsOfFile:settingsPath]; 

BOOL enabled = [[prefs objectForKey:@"enabled"] boolValue]; 

BOOL sound = [[prefs objectForKey:@"sound"] boolValue]; 

if (enabled) { 

    if (sound) { 

    %orig; 

    SystemSoundID mBeep; 

    NSString* path = [[NSBundle mainBundle] pathForResource:@"beep-beep" ofType:@"caf"];; 
    NSURL* url = [NSURL fileURLWithPath:path]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &mBeep); 

    AudioServicesPlaySystemSound(mBeep); 

    } 

    } 

} 

%end 

Мой аудио файл находится в папке RESSOURCES

ответ

0
// Import in your header file 
// #import <AVFoundation/AVAudioPlayer.h> 

@interface ViewController() 
@end 

@implementation ViewController { 
    AVAudioPlayer *beepSound; 
} 

-(void)viewDidLoad { 
    [super viewDidLoad]; 

    // Get file path 
    NSString *beepSoundFile = [[NSBundle mainBundle] pathForResource:@"beep-beep" ofType:@"caf"]; 
    // Create sound 
    beepSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:beepSoundFile] error:NULL]; 
    // Set the volume 
    beepSound.volume = 1.0; 
    // Load the sound so no lag when we want to play it 
    [beepSound prepareToPlay]; 
} 

Чтобы использовать звук:

// This will play the sound 
[beepSound play]; 

// This will stop the sound 
[beepSound stop]; 

// Restarts the sound at the beginning. Good for long sound files that you're going to use again 
beepSound.currentTime = 0; 

// Plays the sound in a loop infinitely 
beepSound.numberOfLoops = -1;