0
我在與NSFileHandleReadCompletionNotification不張貼的問題。我將一個NSTask的結果輸入另一個。無論出於什麼原因,我的getData方法都不會被調用。有任何想法嗎?這兩項任務都正常工作,因爲我正在將預期的文件寫入我的硬盤。只是沒有通知。NSFileHandleReadCompletionNotification不張貼
- (void) encodeVideo:(NSString*)path
{
NSString* ffmpegPath = [[NSBundle mainBundle] pathForResource:@"ffmpeg" ofType:@""];
NSString* presetFile = [[NSBundle mainBundle] pathForResource:@"128" ofType:@"ffpreset"];
NSTask* encodeTask = [[NSTask alloc] init];
NSMutableArray* arguments = [[NSMutableArray alloc] init];
[arguments addObject:@"-y"];
[arguments addObject:@"-i"];
[arguments addObject:path];
[arguments addObject:@"-f"];
[arguments addObject:@"mpegts"];
[arguments addObject:@"-ac"];
[arguments addObject:@"2"];
[arguments addObject:@"-fpre"];
[arguments addObject:presetFile];
[arguments addObject:@"-s"];
[arguments addObject:@"320x240"];
[arguments addObject:@"-aspect"];
[arguments addObject:@"320:240"];
[arguments addObject:@"-async"];
[arguments addObject:@"2"];
[arguments addObject:@"-"];
[encodeTask setLaunchPath: ffmpegPath];
[encodeTask setArguments: arguments];
[encodeTask setStandardOutput:[NSPipe pipe]];
NSMutableArray* segmentArguments = [[NSMutableArray alloc] init];
[segmentArguments addObject:@"-t"];
[segmentArguments addObject:@"10"];
[segmentArguments addObject:@"-f"];
[segmentArguments addObject:@"/Users/Morgan/Desktop/LiveStreamServer/video"];
[segmentArguments addObject:@"-S"];
[segmentArguments addObject:@"1"];
NSString* segmenterPath = [[NSBundle mainBundle] pathForResource:@"mediastreamsegmenter" ofType:@""];
NSLog(@"%@", segmenterPath);
NSTask* segmentTask = [[NSTask alloc] init];
[segmentTask setLaunchPath: segmenterPath];
[segmentTask setArguments: segmentArguments];
[segmentTask setStandardInput:[encodeTask standardOutput]];
[segmentTask setStandardOutput:[NSPipe pipe]];
[segmentTask setStandardError:[NSPipe pipe]];
NSFileHandle *file = [[segmentTask standardOutput] fileHandleForReading];
[file readInBackgroundAndNotify];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getData:) name:NSFileHandleReadCompletionNotification object:file];
[segmentTask launch];
[encodeTask launch];
[arguments release];
[segmentArguments release];
}
- (void) getData: (NSNotification *)aNotification
{
NSData *data = [[aNotification userInfo] objectForKey:@"NSFileHandleNotificationDataItem"];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
[[aNotification object] readInBackgroundAndNotify];
}
我知道你已經解決了這個問題。我只是想添加評論,因爲這是很多人犯的常見錯誤。你應該總是使用系統提供的定義,所以這是一個好主意,用NSFileHandleNotificationDataItem代替@‘NSFileHandleNotificationDataItem’的,因爲如果你拼錯一個字符串,你會碰到一些難以發現的問題。如果拼寫頭文件中提供的定義拼寫錯誤,您只會收到編譯錯誤,並可以立即發現它。 – 2013-12-09 07:05:40