Adding metadata to generated audio file

I'm generating an audio file programmatically, and I'd like to add metadata to it, such as the title and artist. I don't particularly care what format the file is written in, as long as AVPlayer will read it and send it to the playing device. (The whole goal here is to get AVPlayer to send the track name for this generated song to a Bluetooth device. I'm happy to explore easier ways to achive this that don't require writing the file or adding metadata directly to the file.)


I create my writer, and then setup my metadata:


                let writer = try AVAssetWriter(outputURL: output, fileType: .aiff)
                let title = AVMutableMetadataItem()
                title.identifier = .commonIdentifierTitle
                title.dataType = kCMMetadataBaseDataType_UTF8 as String
                title.value = "The Title" as NSString
                writer.metadata = [
                    title,
                ]

                // setup the input and write the file.


The audio file writes without trouble, but the metadata isn't written. Audacity does seem to be able to write metadata to AIFF files. (I tried this originally with CAF files, but neither AVFoundation nor Audactiy wrote metadata in that case.)


I've tried a lot of different approaches. I tried moving the metadata to the AVAssetWriterInput. I tried writing the artist rather than the title (just to see if title was the problem). I tried using "key" and "keyspace" rather than "identifier" for the metadata. I tried AVMetadataID3MetadataKeyLeadPerformer and AVMetadataiTunesMetadataKeySongName. In all cases I get no errors, but the metadata just isn't in the file. If I read the file by hand "The Title" doesn't appear anywhere.

Adding metadata to generated audio file
 
 
Q