我的应用程序创建一个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