Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Class/MPVolumeObserver.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// Created by tashigaofei on 13/08/26.
// Change by LJT on 17/01/15

#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>

@class MPVolumeObserver;
@protocol MPVolumeObserverProtocol <NSObject>
-(void) volumeButtonDidClick:(MPVolumeObserver *) button;
-(void) volumeButtonCameraClick:(MPVolumeObserver *) button;
-(void) volumeButtonStarVideoClick:(MPVolumeObserver *) button;
-(void) volumeButtonEndVideoClick:(MPVolumeObserver *) button;


@end


Expand Down
173 changes: 128 additions & 45 deletions Class/MPVolumeObserver.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@

// Created by tashigaofei on 13/08/26.
//

#import "MPVolumeObserver.h"


@interface MPVolumeObserver()
{
UIView *_volumeView;
float launchVolume;
BOOL _isObservingVolumeButtons;
BOOL _suspended;
MPVolumeView *_volumeView;
float launchVolume;
BOOL _isObservingVolumeButtons;
BOOL _suspended;
int Isfirst;
NSString *strNowVolume;
NSTimer *timeVideoBtn;
NSInteger secondsElapsed;
BOOL isVideoStar;
BOOL isVideoEnd;

float fVolume;
}

@end

@implementation MPVolumeObserver
Expand All @@ -33,42 +38,41 @@ -(id)init
if( self ){
_isObservingVolumeButtons = NO;
_suspended = NO;
CGRect frame = CGRectMake(0, -100, 0, 0);
_volumeView = [[MPVolumeView alloc] initWithFrame:frame];
[[UIApplication sharedApplication].windows[0] addSubview:_volumeView];

Isfirst = 0;
secondsElapsed = 0;

}
return self;
}

-(void)startObserveVolumeChangeEvents
{
double delayInSeconds = 0.25;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self startObserve];
});
_suspended = NO;
isVideoStar = YES;
isVideoEnd = NO;
fVolume = [self volume];
[self startObserve];
}

-(void) startObserve;
{
if(_isObservingVolumeButtons) {
NSLog(@"start add notification");
if(_isObservingVolumeButtons)
{
return;
}

_isObservingVolumeButtons = YES;
AudioSessionInitialize(NULL, NULL, NULL, NULL);
SInt32 process = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(process), &process);
AudioSessionSetActive(YES);

launchVolume = [[MPMusicPlayerController applicationMusicPlayer] volume];
launchVolume = launchVolume == 0 ? 0.05 : launchVolume;
launchVolume = launchVolume == 1 ? 0.95 : launchVolume;
if (launchVolume == 0.05 || launchVolume == 0.95) {
[[MPMusicPlayerController applicationMusicPlayer] setVolume:launchVolume];
CGRect frame = CGRectMake(0, -100, 0, 0);
_volumeView = [[MPVolumeView alloc] initWithFrame:frame];
[[UIApplication sharedApplication].windows[0] addSubview:_volumeView];
strNowVolume = [[ NSString stringWithFormat:@"%f",[self volume] ] substringToIndex:4];
strNowVolume = [strNowVolume isEqual: @"0.00"] ? @"0.05" : strNowVolume;
strNowVolume = [strNowVolume isEqual: @"1.00"] ? @"0.95" : strNowVolume;
if ([strNowVolume isEqual: @"0.05"] || [strNowVolume isEqualToString:@"0.95"])
{
[self setVolume:[strNowVolume floatValue]];
}

if (!_suspended)
{
[[NSNotificationCenter defaultCenter] addObserver:self
Expand All @@ -84,8 +88,6 @@ -(void) startObserve;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChangeNotification:)
name:@"SystemVolumeDidChange" object:nil];
}


}

-(void) volumeChangeNotification:(NSNotification *) no
Expand All @@ -95,17 +97,74 @@ -(void) volumeChangeNotification:(NSNotification *) no
sender = no.object;
}

