HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – Objective-C:如何控制DiracLE中多个音频播放器的音量大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在Objective C中创建一个音乐应用程序.我需要同时播放多个声音,我可以使用DiracLE音频播放器来实现这一点.

但是这个库的音量控制方法 – (void)setVolume:(float)volume;似乎没有工作.

我的DiracAudioPlayer初始化代码

DiracAudioPlayer *player1,*player2;
NSURL *url1,*url2;
NSError *error = nil;

url1 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForresource:@"song1" ofType:@"mp3"]];
url2 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForresource:@"song2" ofType:@"mp3"]];

player1 = [[DiracAudioPlayer alloc] initWithContentsOfURL:mainURL chAnnels:1 error:&error];
[player1 setDelegate:self];
player2 = [[DiracAudioPlayer alloc] initWithContentsOfURL:mainURL chAnnels:1 error:&error];
[player2 setDelegate:self];

设置播放器音量的方法

-(void)setPlayerVolume
{
   [player1 setVolume:0.5];
   [player2 setVolume:0.2];
}

setVolume:即使对于单个播放器也不起作用,也没有抛出异常.

如何解决这个问题?

解决方法

您还可以通过依次初始化2个AVAudioPlayers并使用[self.player setVolume:volumeFloat]来控制音量来实现此目的.
...   

    NSString *songA = [[NSBundle mainBundle] pathForresource:@"songA" ofType:@"mp3"];
    NSError *soundError = nil;
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:songA] error:&soundError];
    if(self.player == nil)
        NSLog(@"%@",soundError);
    else
    {
        [self.player setDelegate:self];
        [self.player setVolume:0.75];
        [self.player play];
    }

    NSString *songB = [[NSBundle mainBundle] pathForresource:@"songB" ofType:@"mp3"];
    soundError = nil;
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:songB] error:&soundError];
    if(self.player == nil)
        NSLog(@"%@",soundError);
    else
    {
        [self.player setDelegate:self];
        [self.player setVolume:0.25];
        [self.player play];
    }

...

如果您决定使用DiracAudioPlayer,则可以使用自己的方法使用DiracAudioPlayerBase.mm中的正确卷参数重新初始化AudioUnit:

-(void)setupInstanceWithUrl:(NSURL*)inUrl numChAnnels:(int)chAnnels volume:(float)volume
{
    mDelegate = nil;
    mInUrl = [inUrl copy];
    mIsPrepared = NO;
    mIsProcessing = NO;
    mWorkerThread = nil;
    m@R_686_10586@lFramesInFile = 0;
    mIsRunning = NO;
    mVolume = volume;
    mLoopCount = mnumberOfLoops = 0;
    mHasFinishedPlaying = YES;

    if (chAnnels < 1) chAnnels = 1;
    else if (chAnnels > 2) chAnnels = 2;
    mNumChAnnels = chAnnels;

    mPeak = new SInt16[mNumChAnnels];
    mPeakOut = new SInt16[mNumChAnnels];

    for (long v = 0; v < mNumChAnnels; v++) {
        mPeakOut[v] = 0;
        mPeak[v] = -1;
    }

    OSStatus status = noErr;
    mTimeFactor = 1./kOversample;
    mPitchFactor = kOversample;
    // This is boilerplate code to set up CoreAudio on iOS in order to play audio via its default output

    // Desired audio component
    AudioComponentDescription desc;
    desc.componentType = kAudioUnitType_Output;
#if TARGET_OS_IPHONE
    desc.componentSubType = kAudioUnitSubType_RemoteIO;
#else
    desc.componentSubType = kAudioUnitSubType_HALOutput;
#endif
    desc.componentManufacturer = kAudioUnitManufacturer_Apple;
    desc.componentFlags = 0;
    desc.componentFlagsmask = 0;

    // Get ref to component
    AudioComponent defaultOutput = AudioComponentFindNext(NULL,&desc);

    // Get matching audio unit
    status = AudioComponenTinstanceNew(defaultOutput,&mAudioUnit);
    checkStatus(status);

    // this is the format we want
    AudioStreamBasicDescription audioFormat;
    mSampleRate=audioFormat.mSampleRate         = 44100.00;
    audioFormat.mFormatID           = kAudioFormatLinearPCM;
    audioFormat.mFormatFlags        = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
    audioFormat.mFramesPerPacket    = 1;
    audioFormat.mChAnnelsPerFrame   = mNumChAnnels;
    audioFormat.mBitsPerChAnnel     = 16;
    audioFormat.mBytesPerPacket     = sizeof(short)*mNumChAnnels;
    audioFormat.mBytesPerFrame      = sizeof(short)*mNumChAnnels;

    status = AudioUnitSetProperty(mAudioUnit,kAudioUnitProperty_StreamFormat,kAudioUnitScope_Input,kOutputBus,&audioFormat,sizeof(audioFormat));
    checkStatus(status);

    // here we set up CoreAudio in order to call our PlayBACkCallBACk
    AURenderCallBACkStruct callBACkStruct;
    callBACkStruct.inputProc = PlayBACkCallBACk;
    callBACkStruct.inputProcRefCon = (__bridge void*) self;
    status = AudioUnitSetProperty(mAudioUnit,kAudioUnitProperty_SetRenderCallBACk,&callBACkStruct,sizeof(callBACkStruct));
    checkStatus(status);


    // Initialize unit
    status = AudioUniTinitialize(mAudioUnit);
    checkStatus(status);

    // here we allocate our audio cache
    mAudioBuffer = AllocateAudioBufferSInt16(mNumChAnnels,kAudioBufferNumFrames);

    // Avoid delay when hitTing play by making sure the graph is pre-initialized
    AudioOutputUnitStart(mAudioUnit);
    AudioOutputUnitStop(mAudioUnit);

    [self prepareToPlay];

}

仔细检查您的卷是否通过记录设置:

NSLog(@"Volume P1: %f",[player1 volume]);
NSLog(@"Volume P2: %f",[player2 volume]);

此外,您还可以通过硬件音量按钮使用的相同方法来控制混音的输出音量:(这将启动iOS音量更改的UI)

-(void)addVolumeObserver {

    MPVolumeView *volumeView = [MPVolumeView new];
    volumeView.showsRouteButton = NO;
    volumeView.showsVolumeSlider = NO;
    AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    [appDelegate.window.rootViewController.view addSubview:volumeView];


    __weak __typeof(self)weakSelf = self;
    [[volumeView subviews] enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop) {
        if ([obj isKindOfClass:[UiSlider class]]) {
            __strong __typeof(weakSelf)strongSelf = weakSelf;
            strongSelf->volumeSlider = obj;
            [obj addTarget:strongSelf action:@SELEctor(handleVolumeChanged:) forControlEvents:UIControlEventValueChanged];
            *stop = YES;
        }
    }];
}

- (void)handleVolumeChanged:(id)sender
{
    NSLog(@"Volume: %f",volumeSlider.value);
}

- (void)setVolumeHandlerTo:(float)volume
{
    volumeSlider.value = volume;
}

大佬总结

以上是大佬教程为你收集整理的ios – Objective-C:如何控制DiracLE中多个音频播放器的音量全部内容,希望文章能够帮你解决ios – Objective-C:如何控制DiracLE中多个音频播放器的音量所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。