forked from orta/tumblrclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioController.m
135 lines (108 loc) · 4.05 KB
/
AudioController.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
//
// AudioController.m
// Tumblor
//
// Created by orta on 20/07/2008.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "AudioController.h"
#import "NSImage+QuickLook.h"
@implementation AudioController
- (void) awakeFromNib{
_recordingFromMic = false;
[mainWindow setInitialFirstResponder:captionTextField];
}
- (IBAction) toggleMic: (id)sender{
if(_recordingFromMic){
[self stopVoiceRecording];
_recordingFromMic = false;
}else{
[self startVoiceRecording];
_recordingFromMic = true;
}
}
- (void) startVoiceRecording{
mCaptureSession = [[QTCaptureSession alloc] init];
BOOL success = NO;
NSError *error;
QTCaptureDevice *audioDevice = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];
success = [audioDevice open:&error];
if (![audioDevice hasMediaType:QTMediaTypeSound] && ![audioDevice hasMediaType:QTMediaTypeMuxed]) {
QTCaptureDevice *audioDevice = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeSound];
success = [audioDevice open:&error];
if (!success) {
audioDevice = nil;
return;
// Handle error
}
if (audioDevice) {
mCaptureAudioDeviceInput = [[QTCaptureDeviceInput alloc] initWithDevice:audioDevice];
success = [mCaptureSession addInput:mCaptureAudioDeviceInput error:&error];
if (!success) {
return;
}
}
mCaptureMovieFileOutput = [[QTCaptureMovieFileOutput alloc] init];
success = [mCaptureSession addOutput:mCaptureMovieFileOutput error:&error];
if (!success) {
// Handle error
}
[mCaptureMovieFileOutput setDelegate:self];
// Set the compression for the audio/video that is recorded to the hard disk.
NSEnumerator *connectionEnumerator = [[mCaptureMovieFileOutput connections] objectEnumerator];
QTCaptureConnection *connection;
// iterate over each output connection for the capture session and specify the desired compression
while ((connection = [connectionEnumerator nextObject])) {
NSString *mediaType = [connection mediaType];
QTCompressionOptions *compressionOptions = nil;
if ([mediaType isEqualToString:QTMediaTypeSound]) {
compressionOptions = [QTCompressionOptions compressionOptionsWithIdentifier:@"QTCompressionOptionsHighQualityAACAudio"];
}
[mCaptureMovieFileOutput setCompressionOptions:compressionOptions forConnection:connection];
}
[mCaptureSession startRunning];
[mCaptureMovieFileOutput recordToOutputFileURL:[NSURL fileURLWithPath:@"/tmp/tumblraudio.mov"]];
}
}
- (void) stopVoiceRecording{
[mCaptureSession stopRunning];
}
- (void)captureOutput:(QTCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL forConnections:(NSArray *)connections dueToError:(NSError *)error {
[self setSource:@"/tmp/tumblraudio.mov"];
//TODO : convert to mp3
}
- (void) hasBecomeKeyView{
if(!_musicViewActive){
musicView = [[iMBMusicView alloc] initWithFrame:[musicContainerView bounds]];
[musicContainerView addSubview:(NSView *)musicView];
[(NSView *)musicView setAutoresizingMask:(NSViewHeightSizable|NSViewWidthSizable)];
[musicView willActivate];
_musicViewActive = true;
}
}
- (void) setSource : (NSString *) path{
log(@"audio path = %@", [[NSURL fileURLWithPath: path] absoluteString] );
QTMovie *movie = [QTMovie movieWithFile:path error:nil];
[movieView setMovie:movie];
[albumImageView setImage:[NSImage imageWithPreviewOfFileAtPath:path ofSize:[albumImageView bounds].size asIcon:YES]];
_filepath = path;
}
- (void) clear{
[self setCaption:@""];
[self setSource:@""];
}
- (void) setCaption : (NSString *) caption{
[captionTextField setStringValue:caption];
}
- (void) resignedAsKeyView{
if ([[mCaptureAudioDeviceInput device] isOpen]){
[mCaptureSession stopRunning];
[[mCaptureAudioDeviceInput device] close];
}
}
- (NSDictionary *)values {
return [NSDictionary dictionaryWithObjectsAndKeys:
_filepath, @"filepath",
[captionTextField stringValue], @"caption",nil];
}
@end