ios - how to play sound by AVAssetReader -



ios - how to play sound by AVAssetReader -

i using avassetreader individual frames video file. know how can play sound mp4 file.

the homecoming value of method [player play] false, there no sound play, why

thanks.

create avassetreader

nsurl *url = [nsurl fileurlwithpath:[[nsbundle mainbundle] pathforresource:@"zhang" oftype:@"mp4"]]; avurlasset *avasset = [[avurlasset alloc] initwithurl:url options:nil]; avassettrack *track1 = [[avasset trackswithmediatype:avmediatypeaudio] objectatindex:0]; nsmutabledictionary *dic2 = [nsmutabledictionary dictionarywithobjectsandkeys:[nsnumber numberwithint:kaudioformatlinearpcm], avformatidkey, [nsnumber numberwithint:16],avlinearpcmbitdepthkey, [nsnumber numberwithbool:no],avlinearpcmisbigendiankey, [nsnumber numberwithbool:no],avlinearpcmisfloatkey, [nsnumber numberwithbool:no],avlinearpcmisnoninterleaved, nil]; output1 = [[avassetreadertrackoutput alloc] initwithtrack:track1 outputsettings:dic2]; avassetreader *reader = [[avassetreader alloc] initwithasset:avasset error:nil]; [reader addoutput:output1]; [reader startreading];

output code following:

cmsamplebufferref sample = [output1 copynextsamplebuffer]; cmblockbufferref blockbufferref = cmsamplebuffergetdatabuffer(sample); size_t length = cmblockbuffergetdatalength(blockbufferref); uint8 buffer[length]; cmblockbuffercopydatabytes(blockbufferref, 0, length, buffer); nsdata * info = [[nsdata alloc] initwithbytes:buffer length:length]; nsstring *docdirpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring *filepath = [nsstring stringwithformat:@"%@/test.mp3", docdirpath]; [data writetofile:filepath atomically:yes]; [data release]; nserror *error; nsurl *fileurl = [nsurl fileurlwithpath:filepath]; avaudioplayer *player = [[avaudioplayer alloc] initwithcontentsofurl:fileurl error:&error]; player.numberofloops = 0; [player play];

one alternative avoid using avaudioplayer, , instead utilize pcm info have decoded fill audioqueue output.

the ideal method create avcomposition sound track of avasset created source mp4 file. avcomposition (as subclass of avasset) can used within avplayer, play sound track you.

you're not able write out blocks of pcm info file extension ".mp3" - that's encoded format. avaudioplayer looking encoded info in file, , file have written incompatible, why receiving homecoming value of false. if have heart set on writing sound out file, utilize avassetwriter appropriate settings. written out core sound file maximum performance. step encode sound format (say, mp3, or aac), have heavy performance cost associated it, , it's not suitable real-time playback.

ios audio avaudioplayer avassetreader

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -