MPMediaItems raw song data

孤人 提交于 2019-12-17 07:03:36

问题


I was wondering how to access an MPMediaItem's raw data.

Any ideas?


回答1:


you can obtain the media item's data in such way:

-(void)mediaItemToData
{
    // Implement in your project the media item picker

    MPMediaItem *curItem = musicPlayer.nowPlayingItem;

    NSURL *url = [curItem valueForProperty: MPMediaItemPropertyAssetURL];

    AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: url options:nil];

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset
                                       presetName: AVAssetExportPresetPassthrough];

    exporter.outputFileType = @"public.mpeg-4";

    NSString *exportFile = [[self myDocumentsDirectory] stringByAppendingPathComponent:           
                                                               @"exported.mp4"];

    NSURL *exportURL = [[NSURL fileURLWithPath:exportFile] retain];
    exporter.outputURL = exportURL; 

    // do the export
    // (completion handler block omitted) 
    [exporter exportAsynchronouslyWithCompletionHandler:
    ^{
    NSData *data = [NSData dataWithContentsOfFile: [[self myDocumentsDirectory] 
                                     stringByAppendingPathComponent: @"exported.mp4"]];

    // Do with data something

    }];
}

This code will work only on ios 4.0 and later

Good luck!




回答2:


Of course you can access the data of a MPMediaItem. It's not crystal clear at once but it works. Here's how:

  • Get the media item's URL from it's MPMediaItemPropertyAssetURL property
  • Initialize an AVURLAsset with this URL
  • Initialize an AVAssetReader with this asset
  • Fetch the AVAssetTrack you want to read from the AVURLAsset
  • Create an AVAssetReaderTrackOutput with this track
  • Add this output to the AVAssetReader created before and -startReading
  • Fetch all data with AVAssetReaderTrackOutput's -copyNextSampleBuffer
  • PROFIT!

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):

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:&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<[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 && 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(&m_mtx);

  OSStatus err = noErr;
  AVAssetReaderStatus status = m_reader.status;

  if (m_invalid) {
    pthread_mutex_unlock(&m_mtx);
    return kEDLibraryAssetReader_Invalidated;
  }
  else if (status != AVAssetReaderStatusReading) {
    pthread_mutex_unlock(&m_mtx);
    return kEDLibraryAssetReader_NoMoreSampleBuffers;
  }

  // Read the next sample buffer
  CMSampleBufferRef sbuf = [m_output copyNextSampleBuffer];
  if (sbuf == NULL) {
    pthread_mutex_unlock(&m_mtx);
    return kEDLibraryAssetReader_NoMoreSampleBuffers;
  }

  CMSampleBufferRepresentationRef srep = CMSampleBufferRepresentationCreateWithSampleBuffer(sbuf);
  if (srep && repOut != NULL) {
    *repOut = srep;
  }
  else {
    DDLogError(@"CMSampleBufferRef corrupted");
    EDCFShow(sbuf);
    err = kEDLibraryAssetReader_BufferCorrupted;
  }
  CFRelease(sbuf);

  pthread_mutex_unlock(&m_mtx);

  return err;
}



回答3:


You can't, and there are no workaround. An MPMediaItem is not the actual piece of media, it is just the metadata about the media item communicated to the application via RPC from another process. The data for the item itself is not accessible in your address space.

I should note that even if you have the MPMediaItem its data probably is not loaded into the devices memory. The flash on the iPhone is slow and memory is scarce. While Apple may not want you to have access to the raw data backing an MPMediaItem, it is just as likely that they didn't bother dealing with it because they didn't want to invest the time necessary to deal with the APIs. If they did provide access to such a thing it almost certainly would not be as an NSData, but more likely as an NSURL they would give your application that would allow it to open the file and stream through the data.

In any event, if you want the functionality, you should file a bug report asking for.

Also, as a side note, don't mention your age in a bug report you send to Apple. I think it is very cool you are writing apps for the phone, when I was your age I loved experimenting with computers (back then I was working on things written in Lisp). The thing is you cannot legally agree to a contract in the United States, which is why the developer agreement specifically prohibits you from joining. From the first paragraph of the agreement:

You also certify that you are of the legal age of majority in the jurisdiction in which you reside (at least 18 years of age in many countries) and you represent that you are legally permitted to become a Registered iPhone Developer.

If you mention to a WWDR representative that you are not of age of majority they may realize you are in violation of the agreement and be obligated to terminate your developer account. Just a friendly warning.



来源:https://stackoverflow.com/questions/1656124/mpmediaitems-raw-song-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!