From d347e5e80c920652d797a3e6667a65632bbaba5a Mon Sep 17 00:00:00 2001 From: Thong Nguyen Date: Sat, 1 Feb 2014 23:24:06 +0000 Subject: [PATCH] Added mute/unmute support --- ExampleApp/ExampleApp/AudioPlayerView.h | 1 + ExampleApp/ExampleApp/AudioPlayerView.m | 26 ++++++++++-- StreamingKit/StreamingKit/STKAudioPlayer.h | 1 + StreamingKit/StreamingKit/STKAudioPlayer.m | 49 +++++++++++++++++++--- 4 files changed, 68 insertions(+), 9 deletions(-) diff --git a/ExampleApp/ExampleApp/AudioPlayerView.h b/ExampleApp/ExampleApp/AudioPlayerView.h index fff2561..92d06cb 100644 --- a/ExampleApp/ExampleApp/AudioPlayerView.h +++ b/ExampleApp/ExampleApp/AudioPlayerView.h @@ -52,6 +52,7 @@ UILabel* statusLabel; UISlider* slider; UISwitch* repeatSwitch; + UIButton* muteButton; UIButton* playButton; UIButton* stopButton; UIButton* playFromHTTPButton; diff --git a/ExampleApp/ExampleApp/AudioPlayerView.m b/ExampleApp/ExampleApp/AudioPlayerView.m index 70cb75d..ac3b71e 100644 --- a/ExampleApp/ExampleApp/AudioPlayerView.m +++ b/ExampleApp/ExampleApp/AudioPlayerView.m @@ -75,7 +75,7 @@ [queuePcmWaveFileFromHTTPButton addTarget:self action:@selector(queuePcmWaveFileButtonTouched) forControlEvents:UIControlEventTouchUpInside]; [queuePcmWaveFileFromHTTPButton setTitle:@"Queue PCM/WAVE from HTTP" forState:UIControlStateNormal]; - size = CGSizeMake(90, 50); + size = CGSizeMake(90, 40); playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; playButton.frame = CGRectMake(30, 380, size.width, size.height); @@ -86,6 +86,11 @@ [stopButton addTarget:self action:@selector(stopButtonPressed) forControlEvents:UIControlEventTouchUpInside]; [stopButton setTitle:@"Stop" forState:UIControlStateNormal]; + muteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; + muteButton.frame = CGRectMake((320 - size.width) - 30, 410, size.width, size.height); + [muteButton addTarget:self action:@selector(muteButtonPressed) forControlEvents:UIControlEventTouchUpInside]; + [muteButton setTitle:@"Mute" forState:UIControlStateNormal]; + slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 320, 280, 20)]; slider.continuous = YES; [slider addTarget:self action:@selector(sliderChanged) forControlEvents:UIControlEventValueChanged]; @@ -94,7 +99,7 @@ repeatSwitch = [[UISwitch alloc] initWithFrame:CGRectMake((320 - size.width) / 2, frame.size.height * 0.15 + 180, size.width, size.height)]; - label = [[UILabel alloc] initWithFrame:CGRectMake(0, slider.frame.origin.y + slider.frame.size.height, frame.size.width, 50)]; + label = [[UILabel alloc] initWithFrame:CGRectMake(0, slider.frame.origin.y + slider.frame.size.height, frame.size.width, 25)]; label.textAlignment = NSTextAlignmentCenter; @@ -103,7 +108,7 @@ statusLabel.textAlignment = NSTextAlignmentCenter; - meter = [[UIView alloc] initWithFrame:CGRectMake(0, 460, 0, 20)]; + meter = [[UIView alloc] initWithFrame:CGRectMake(0, 450, 0, 20)]; meter.backgroundColor = [UIColor greenColor]; @@ -118,6 +123,7 @@ [self addSubview:statusLabel]; [self addSubview:stopButton]; [self addSubview:meter]; + [self addSubview:muteButton]; [self setupTimer]; [self updateControls]; @@ -200,6 +206,20 @@ [self.delegate audioPlayerViewQueuePcmWaveFileSelected:self]; } +-(void) muteButtonPressed +{ + audioPlayer.muted = !audioPlayer.muted; + + if (audioPlayer.muted) + { + [muteButton setTitle:@"Unmute" forState:UIControlStateNormal]; + } + else + { + [muteButton setTitle:@"Mute" forState:UIControlStateNormal]; + } +} + -(void) stopButtonPressed { [audioPlayer stop]; diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.h b/StreamingKit/StreamingKit/STKAudioPlayer.h index ad49d88..2f7a43c 100644 --- a/StreamingKit/StreamingKit/STKAudioPlayer.h +++ b/StreamingKit/StreamingKit/STKAudioPlayer.h @@ -122,6 +122,7 @@ typedef void(^STKFrameFilter)(UInt32 channelsPerFrame, UInt32 bytesPerFrame, UIn @interface STKAudioPlayer : NSObject +@property (readwrite) BOOL muted; @property (readonly) double duration; @property (readonly) double progress; @property (readwrite) BOOL meteringEnabled; diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.m b/StreamingKit/StreamingKit/STKAudioPlayer.m index 28a4e53..201449e 100644 --- a/StreamingKit/StreamingKit/STKAudioPlayer.m +++ b/StreamingKit/StreamingKit/STKAudioPlayer.m @@ -80,6 +80,8 @@ @interface STKAudioPlayer() { + BOOL muted; + UInt8* readBuffer; int readBufferSize; @@ -1509,14 +1511,24 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn } } +-(BOOL) muted +{ + return self->muted; +} + +-(void) setMuted:(BOOL)value +{ + self->muted = value; +} + -(void) mute { - // TODO + self.muted = YES; } -(void) unmute { - // TODO + self.muted = NO; } -(void) dispose @@ -2039,7 +2051,8 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* OSSpinLockLock(&audioPlayer->pcmBufferSpinLock); BOOL waitForBuffer = NO; - STKQueueEntry* entry = audioPlayer->currentlyPlayingEntry; + BOOL muted = audioPlayer->muted; + STKQueueEntry* entry = audioPlayer->currentlyPlayingEntry; AudioBuffer* audioBuffer = audioPlayer->pcmAudioBuffer; UInt32 frameSizeInBytes = audioPlayer->pcmBufferFrameSizeInBytes; UInt32 used = audioPlayer->pcmBufferUsedFrameCount; @@ -2111,7 +2124,15 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* ioData->mBuffers[0].mNumberChannels = 2; ioData->mBuffers[0].mDataByteSize = frameSizeInBytes * framesToCopy; - memcpy(ioData->mBuffers[0].mData, audioBuffer->mData + (start * frameSizeInBytes), ioData->mBuffers[0].mDataByteSize); + + if (muted) + { + memset(ioData->mBuffers[0].mData, 0, ioData->mBuffers[0].mDataByteSize); + } + else + { + memcpy(ioData->mBuffers[0].mData, audioBuffer->mData + (start * frameSizeInBytes), ioData->mBuffers[0].mDataByteSize); + } totalFramesCopied = framesToCopy; @@ -2126,7 +2147,15 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* ioData->mBuffers[0].mNumberChannels = 2; ioData->mBuffers[0].mDataByteSize = frameSizeInBytes * framesToCopy; - memcpy(ioData->mBuffers[0].mData, audioBuffer->mData + (start * frameSizeInBytes), ioData->mBuffers[0].mDataByteSize); + + if (muted) + { + memset(ioData->mBuffers[0].mData, 0, ioData->mBuffers[0].mDataByteSize); + } + else + { + memcpy(ioData->mBuffers[0].mData, audioBuffer->mData + (start * frameSizeInBytes), ioData->mBuffers[0].mDataByteSize); + } UInt32 moreFramesToCopy = 0; UInt32 delta = inNumberFrames - framesToCopy; @@ -2137,7 +2166,15 @@ static OSStatus OutputRenderCallback(void* inRefCon, AudioUnitRenderActionFlags* ioData->mBuffers[0].mNumberChannels = 2; ioData->mBuffers[0].mDataByteSize += frameSizeInBytes * moreFramesToCopy; - memcpy(ioData->mBuffers[0].mData + (framesToCopy * frameSizeInBytes), audioBuffer->mData, frameSizeInBytes * moreFramesToCopy); + + if (muted) + { + memset(ioData->mBuffers[0].mData + (framesToCopy * frameSizeInBytes), 0, frameSizeInBytes * moreFramesToCopy); + } + else + { + memcpy(ioData->mBuffers[0].mData + (framesToCopy * frameSizeInBytes), audioBuffer->mData, frameSizeInBytes * moreFramesToCopy); + } } totalFramesCopied = framesToCopy + moreFramesToCopy;