First, introduce the framework audiotool.
Code
- (ID) initwithpath :( nsstring * ) Path
{
Uint32 size, maxpacketsize;
Char * Cookie;
Int I;
If (Gthereisanactivetrack)
{
Nslog ( @" * ** Warning ** gbmusictrack only plays one track at a time! You must close the previusly Running Track "
" Before you can play another. Requested track was: % @ " , [Path lastpathcomponent]);
Return Nil;
}
// If the path does not exist, null is returned.
If (Path = Nil) Return Nil; // Initialization
If ( ! (Self = [Super init]) Return Nil;
// Open music in the specified path If (Noerr ! = Audiofileopenurl (cfurlref) [nsurl fileurlwithpath: path], 0x01 , 0 , & Audiofile ))
{
Nslog ( @" * ** Error ** gbmusictrack-initwithpath: cocould not open audio file. Path given was: % @ " , PATH );
Return Nil;
}
// Obtain the Data Type of the file.
Size = Sizeof (Dataformat );
Audiofilegetproperty (audiofile, kaudiofilepropertydataformat, & Size, & Dataformat );
// Create a new queue using the specified special data type and callback cache.
Audioqueuenewoutput ( & Dataformat, buffercallback, self, nil, nil, 0 , & Queue );
// Read the package size and allocate the required space if needed
If (Dataformat. mbytesperpacket = 0 | Dataformat. mframesperpacket = 0 )
{
// Since we didn't get sizes to work with, then this must be VBR data (variable bitrate), so
// We'll have to ask core audio to give us a conservative estimate of the largest packet we are
// Likely to read with kaudiofilepropertypacketsizeupperbound
Size = Sizeof (Maxpacketsize );
Audiofilegetproperty (audiofile, kaudiofilepropertypacketsizeupperbound, & Size, & Maxpacketsize );
If (Maxpacketsize > Gbuffersizebytes)
{
Maxpacketsize = Gbuffersizebytes;
Nslog ( @" * ** Warning ** gbmusictrack-initwithpath: had to limit packet size requested for file: % @ " , [Path lastpathcomponent]);
}
Numpacketstoread = Gbuffersizebytes / Maxpacketsize;
// Allocate space for the package description
Packetdescs = Malloc ( Sizeof (Audiostreampacketdescription) * Numpacketstoread );
}
Else
{
// For CBR data (constant bitrate), use suitable data to fill in the mixed village
Numpacketstoread = Gbuffersizebytes / Dataformat. mbytesperpacket;
// This package description is not required
Packetdescs = Nil;
}
// See if file uses a Magic cookie (a Magic cookie is meta data which some formats use)
Audiofilegetpropertyinfo (audiofile, kaudiofilepropertymagiccookiedata, & Size, nil );
If (Size > 0 )
{
// Copy the cached data from the file to the audio queue.
Cookie = Malloc ( Sizeof ( Char ) * Size );
Audiofilegetproperty (audiofile, kaudiofilepropertymagiccookiedata, & Size, cookie );
Audioqueuesetproperty (queue, kaudioqueueproperty_magiccookie, Cookie, size );
Free (cookie );
}
Audioqueueaddpropertylistener (queue, kaudioqueueproperty_isrunning, propertylistenercallback, self );
// Allocate data to a specific Cache
Packetindex = 0 ;
For (I = 0 ; I < Num_queue_buffers; I ++ )
{
Audioqueueallocatebuffer (queue, gbuffersizebytes, & Buffers [I]);
If ([Self readpacketsintobuffer: buffers [I] = 0 )
{
// Check the size of the package read to the cache
Break ;
}
}
Repeat = No;
Trackclosed = No;
Trackended = No;
Gthereisanactivetrack = Yes;
Return Self;
}
// Created by FIG on 7/6/08.
// Copyright 2008 Jake Peterson. All rights reserved.
Add a link for anti-theft:
Author: alexliu (Alex DOTNET learning)
Source: http://alexliu.cnblogs.com/