Added mute/unmute support

This commit is contained in:
Thong Nguyen 2014-02-01 23:24:06 +00:00
parent 4d73784338
commit d347e5e80c
4 changed files with 68 additions and 9 deletions

View File

@ -52,6 +52,7 @@
UILabel* statusLabel;
UISlider* slider;
UISwitch* repeatSwitch;
UIButton* muteButton;
UIButton* playButton;
UIButton* stopButton;
UIButton* playFromHTTPButton;

View File

@ -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];

View File

@ -122,6 +122,7 @@ typedef void(^STKFrameFilter)(UInt32 channelsPerFrame, UInt32 bytesPerFrame, UIn
@interface STKAudioPlayer : NSObject<STKDataSourceDelegate>
@property (readwrite) BOOL muted;
@property (readonly) double duration;
@property (readonly) double progress;
@property (readwrite) BOOL meteringEnabled;

View File

@ -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;