[Iphone-cocos2d] share a piece of objective-C callable play music in a game (1)

Source: Internet
Author: User

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/

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.