Creating a Movie from Movie Data in Memory
2010-01-16 21:45
615 查看
Technical Q&A QA1341
Creating a Movie from Movie Data in Memory
Q: I can create a movie from a movie file using the NewMovieFromFile
API, but how do I create a movie from movie data which resides in memory?
A: You can use QuickTime data references to accomplish this.Simply wrap a handle data reference or pointer data reference around the data, then use the
NewMovieFromDataRefAPI to create the movie. Play or otherwise use the movie normally, then dispose of the movie, data reference and data when done.
IMPORTANT: If you are attempting to create a movie from non-QuickTime movie (.mov) data (MP3, for example) you must add appropriate data reference extensions to indicate the media type. QuickTime uses this information to find the movie importer to use. Technical Note TN1195, 'Tagging Handle and Pointer Data References in QuickTime' describes the data reference extensions in great detail.
Calling
NewMovieFromDataReffor any non-QuickTime movie data in a handle or pointer data reference withoutspecifying any of the data reference extensions (filename, file type, MIME type, and so on) will fail with the -2048
noMovieFounderror. You must set at least one of the data reference extensions.
Alternately, you can use the MovieImportDataRef function and directly specify the movie importer component you would like to use to perform the import (rather than let QuickTime decide based on the data reference extensions). See Q&A QTMTB52 - Playing memory resident WAVE data using QuickTime for an example code listing.
The code snippet in Listing 1 shows how to create a pointer data reference for data (MP3 data in this case) contained in a
NSDatadata buffer. A data reference extension for file name is also added to the data reference to enable QuickTime to more easily determine which movie importer component to use. The
createPointerDataRefWithExtensionsroutine is taken from Technical Note TN1195, 'Tagging Handle and Pointer Data References in QuickTime'.
This pointer data reference is then passed to
NewMovieFromDataRefto create the movie:
Listing 1: Creating a movie from an NSData data buffer using a pointer data reference.
// // LoadAndPlayMovieFromNSData // // Given an NSData data buffer, make a pointer data // reference then create a movie for the data. // Optionally add a file name data reference // extension to the data reference before creating // the movie. // // Parameters // // data A data buffer containing the // data you wish to import // fileName If you know the original file name // you should pass it here to help // QuickTime locate a movie importer // for the data. void LoadAndPlayMovieFromNSData(NSData *data, NSString *fileName) { Str255 fileName255; Handle ptrDataRef = NULL; Movie newMovie = NULL; c2pstrcpy(fileName255, [fileName UTF8String]); ptrDataRef = createPointerDataRefWithExtensions( [data bytes], /* pointer to data */ (Size)[data length], /* data size */ fileName255, /* file name */ 0, /* file type */ nil /* mime type string */ ); if (ptrDataRef) { short id = 0; OSErr err = noErr; /* now create a movie from the data reference */ err = NewMovieFromDataRef(&newMovie, newMovieActive, &id, ptrDataRef, PointerDataHandlerSubType); if (err == noErr) { // ...play/manipulate your movie here // when finished, clean up DisposeMovie(newMovie); } DisposeHandle(ptrDataRef); } }
Similarly, the code snippet in Listing 2 shows how to create a pointer data reference from a pointer to a block of MP3 data along with data reference extensions for file name. Once again, the pointer data reference is passed to
NewMovieFromDataRefto create the movie:
Listing 2: Creating a movie using a pointer data reference.
// // createMovieFromMemory // // Given a pointer to some movie data, it creates a // pointer data reference and then calls NewMovieFromDataRef // with this data reference to create a movie. // // Parameters // // data A Pointer to your movie data // dataSize The actual size of the movie data // specified by the data pointer // fileName If you know the original file name // you should pass it here to help // QuickTime determine which importer // to use. Pass nil if you do not wish // to specify the fileName void createMovieFromMemory(void *data, Size dataSize, Str255 fileName) { Handle myDataRef = NULL; myDataRef = createPointerDataRefWithExtensions( data, /* pointer to data */ dataSize, /* size of data */ fileName, /* file name */ 0, /* file type */ nil); /* mime type */ if (myDataRef) { OSErr err = noErr; short id = 0; Movie newMovie = NULL; err = NewMovieFromDataRef( &newMovie, newMovieActive, &id, myDataRef, PointerDataHandlerSubType); if (err == noErr) { // ... play/manipulate your movie here // clean up when finished using movie DisposeMovie(newMovie); } // more clean up DisposeHandle(myDataRef); } }
Lastly, the code snippet in Listing 3 shows how to create a handle data reference for MP3 data in a handle along with the same data reference extensions as shown in Listing 2. Again, the
createHandleDataRefWithExtensionsroutine is taken from Technical Note TN1195, 'Tagging Handle and Pointer Data References in QuickTime'.
Listing 3: Creating a movie using a handle data reference.
// // createMovieFromHandleMemory // // Given a pointer to some movie data, it creates a // pointer data reference and then calls NewMovieFromDataRef // with this data reference to create a movie. // // Parameters // // data A Pointer to your movie data // dataSize The actual size of the movie data // specified by the data pointer // fileName If you know the original file name // you should pass it here to help // QuickTime determine which importer // to use. Pass nil if you do not wish // to specify the fileName void createMovieFromHandleMemory(Handle dataHandle, Str255 fileName) { Handle myDataRef = NULL; myDataRef = createHandleDataRefWithExtensions( dataHandle, /* data handle */ fileName, /* file name */ 0, /* file type */ nil, /* mime type */ nil, /* initialization data */ 0); /* init data byte count */ if (myDataRef) { OSErr err = noErr; short id = 0; Movie newMovie = NULL; err = NewMovieFromDataRef( &newMovie, newMovieActive, &id, myDataRef, HandleDataHandlerSubType); if (err == noErr) { // ... play/manipulate your movie here // clean up when finished using movie DisposeMovie(newMovie); } // more clean up DisposeHandle(myDataRef); } }
相关文章推荐
- decompress gzip data from object in memory
- Playback audio data from memory in windows
- android in practice_Creating a data manager(MyMoviesDataBases project)
- 28msec - query data from any source in real time
- ImportSheet in QTP Data Table from QC
- 【跟着stackoverflow学Pandas】Select rows from a DataFrame based on values in a column -pandas 筛选
- oracle dg 报错: ORA-16057: DGID from server not in Data Guard configuration
- 94.You plan to move data from a flat file to a table in your database. You decide to use SQL*Loader
- visual event recognition in videos by learning from web data
- OleCreateStaticFromData返回E_OUTOFMEMORY
- opencv 分类器训练 Assertion failed (elements_read == 1) in icvGetHaarTraininDataFrom
- Calculate difference between consecutive data points in a column from a file
- Extended paging tables to map guest physical memory addresses from virtual memory page tables to host physical memory addresses in a virtual machine system
- How to extract data from ContentHolder in Windc...
- Insight into DOMDocument - how to convert data from XML to array in PHP
- Memory leak in Windows 8.1 with Killer E2200 and Windows Network Data Usage Monitoring
- 解决mysql不能修改(You can't specify target table 'basic_data' for update in FROM clause)
- HazelCast-Java实现的内存数据网格(In-Memory Data Grid)分析 一
- 直接插入 - An Insecure JSON Data Transference from C# Server Page to Smart Devices Case Share
- visual event recognition in videos by learning from web data