11

Essentially I am looking to concatenate AVAsset files. I've got a rough idea of what to do but I'm struggling with loading the audio files.

I can play the files with an AVAudioPlayer, I can see them in the directory via my terminal, but when I attempt to load them with AVAssetURL it always returns an empty array for tracks.

The URL I am using:

NSURL *firstAudioFileLocation = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@%@", workingDirectory , @"/temp.pcm"]];

Which results in:

file:///Users/evolve/Library/Developer/CoreSimulator/Devices/8BF465E8-321C-47E6-BF2E-049C5E900F3C/data/Containers/Data/Application/4A2D29B2-E5B4-4D07-AE6B-1DD15F5E59A3/Documents/temp.pcm



The asset being loaded:

AVAsset *test = [AVURLAsset URLAssetWithURL:firstAudioFileLocation options:nil];

However when calling this:

NSLog(@" total tracks %@", test.tracks);

My output is always total tracks ().

My subsequent calls to add them to my AVMutableCompositionTrack end up crashing the app as the AVAsset seems to not have loaded correctly.



I have played with other variations for loading the asset including:

NSURL *alternativeLocation = [[NSBundle mainBundle] URLForResource:@"temp" withExtension:@"pcm"];

As well as trying to load AVAsset with the options from the documentation:

NSDictionary *assetOptions = @{AVURLAssetPreferPreciseDurationAndTimingKey: @YES};



How do I load the tracks from a local resource, recently created by the AVAudioRecorder?

EDIT

I had a poke around and found I can record and load a .CAF file extension.

Seems .PCM is unsupported for AVAsset, this page also was of great help. https://developer.apple.com/documentation/avfoundation/avfiletype

Malii
  • 448
  • 6
  • 17

1 Answers1

12

An AVAsset load is not instantaneous. You need to wait for the data to be available. Example:

AVAsset *test = [AVURLAsset URLAssetWithURL:firstAudioFileLocation options:nil];
[test loadValuesAsynchronouslyForKeys:@[@"playable",@"tracks"] completionHandler:^{

    // Now tracks is available
    NSLog(@" total tracks %@", test.tracks);
}];

A more detailed example can be found in the documentation.

Gerd K
  • 2,933
  • 1
  • 20
  • 23
  • 2
    This has helped massively, and but it still returns an empty array. After looking into it using AVKeyValueStatus it seems Linear PCM is unsupported for AVAsset? Am I misunderstanding and is there a list of supported types for composition? – Malii Dec 15 '17 at 09:40
  • Your .pcm file is raw? That's not a supported format (oddly enough), if you put a wav header on it, it will work. – Rhythmic Fistman Dec 18 '17 at 14:00
  • Pay attention that this procedure working well for .mp4 videos but NOT for HLS(.m3u8) videos. https://stackoverflow.com/questions/52409687/cant-able-to-get-video-tracks-from-avurlasset-for-hls-videos-m3u8-format-for – inexcii Jan 30 '21 at 05:31