if (no.object != sender || [[no.userInfo objectForKey:@"AudioVolume"] floatValue] == launchVolume) {
NSString * NowChangeVolume = [[ NSString stringWithFormat:@"%f",[[no.userInfo objectForKey:@"AudioVolume"] floatValue] ] substringToIndex:4];
if (no.object != sender || [NowChangeVolume isEqualToString: strNowVolume]) {
return;
}

[[MPMusicPlayerController applicationMusicPlayer] setVolume:launchVolume];

if ([_delegate respondsToSelector:@selector(volumeButtonDidClick:)]) {
[_delegate volumeButtonDidClick:self];
[self setVolume:fVolume];
if(timeVideoBtn)
{
if(secondsElapsed == 1)
{
if (isVideoStar)
{
NSLog(@"start video");
if ([self.delegate respondsToSelector:@selector(volumeButtonStarVideoClick:)]) {
[self.delegate volumeButtonStarVideoClick:self];
}
isVideoStar = NO;
isVideoEnd = YES;
}
}
secondsElapsed ++;
[timeVideoBtn invalidate];
}

// NSLog(@"\n\n%@\n\n", no);
timeVideoBtn = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(onTimeFire)
userInfo:nil
repeats:NO];
}

-(void)onTimeFire
{

timeVideoBtn = nil;

if (secondsElapsed > 2)
{
if (isVideoEnd) {
secondsElapsed = 0;
NSLog(@"end video");
if ([self.delegate respondsToSelector:@selector(volumeButtonEndVideoClick:)]) {
[self.delegate volumeButtonEndVideoClick:self];
isVideoEnd = NO;
}
}
}
else
{
double delayInSeconds = 0.65;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self tackPhoto];
});
}
}




- (void)tackPhoto
{
if (isVideoStar)
{
NSLog(@"take Photo");
if ([self.delegate respondsToSelector:@selector(volumeButtonCameraClick:)]) {
[self.delegate volumeButtonCameraClick:self];
}
}
}


Expand All @@ -115,6 +174,7 @@ - (void)suspendObserveVolumeChangeEvents:(NSNotification *)notification
{
_suspended = YES; // Call first!
[self stopObserveVolumeChangeEvents];
Isfirst = 0;
}
}

Expand All @@ -123,6 +183,7 @@ - (void)resumeObserveVolumeButtonEvents:(NSNotification *)notification
if(_suspended)
{
[self startObserveVolumeChangeEvents];
Isfirst = 0;
_suspended = NO; // Call last!
}
}
Expand All @@ -134,20 +195,42 @@ -(void)stopObserveVolumeChangeEvents
return;
}

if (!_suspended){
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
AudioSessionRemovePropertyListenerWithUserData(kAudioSessionProperty_CurrentHardwareOutputVolume, NULL, self);
AudioSessionSetActive(NO);

[[NSNotificationCenter defaultCenter]removeObserver:self name:@"SystemVolumeDidChange" object:nil];

Isfirst = 0;
[timeVideoBtn invalidate];
secondsElapsed = 0;
_isObservingVolumeButtons = NO;

}

- (float)volume
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
CGFloat volume = audioSession.outputVolume;
return volume;
}

- (void)setVolume:(float)newVolume
{
MPVolumeView* volumeView = [[MPVolumeView alloc] init];

//find the volumeSlider
UISlider* volumeViewSlider = nil;
for (UIView *view in [volumeView subviews]){
if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
volumeViewSlider = (UISlider*)view;
break;
}
}

[volumeViewSlider setValue:newVolume animated:YES];
[volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside];
}
-(void)dealloc
{
_suspended = NO;
[super dealloc];
_suspended = NO;
Isfirst = 0;
}

