【问题标题】:Objective-C : How to control volume of multiple audio players in DiracLEObjective-C:如何在 DiracLE 中控制多个音频播放器的音量
【发布时间】:2016-08-22 18:56:49
【问题描述】:

我正在 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: 甚至对于单人游戏也不起作用,并且没有抛出异常。

如何解决?

【问题讨论】:

  • 你试过我的解决方案了吗?成功了吗?
  • @Elly_Philip 太糟糕了:/

标签: ios objective-c audio


【解决方案1】:

您也可以通过依次初始化 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;
    mTotalFramesInFile = 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,
                                  kAudioUnitScope_Input,
                                  kOutputBus,
                                  &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;
}

【讨论】:

  • 如果可能的话,我想要一个拒绝投票的理由。也许我可以提供更好的解决方案,或者编辑我的答案以使其更易于理解。谢谢
猜你喜欢
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 2021-04-09
  • 2013-01-20
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多