1

我想用 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 文件夹中

4

1 回答 1

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;
于 2015-02-26T18:28:53.370 回答