1

我试图多次改变音频文件的音高,以向用户指示状态的变化。我不确定如何在objective-c中做到这一点

我发现这篇文章是关于一个讨论它的问题,但它是为了快速。另外,我不确定如何使用这个库加载音频文件。

我尝试了这段代码,但每次运行它时,都会因为第 3-4 行而出错。另一件事,因为我是新手,我不确定如何使用这个库加载 mp3 文件。有人可以帮忙吗?

//This is the file I need to shift its pitch
NSString *path = [NSString stringWithFormat:@"%@/Beep5.mp3", [[NSBundle mainBundle] resourcePath]];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
AVAudioFile *file=[[AVAudioFile alloc] initForReading:soundUrl error:nil];
AVAudioFormat *format=file.processingFormat;
AVAudioFrameCount capacity= (AVAudioFrameCount)file.length;
AVAudioPCMBuffer *buffer=[[AVAudioPCMBuffer alloc] initWithPCMFormat:format frameCapacity:capacity];
[file readIntoBuffer:buffer error:nil];
[playerNode scheduleBuffer:buffer completionHandler:nil];


AVAudioEngine *engine;
AVAudioPlayerNode *playerNode;

//get the error in one of the following two LOC
engine = [[AVAudioPlayerNode alloc] init];
[engine attachNode: playerNode];

AVAudioMixerNode *mixer = engine.mainMixerNode;
AVAudioUnitTimePitch *auTimePitch;
auTimePitch.pitch=1200.0;// In cents. The default value is 1.0. The range of values is -2400 to 2400
auTimePitch.rate = 2.0; //The default value is 1.0. The range of supported values is 1/32 to 32.0.
[engine attachNode: auTimePitch];
[engine connect:playerNode to:auTimePitch format:[mixer outputFormatForBus:0]];
[engine connect:playerNode to:mixer format:[mixer outputFormatForBus:0]];
    playerNode.play;
4

2 回答 2

2

请添加以下标题并尝试 -

#import <AVFoundation/AVAudioPlayerNode.h>
#import <AVFoundation/AVAudioEngine.h>

engine = [[AVAudioEngine alloc] init];

playerNode = [[AVAudioPlayerNode alloc] init];
于 2015-04-29T21:30:23.780 回答
0

您的代码看起来不错,但您跳过了 AVAudioPlayerNode 和 AVAudioEngine 的新对象的创建。

这应该看起来像这样

     @interface ViewController (){

            AVAudioEngine *engine;
        }
        AVAudioPlayerNode* node = [AVAudioPlayerNode new];    
        engine= [[AVAudioEngine alloc] init];

以及添加这些行后的完整代码。

    //AVAudioEngine *engine should be declare in @interface section

    @interface ViewController (){

            AVAudioEngine *engine;
        }

- (void)viewDidLoad {    
    [super viewDidLoad];
     NSString* path=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp3"];
        NSURL *soundUrl = [NSURL fileURLWithPath:path];
        AVAudioFile* File = [[AVAudioFile alloc] initForReading: soundUrl error: nil];
        AVAudioPlayerNode* node = [AVAudioPlayerNode new];
        engine= [[AVAudioEngine alloc] init];

        [node stop];
        [engine stop];
        [engine reset];
        [engine attachNode: node];
        AVAudioUnitTimePitch* changeAudioUnitTime = [AVAudioUnitTimePitch new];
        changeAudioUnitTime.rate = 1;
        changeAudioUnitTime.pitch = 1000;
        [engine attachNode: changeAudioUnitTime];
        [engine connect:node to: changeAudioUnitTime format: nil];
        [engine connect:changeAudioUnitTime to: engine.outputNode format:nil];
        [node scheduleFile:File atTime: nil completionHandler: nil];
        [engine startAndReturnError: nil];
        [node play];
    }

注意 :: 此代码在实际设备上进行了测试,并且在我的项目中运行良好。不要忘记添加 CoreAudio.framework , AVFoundation.framework。

于 2017-09-05T17:50:01.093 回答