@end
24 changes: 22 additions & 2 deletions MPLovumeObserveDemo/MPLovumeObserveDemo.xcodeproj/project.pbxproj
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
819FF5F117D5F0A500E5A227 /* MPVolumeObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = 819FF5F017D5F0A500E5A227 /* MPVolumeObserver.m */; };
819FF5F317D5F0B900E5A227 /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 819FF5F217D5F0B900E5A227 /* AudioToolbox.framework */; };
819FF5F717D5F0DE00E5A227 /* MediaPlayer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 819FF5F617D5F0DE00E5A227 /* MediaPlayer.framework */; };
D0680E6E1E2DFD2700B8B0EB /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0680E6D1E2DFD2700B8B0EB /* AVFoundation.framework */; };
D0680E711E2DFD7C00B8B0EB /* PushVC.m in Sources */ = {isa = PBXBuildFile; fileRef = D0680E701E2DFD7C00B8B0EB /* PushVC.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -42,13 +44,17 @@
819FF5F017D5F0A500E5A227 /* MPVolumeObserver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPVolumeObserver.m; sourceTree = "<group>"; };
819FF5F217D5F0B900E5A227 /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = System/Library/Frameworks/AudioToolbox.framework; sourceTree = SDKROOT; };
819FF5F617D5F0DE00E5A227 /* MediaPlayer.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MediaPlayer.framework; path = System/Library/Frameworks/MediaPlayer.framework; sourceTree = SDKROOT; };
D0680E6D1E2DFD2700B8B0EB /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
D0680E6F1E2DFD7C00B8B0EB /* PushVC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PushVC.h; sourceTree = "<group>"; };
D0680E701E2DFD7C00B8B0EB /* PushVC.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PushVC.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
819FF5C117D5F01200E5A227 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D0680E6E1E2DFD2700B8B0EB /* AVFoundation.framework in Frameworks */,
819FF5F717D5F0DE00E5A227 /* MediaPlayer.framework in Frameworks */,
819FF5F317D5F0B900E5A227 /* AudioToolbox.framework in Frameworks */,
819FF5C817D5F01200E5A227 /* UIKit.framework in Frameworks */,
Expand Down Expand Up @@ -80,6 +86,7 @@
819FF5C617D5F01200E5A227 /* Frameworks */ = {
isa = PBXGroup;
children = (
D0680E6D1E2DFD2700B8B0EB /* AVFoundation.framework */,
819FF5F617D5F0DE00E5A227 /* MediaPlayer.framework */,
819FF5F217D5F0B900E5A227 /* AudioToolbox.framework */,
819FF5C717D5F01200E5A227 /* UIKit.framework */,
Expand All @@ -97,6 +104,8 @@
819FF5D717D5F01200E5A227 /* AppDelegate.m */,
819FF5DF17D5F01200E5A227 /* ViewController.h */,
819FF5E017D5F01200E5A227 /* ViewController.m */,
D0680E6F1E2DFD7C00B8B0EB /* PushVC.h */,
D0680E701E2DFD7C00B8B0EB /* PushVC.m */,
819FF5CE17D5F01200E5A227 /* Supporting Files */,
);
path = MPLovumeObserveDemo;
Expand Down Expand Up @@ -154,6 +163,11 @@
attributes = {
LastUpgradeCheck = 0460;
ORGANIZATIONNAME = baidu;
TargetAttributes = {
819FF5C317D5F01200E5A227 = {
DevelopmentTeam = WEFT5XRB8Z;
};
};
};
buildConfigurationList = 819FF5BF17D5F01200E5A227 /* Build configuration list for PBXProject "MPLovumeObserveDemo" */;
compatibilityVersion = "Xcode 3.2";
Expand Down Expand Up @@ -194,6 +208,7 @@
819FF5D417D5F01200E5A227 /* main.m in Sources */,
819FF5D817D5F01200E5A227 /* AppDelegate.m in Sources */,
819FF5E117D5F01200E5A227 /* ViewController.m in Sources */,
D0680E711E2DFD7C00B8B0EB /* PushVC.m in Sources */,
819FF5F117D5F0A500E5A227 /* MPVolumeObserver.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -269,14 +284,16 @@
819FF5E817D5F01200E5A227 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES;
DEVELOPMENT_TEAM = WEFT5XRB8Z;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"\"$(SDKROOT)$(SYSTEM_LIBRARY_DIR)/PrivateFrameworks\"",
);
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "MPLovumeObserveDemo/MPLovumeObserveDemo-Prefix.pch";
INFOPLIST_FILE = "MPLovumeObserveDemo/MPLovumeObserveDemo-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -285,14 +302,16 @@
819FF5E917D5F01200E5A227 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES;
DEVELOPMENT_TEAM = WEFT5XRB8Z;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"\"$(SDKROOT)$(SYSTEM_LIBRARY_DIR)/PrivateFrameworks\"",
);
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "MPLovumeObserveDemo/MPLovumeObserveDemo-Prefix.pch";
INFOPLIST_FILE = "MPLovumeObserveDemo/MPLovumeObserveDemo-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
Expand All @@ -317,6 +336,7 @@
819FF5E917D5F01200E5A227 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
Empty file.
Empty file.
Binary file not shown.
Empty file.
Empty file.
Empty file.
Loading