forked from mplusp/MMPDeepSleepPreventer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMPDeepSleepPreventer.m
183 lines (147 loc) · 6.25 KB
/
MMPDeepSleepPreventer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// MMPDeepSleepPreventer.m
// MMPDeepSleepPreventer
//
// Created by Marco Peluso on 20.08.09.
// Copyright (c) 2009-2010, Marco Peluso - marcopeluso.com
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holders nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#pragma mark -
#pragma mark Imports
#import "MMPDeepSleepPreventer.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
#pragma mark -
#pragma mark MMPDeepSleepPreventer Private Interface
@interface MMPDeepSleepPreventer ()
- (void)mmp_playPreventSleepSound;
- (void)mmp_setUpAudioSession;
@end
@implementation MMPDeepSleepPreventer
#pragma mark -
#pragma mark Synthesizes
@synthesize audioPlayer = audioPlayer_;
#pragma mark -
#pragma mark Creation and Destruction
- (id)init
{
if ( !(self = [super init]) )
{
return nil;
}
[self mmp_setUpAudioSession];
// Set up path to sound file
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"MMPSilence"
ofType:@"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
// Set up audio player with sound file
audioPlayer_ = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL
error:nil];
[fileURL release];
[self.audioPlayer prepareToPlay];
// You may want to set this to 0.0 even if your sound file is silent.
// I don't know exactly, if this affects battery life, but it can't hurt.
[self.audioPlayer setVolume:0.0];
//set the infinite loop, stop is triggered by stopPreventSleep method
[self.audioPlayer setNumberOfLoops:-1];
return self;
}
- (void)dealloc
{
[audioPlayer_ release];
[super dealloc];
}
#pragma mark -
#pragma mark Public Methods
- (void)startPreventSleep
{
[self mmp_playPreventSleepSound];
}
- (void)stopPreventSleep
{
[self.audioPlayer stop];
}
- (BOOL)isPreventingSleep
{
return [self.audioPlayer isPlaying];
}
#pragma mark -
#pragma mark Private Methods
- (void)mmp_playPreventSleepSound
{
[self.audioPlayer play];
}
- (void)mmp_setUpAudioSession
{
// Initialize audio session
AudioSessionInitialize
(
NULL, // Use NULL to use the default (main) run loop.
NULL, // Use NULL to use the default run loop mode.
NULL, // A reference to your interruption listener callback function.
// See “Responding to Audio Session Interruptions” in Apple's "Audio Session Programming Guide" for a description of how to write
// and use an interruption callback function.
NULL // Data you intend to be passed to your interruption listener callback function when the audio session object invokes it.
);
// Activate audio session
OSStatus activationResult = 0;
activationResult = AudioSessionSetActive(true);
if (activationResult)
{
MMPDLog(@"AudioSession is active");
}
// Set up audio session category to kAudioSessionCategory_MediaPlayback.
// While playing sounds using this session category at least every 10 seconds, the iPhone doesn't go to sleep.
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback; // Defines a new variable of type UInt32 and initializes it with the identifier
// for the category you want to apply to the audio session.
AudioSessionSetProperty
(
kAudioSessionProperty_AudioCategory, // The identifier, or key, for the audio session property you want to set.
sizeof(sessionCategory), // The size, in bytes, of the property value that you are applying.
&sessionCategory // The category you want to apply to the audio session.
);
// Set up audio session playback mixing behavior.
// kAudioSessionCategory_MediaPlayback usually prevents playback mixing, so we allow it here. This way, we don't get in the way of other sound playback in an application.
// This property has a value of false (0) by default. When the audio session category changes, such as during an interruption, the value of this property reverts to false.
// To regain mixing behavior you must then set this property again.
// Always check to see if setting this property succeeds or fails, and react appropriately; behavior may change in future releases of iPhone OS.
OSStatus propertySetError = 0;
UInt32 allowMixing = true;
propertySetError = AudioSessionSetProperty
(
kAudioSessionProperty_OverrideCategoryMixWithOthers, // The identifier, or key, for the audio session property you want to set.
sizeof(allowMixing), // The size, in bytes, of the property value that you are applying.
&allowMixing // The value to apply to the property.
);
if (propertySetError)
{
MMPALog(@"Error setting kAudioSessionProperty_OverrideCategoryMixWithOthers: %d", propertySetError);
}
}
@end