Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Of course you can access the data of a <code>MPMediaItem</code>. It's not crystal clear at once but it works. Here's how:</p> <ul> <li>Get the media item's URL from it's <code>MPMediaItemPropertyAssetURL</code> property</li> <li>Initialize an <code>AVURLAsset</code> with this URL</li> <li>Initialize an <code>AVAssetReader</code> with this asset</li> <li>Fetch the <code>AVAssetTrack</code> you want to read from the <code>AVURLAsset</code></li> <li>Create an <code>AVAssetReaderTrackOutput</code> with this track</li> <li>Add this output to the <code>AVAssetReader</code> created before and <code>-startReading</code></li> <li>Fetch all data with <code>AVAssetReaderTrackOutput</code>'s <code>-copyNextSampleBuffer</code></li> <li>PROFIT!</li> </ul> <p>Here is some sample code from a project of mine (this is not a code jewel of mine, wrote it some time back in my coding dark ages):</p> <pre><code>typedef enum { kEDSupportedMediaTypeAAC = 'aac ', kEDSupportedMediaTypeMP3 = '.mp3' } EDSupportedMediaType; - (EDLibraryAssetReaderStatus)prepareAsset { // Get the AVURLAsset AVURLAsset *uasset = [m_asset URLAsset]; // Check for DRM protected content if (uasset.hasProtectedContent) { return kEDLibraryAssetReader_TrackIsDRMProtected; } if ([uasset tracks] == 0) { DDLogError(@"no asset tracks found"); return AVAssetReaderStatusFailed; } // Initialize a reader with a track output NSError *err = noErr; m_reader = [[AVAssetReader alloc] initWithAsset:uasset error:&amp;err]; if (!m_reader || err) { DDLogError(@"could not create asset reader (%i)\n", [err code]); return AVAssetReaderStatusFailed; } // Check tracks for valid format. Currently we only support all MP3 and AAC types, WAV and AIFF is too large to handle for (AVAssetTrack *track in uasset.tracks) { NSArray *formats = track.formatDescriptions; for (int i=0; i&lt;[formats count]; i++) { CMFormatDescriptionRef format = (CMFormatDescriptionRef)[formats objectAtIndex:i]; // Check the format types CMMediaType mediaType = CMFormatDescriptionGetMediaType(format); FourCharCode mediaSubType = CMFormatDescriptionGetMediaSubType(format); DDLogVerbose(@"mediaType: %s, mediaSubType: %s", COFcc(mediaType), COFcc(mediaSubType)); if (mediaType == kCMMediaType_Audio) { if (mediaSubType == kEDSupportedMediaTypeAAC || mediaSubType == kEDSupportedMediaTypeMP3) { m_track = [track retain]; m_format = CFRetain(format); break; } } } if (m_track != nil &amp;&amp; m_format != NULL) { break; } } if (m_track == nil || m_format == NULL) { return kEDLibraryAssetReader_UnsupportedFormat; } // Create an output for the found track m_output = [[AVAssetReaderTrackOutput alloc] initWithTrack:m_track outputSettings:nil]; [m_reader addOutput:m_output]; // Start reading if (![m_reader startReading]) { DDLogError(@"could not start reading asset"); return kEDLibraryAssetReader_CouldNotStartReading; } return 0; } - (OSStatus)copyNextSampleBufferRepresentation:(CMSampleBufferRepresentationRef *)repOut { pthread_mutex_lock(&amp;m_mtx); OSStatus err = noErr; AVAssetReaderStatus status = m_reader.status; if (m_invalid) { pthread_mutex_unlock(&amp;m_mtx); return kEDLibraryAssetReader_Invalidated; } else if (status != AVAssetReaderStatusReading) { pthread_mutex_unlock(&amp;m_mtx); return kEDLibraryAssetReader_NoMoreSampleBuffers; } // Read the next sample buffer CMSampleBufferRef sbuf = [m_output copyNextSampleBuffer]; if (sbuf == NULL) { pthread_mutex_unlock(&amp;m_mtx); return kEDLibraryAssetReader_NoMoreSampleBuffers; } CMSampleBufferRepresentationRef srep = CMSampleBufferRepresentationCreateWithSampleBuffer(sbuf); if (srep &amp;&amp; repOut != NULL) { *repOut = srep; } else { DDLogError(@"CMSampleBufferRef corrupted"); EDCFShow(sbuf); err = kEDLibraryAssetReader_BufferCorrupted; } CFRelease(sbuf); pthread_mutex_unlock(&amp;m_mtx); return err; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload