我的應用程序創建一個mp4文件。我驗證過的代碼,我對以下設備的工作原理:AVAssetWriter startWriting在iPod Touch第三代操作系統4.2.1上失敗
- 的iPad(OS 4.3.2)
- 的iPhone4(OS 4.2.1)
- 的iPhone 3GS(OS 4.2.1)
..但我的iPod Touch第三代運行OS 4.2.1的init失敗。
這與這裏的another question有關,但我在另一個iOS設備上看到了它,而我在這裏包含了我的init代碼。與其他問題一樣,我嘗試過不同的像素格式以及比特率,但AVAssetWriter的狀態在調用其startWriting函數後始終更改爲AVAssetWriterStatusFailed。
任何想法將不勝感激。我知道在這個設備上可以創建mp4,因爲我已經下載了另一個應用程序,它可以在我的代碼失敗的相同設備上正常工作。
這是做視頻設置的最小代碼。
#import "AVFoundation/AVFoundation.h"
#import "AVFoundation/AVAssetWriterInput.h"
#import "AVFoundation/AVAssetReader.h"
#import "Foundation/NSUrl.h"
void VideoSetupTest()
{
int width = 320;
int height = 480;
// Setup the codec settings.
int nBitsPerSecond = 100000;
NSDictionary *codecSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: nBitsPerSecond], AVVideoAverageBitRateKey,
nil];
// Create the AVAssetWriterInput.
NSDictionary *outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
codecSettings, AVVideoCompressionPropertiesKey,
[NSNumber numberWithInt: width], AVVideoWidthKey,
[NSNumber numberWithInt: height], AVVideoHeightKey,
nil];
AVAssetWriterInput *assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType: AVMediaTypeVideo outputSettings: outputSettings];
[assetWriterInput retain];
// Create the AVAssetWriterPixelBufferAdaptor.
NSDictionary *pixelBufferAdaptorAttribs = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey,
[NSNumber numberWithInt: width], kCVPixelBufferWidthKey,
[NSNumber numberWithInt: height], kCVPixelBufferHeightKey,
nil];
AVAssetWriterInputPixelBufferAdaptor *pixelBufferAdaptor = [AVAssetWriterInputPixelBufferAdaptor alloc];
[pixelBufferAdaptor initWithAssetWriterInput: assetWriterInput sourcePixelBufferAttributes: pixelBufferAdaptorAttribs];
// Figure out a filename.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
char szFilename[256];
sprintf(szFilename, "%s/test.mp4", [documentsDirectory UTF8String]);
unlink(szFilename);
printf("Filename:\n%s\n\n", szFilename);
// Create the AVAssetWriter.
NSError *pError;
NSURL *url = [NSURL fileURLWithPath: [NSString stringWithUTF8String: szFilename]];
AVAssetWriter *assetWriter = [AVAssetWriter alloc];
[assetWriter initWithURL:url fileType:AVFileTypeMPEG4 error:&pError];
// Bind these things together and start!
assetWriterInput.expectsMediaDataInRealTime = YES;
[assetWriter addInput:assetWriterInput];
//
// ** NOTE: Here's the call where [assetWriter status] starts returning AVAssetWriterStatusFailed
// on an iPod 3rd Gen running iOS 4.2.1.
//
[assetWriter startWriting];
}
**下面是我運行的一個額外的測試:**以.mp4並嘗試爲它創建一個AVAssetExportSession,然後查看_ [AVAssetExportSession supportedFileTypes] _返回的內容。結果:在** iPhone4 **和** iPad **上,我可以創建會話並支持com.apple.quicktime-電影文件。在** iPod Touch第3代**上,我甚至無法創建AVAssetExportSession。 _ [AVAssetExportSession exportSessionWithAsset] _只返回NULL。請注意,它_does_將.mp4文件正確加載到iPodT3上的AVAsset中。 – Mike 2011-05-05 22:53:01
**另一個測試:**看看iPodT3是否可以播放這些.mp4視頻。我通過電子郵件(通過gmail)發送了我的iPhone4創建的.mp4文件之一,並在iPodT3上閱讀該電子郵件。我能夠播放.mp4文件就好了。 – Mike 2011-05-05 22:53:43
**另一個測試:**嘗試使用AVAssetWriterInput創建一個.wav文件(使用LinearPCM 16,單聲道,11025hz)或.m4a(kAudioFormatMPEG4AAC)。我可以在iPodT3上創建這兩個。所以我可以用它來創建mpeg4音頻文件,但我無法創建(任何)視頻文件。 – Mike 2011-05-05 22:57:00