首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >新的iOS 8的麻烦

新的iOS 8的麻烦
EN

Stack Overflow用户
提问于 2014-06-14 18:46:10
回答 1查看 1.1K关注 0票数 1

我正在尝试学习新的CoreAudio iOS 8的API,并且似乎无法在我的设备上产生任何声音。我正在使用来自WWDC会话502的代码,另外,我认为启动音频会话是个好主意。

代码语言:javascript
复制
#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>

@interface AppDelegate ()


@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions {
// Override point for customization after application launch.
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error;
[session setCategory:AVAudioSessionCategoryPlayback error:&error];


AVAudioEngine *engine = [[AVAudioEngine alloc] init];
AVAudioPlayerNode *player = [[AVAudioPlayerNode alloc] init];
[engine attachNode:player];

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"mySound" withExtension:@"aif"];
AVAudioFile *file = [[AVAudioFile alloc] initForReading:fileURL error:&error];
if (error) {
    NSLog(@"error getting audio file");
}

AVAudioMixerNode *mainMixer = [engine mainMixerNode];
// just to be safe
mainMixer.outputVolume = 1;
[engine connect:player to:mainMixer format:file.processingFormat];
[player scheduleFile:file atTime:nil completionHandler:nil];

if ([engine startAndReturnError:&error]) {
    NSLog(@"engine succsessful %@", error);
} else {
    NSLog(@"error starting engine: %@", error);
}
[player play];




return YES;

}

我遗漏了什么?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-14 20:20:31

engine在你听到任何消息之前就被释放了。将引擎添加为类成员,这样,一旦didFinishLaunching返回,它就不会被丢弃

代码语言:javascript
复制
#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>

@interface AppDelegate ()
    AVAudioEngine *engine;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions {
// Override point for customization after application launch.
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error;
[session setCategory:AVAudioSessionCategoryPlayback error:&error];


engine = [[AVAudioEngine alloc] init];
AVAudioPlayerNode *player = [[AVAudioPlayerNode alloc] init];
[engine attachNode:player];

NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"mySound" withExtension:@"aif"];
AVAudioFile *file = [[AVAudioFile alloc] initForReading:fileURL error:&error];
if (error) {
    NSLog(@"error getting audio file");
}

AVAudioMixerNode *mainMixer = [engine mainMixerNode];
// just to be safe
mainMixer.outputVolume = 1;
[engine connect:player to:mainMixer format:file.processingFormat];
[player scheduleFile:file atTime:nil completionHandler:nil];

if ([engine startAndReturnError:&error]) {
    NSLog(@"engine succsessful %@", error);
} else {
    NSLog(@"error starting engine: %@", error);
}
[player play];

return YES;
}

AVAudioSession是不必要的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24223111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档