ios – Objective-C:如何控制DiracLE中多个音频播放器的音量

前端之家收集整理的这篇文章主要介绍了ios – Objective-C:如何控制DiracLE中多个音频播放器的音量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在Objective C中创建一个音乐应用程序.我需要同时播放多个声音,我可以使用DiracLE音频播放器来实现这一点.

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

我的DiracAudioPlayer初始化代码

  1. DiracAudioPlayer *player1,*player2;
  2. NSURL *url1,*url2;
  3. NSError *error = nil;
  4.  
  5. url1 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"song1" ofType:@"mp3"]];
  6. url2 = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"song2" ofType:@"mp3"]];
  7.  
  8. player1 = [[DiracAudioPlayer alloc] initWithContentsOfURL:mainURL channels:1 error:&error];
  9. [player1 setDelegate:self];
  10. player2 = [[DiracAudioPlayer alloc] initWithContentsOfURL:mainURL channels:1 error:&error];
  11. [player2 setDelegate:self];

设置播放器音量的方法

  1. -(void)setPlayerVolume
  2. {
  3. [player1 setVolume:0.5];
  4. [player2 setVolume:0.2];
  5. }

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

如何解决这个问题?

解决方法

您还可以通过依次初始化2个AVAudioPlayers并使用[self.player setVolume:volumeFloat]来控制音量来实现此目的.
  1. ...
  2.  
  3. NSString *songA = [[NSBundle mainBundle] pathForResource:@"songA" ofType:@"mp3"];
  4. NSError *soundError = nil;
  5. self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:songA] error:&soundError];
  6. if(self.player == nil)
  7. NSLog(@"%@",soundError);
  8. else
  9. {
  10. [self.player setDelegate:self];
  11. [self.player setVolume:0.75];
  12. [self.player play];
  13. }
  14.  
  15. NSString *songB = [[NSBundle mainBundle] pathForResource:@"songB" ofType:@"mp3"];
  16. soundError = nil;
  17. self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:songB] error:&soundError];
  18. if(self.player == nil)
  19. NSLog(@"%@",soundError);
  20. else
  21. {
  22. [self.player setDelegate:self];
  23. [self.player setVolume:0.25];
  24. [self.player play];
  25. }
  26.  
  27. ...

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

  1. -(void)setupInstanceWithUrl:(NSURL*)inUrl numChannels:(int)channels volume:(float)volume
  2. {
  3. mDelegate = nil;
  4. mInUrl = [inUrl copy];
  5. mIsPrepared = NO;
  6. mIsProcessing = NO;
  7. mWorkerThread = nil;
  8. mTotalFramesInFile = 0;
  9. mIsRunning = NO;
  10. mVolume = volume;
  11. mLoopCount = mNumberOfLoops = 0;
  12. mHasFinishedPlaying = YES;
  13.  
  14. if (channels < 1) channels = 1;
  15. else if (channels > 2) channels = 2;
  16. mNumChannels = channels;
  17.  
  18. mPeak = new SInt16[mNumChannels];
  19. mPeakOut = new SInt16[mNumChannels];
  20.  
  21. for (long v = 0; v < mNumChannels; v++) {
  22. mPeakOut[v] = 0;
  23. mPeak[v] = -1;
  24. }
  25.  
  26. OSStatus status = noErr;
  27. mTimeFactor = 1./kOversample;
  28. mPitchFactor = kOversample;
  29. // This is boilerplate code to set up CoreAudio on iOS in order to play audio via its default output
  30.  
  31. // Desired audio component
  32. AudioComponentDescription desc;
  33. desc.componentType = kAudioUnitType_Output;
  34. #if TARGET_OS_IPHONE
  35. desc.componentSubType = kAudioUnitSubType_RemoteIO;
  36. #else
  37. desc.componentSubType = kAudioUnitSubType_HALOutput;
  38. #endif
  39. desc.componentManufacturer = kAudioUnitManufacturer_Apple;
  40. desc.componentFlags = 0;
  41. desc.componentFlagsMask = 0;
  42.  
  43. // Get ref to component
  44. AudioComponent defaultOutput = AudioComponentFindNext(NULL,&desc);
  45.  
  46. // Get matching audio unit
  47. status = AudioComponentInstanceNew(defaultOutput,&mAudioUnit);
  48. checkStatus(status);
  49.  
  50. // this is the format we want
  51. AudioStreamBasicDescription audioFormat;
  52. mSampleRate=audioFormat.mSampleRate = 44100.00;
  53. audioFormat.mFormatID = kAudioFormatLinearPCM;
  54. audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
  55. audioFormat.mFramesPerPacket = 1;
  56. audioFormat.mChannelsPerFrame = mNumChannels;
  57. audioFormat.mBitsPerChannel = 16;
  58. audioFormat.mBytesPerPacket = sizeof(short)*mNumChannels;
  59. audioFormat.mBytesPerFrame = sizeof(short)*mNumChannels;
  60.  
  61. status = AudioUnitSetProperty(mAudioUnit,kAudioUnitProperty_StreamFormat,kAudioUnitScope_Input,kOutputBus,&audioFormat,sizeof(audioFormat));
  62. checkStatus(status);
  63.  
  64. // here we set up CoreAudio in order to call our PlaybackCallback
  65. AURenderCallbackStruct callbackStruct;
  66. callbackStruct.inputProc = PlaybackCallback;
  67. callbackStruct.inputProcRefCon = (__bridge void*) self;
  68. status = AudioUnitSetProperty(mAudioUnit,kAudioUnitProperty_SetRenderCallback,&callbackStruct,sizeof(callbackStruct));
  69. checkStatus(status);
  70.  
  71.  
  72. // Initialize unit
  73. status = AudioUnitInitialize(mAudioUnit);
  74. checkStatus(status);
  75.  
  76. // here we allocate our audio cache
  77. mAudioBuffer = AllocateAudioBufferSInt16(mNumChannels,kAudioBufferNumFrames);
  78.  
  79. // Avoid delay when hitting play by making sure the graph is pre-initialized
  80. AudioOutputUnitStart(mAudioUnit);
  81. AudioOutputUnitStop(mAudioUnit);
  82.  
  83. [self prepareToPlay];
  84.  
  85. }

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

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

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

  1. -(void)addVolumeObserver {
  2.  
  3. MPVolumeView *volumeView = [MPVolumeView new];
  4. volumeView.showsRouteButton = NO;
  5. volumeView.showsVolumeSlider = NO;
  6. AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
  7. [appDelegate.window.rootViewController.view addSubview:volumeView];
  8.  
  9.  
  10. __weak __typeof(self)weakSelf = self;
  11. [[volumeView subviews] enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop) {
  12. if ([obj isKindOfClass:[UiSlider class]]) {
  13. __strong __typeof(weakSelf)strongSelf = weakSelf;
  14. strongSelf->volumeSlider = obj;
  15. [obj addTarget:strongSelf action:@selector(handleVolumeChanged:) forControlEvents:UIControlEventValueChanged];
  16. *stop = YES;
  17. }
  18. }];
  19. }
  20.  
  21. - (void)handleVolumeChanged:(id)sender
  22. {
  23. NSLog(@"Volume: %f",volumeSlider.value);
  24. }
  25.  
  26. - (void)setVolumeHandlerTo:(float)volume
  27. {
  28. volumeSlider.value = volume;
  29. }

猜你在找的iOS相关文章