Creating a Movie from Movie Data in Memory

Technical Q&A QA1341
Creating a Movie from Movie Data in Memory

Q: I can create a movie from a movie file using the NewMovieFromFileAPI, 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 theNewMovieFromDataRef API 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 NewMovieFromDataRef for 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 -2048noMovieFound error. 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 NSData data 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. ThecreatePointerDataRefWithExtensions routine is taken from Technical Note TN1195, 'Tagging Handle and Pointer Data References in QuickTime'.

This pointer data reference is then passed to NewMovieFromDataRef to 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 NewMovieFromDataRef to 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, thecreateHandleDataRefWithExtensions routine 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);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值