如何在音频文件中添加图稿以在专辑封面中显示?

乌伊瓦尔·辛格

我有一个音频文件,想要更改其专辑封面插图。那么,有可能吗?并且,如何在iOS编程中为音频文件设置专辑封面中的插图?

实际上,我合并了两个音频文件,并希望添加专辑封面的插图,该插图将在iTune中显示。

代码如下:

- (BOOL) combineVoices1
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryCachesDirectory = [paths objectAtIndex:0];
libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:@"Caches"];
NSString *OutputFilePath = [libraryCachesDirectory stringByAppendingFormat:@"/%@.m4a",textFieldMixFile.text];
NSURL *audioFileOutput = [NSURL fileURLWithPath:OutputFilePath];
NSURL *audioFileInput1= audioFileURL1;//<Path of orignal audio file>
NSURL *audioFileInput2= audioFileURL2;//<Path of orignal audio file>

if (!audioFileInput1 || !audioFileInput2 || !audioFileOutput)
{
    return NO;
}

[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
//CMTime nextClipStartTime = kCMTimeZero;
AVMutableComposition *composition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime nextClipStartTimeMix1;
if (playbackDelayAfterTimeMix1 > 0) {
    nextClipStartTimeMix1 = CMTimeMake(playbackDelayAfterTimeMix1, 1);
}else{
    nextClipStartTimeMix1 = kCMTimeZero;
}
CMTime startTimeMix1;
if (playbackDelayMix1 > 0) {
    startTimeMix1 = CMTimeMake(playbackDelayMix1, 1);
}else{
    startTimeMix1 = kCMTimeZero;
}
[compositionAudioTrack setPreferredVolume:[NSTSharedData instance].volumeOfMIX1];
NSURL *url = audioFileURL1; //[NSURL fileURLWithPath:soundOne];
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio];
AVAssetTrack *clipAudioTrack;
if (tracks.count > 0) {
    clipAudioTrack = [[avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
}else{
    return NO;
}
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(startTimeMix1, avAsset.duration) ofTrack:clipAudioTrack atTime:nextClipStartTimeMix1 error:nil];

//avAsset.commonMetadata
AVMutableCompositionTrack *compositionAudioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime nextClipStartTimeMix2;
if (playbackDelayAfterTimeMix2 > 0) {
    nextClipStartTimeMix2 = CMTimeMake(playbackDelayAfterTimeMix2, 1);
}else{
    nextClipStartTimeMix2 = kCMTimeZero;
}
CMTime startTimeMix2;
if (playbackDelayMix2 > 0) {
    startTimeMix2 = CMTimeMake(playbackDelayMix2, 1);
}else{
    startTimeMix2 = kCMTimeZero;
}

[compositionAudioTrack1 setPreferredVolume:[NSTSharedData instance].volumeOfMIX2];
//NSString *soundOne1  =[[NSBundle mainBundle]pathForResource:@"test" ofType:@"caf"];
NSURL *url1 = audioFileURL2;  //[NSURL fileURLWithPath:soundOne1];
AVAsset *avAsset1 = [AVURLAsset URLAssetWithURL:url1 options:nil];
NSArray *tracks1 = [avAsset1 tracksWithMediaType:AVMediaTypeAudio];
AVAssetTrack *clipAudioTrack1;
if (tracks1.count > 0) {
    clipAudioTrack1 = [[avAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
}else{
    return NO;
}
[compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(startTimeMix2, avAsset1.duration) ofTrack:clipAudioTrack1 atTime:nextClipStartTimeMix2 error:nil];


AVAssetExportSession *exportSession = [AVAssetExportSession
                                       exportSessionWithAsset:composition
                                       presetName:AVAssetExportPresetAppleM4A];

if (nil == exportSession) return NO;


exportSession.outputURL = audioFileOutput;
exportSession.outputFileType = AVFileTypeAppleM4A;

[exportSession exportAsynchronouslyWithCompletionHandler:^
 {
     if (AVAssetExportSessionStatusCompleted == exportSession.status)
     {

        [self performSelectorOnMainThread:@selector(performAction) withObject:nil waitUntilDone:NO];

     }
     else if (AVAssetExportSessionStatusFailed == exportSession.status)
     {
         [self performSelectorOnMainThread:@selector(hideSpinningWheel) withObject:nil waitUntilDone:NO];
         [[NSTSharedData instance] showAlertForTitle:@"Error!" andMessage:[NSString stringWithFormat:@"%@",[[exportSession error] localizedDescription]]];
         //NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
     }
 }];

return YES;
}
乌伊瓦尔·辛格

我解决了问题,现在工作正常,我在上述代码中的“ AVAssetExportSession”附近添加了代码。最后的方法是:

- (BOOL) combineVoices1
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryCachesDirectory = [paths objectAtIndex:0];
libraryCachesDirectory = [libraryCachesDirectory stringByAppendingPathComponent:@"Caches"];
NSString *OutputFilePath = [libraryCachesDirectory stringByAppendingFormat:@"/%@.m4a",textFieldMixFile.text];
NSURL *audioFileOutput = [NSURL fileURLWithPath:OutputFilePath];
NSURL *audioFileInput1= audioFileURL1;//<Path of orignal audio file>
NSURL *audioFileInput2= audioFileURL2;//<Path of orignal audio file>

if (!audioFileInput1 || !audioFileInput2 || !audioFileOutput)
{
    return NO;
}

[[NSFileManager defaultManager] removeItemAtURL:audioFileOutput error:NULL];
//CMTime nextClipStartTime = kCMTimeZero;
AVMutableComposition *composition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime nextClipStartTimeMix1;
if (playbackDelayAfterTimeMix1 > 0) {
    nextClipStartTimeMix1 = CMTimeMake(playbackDelayAfterTimeMix1, 1);
}else{
    nextClipStartTimeMix1 = kCMTimeZero;
}
CMTime startTimeMix1;
if (playbackDelayMix1 > 0) {
    startTimeMix1 = CMTimeMake(playbackDelayMix1, 1);
}else{
    startTimeMix1 = kCMTimeZero;
}
[compositionAudioTrack setPreferredVolume:[NSTSharedData instance].volumeOfMIX1];
NSURL *url = audioFileURL1; //[NSURL fileURLWithPath:soundOne];
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
NSArray *tracks = [avAsset tracksWithMediaType:AVMediaTypeAudio];
AVAssetTrack *clipAudioTrack;
if (tracks.count > 0) {
    clipAudioTrack = [[avAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
}else{
    return NO;
}
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(startTimeMix1, avAsset.duration) ofTrack:clipAudioTrack atTime:nextClipStartTimeMix1 error:nil];

//avAsset.commonMetadata
AVMutableCompositionTrack *compositionAudioTrack1 = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime nextClipStartTimeMix2;
if (playbackDelayAfterTimeMix2 > 0) {
    nextClipStartTimeMix2 = CMTimeMake(playbackDelayAfterTimeMix2, 1);
}else{
    nextClipStartTimeMix2 = kCMTimeZero;
}
CMTime startTimeMix2;
if (playbackDelayMix2 > 0) {
    startTimeMix2 = CMTimeMake(playbackDelayMix2, 1);
}else{
    startTimeMix2 = kCMTimeZero;
}

[compositionAudioTrack1 setPreferredVolume:[NSTSharedData instance].volumeOfMIX2];
//NSString *soundOne1  =[[NSBundle mainBundle]pathForResource:@"test" ofType:@"caf"];
NSURL *url1 = audioFileURL2;  //[NSURL fileURLWithPath:soundOne1];
AVAsset *avAsset1 = [AVURLAsset URLAssetWithURL:url1 options:nil];
NSArray *tracks1 = [avAsset1 tracksWithMediaType:AVMediaTypeAudio];
AVAssetTrack *clipAudioTrack1;
if (tracks1.count > 0) {
    clipAudioTrack1 = [[avAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
}else{
    return NO;
}
[compositionAudioTrack1 insertTimeRange:CMTimeRangeMake(startTimeMix2, avAsset1.duration) ofTrack:clipAudioTrack1 atTime:nextClipStartTimeMix2 error:nil];

/**
 added MetadataItem
 **/

AVMutableMetadataItem *artistMetadata = [[AVMutableMetadataItem alloc] init];
artistMetadata.key = AVMetadataiTunesMetadataKeyArtist;
artistMetadata.keySpace = AVMetadataKeySpaceiTunes;
artistMetadata.locale = [NSLocale currentLocale];
artistMetadata.value = uTakeTheMicArtist;

AVMutableMetadataItem *albumMetadata = [[AVMutableMetadataItem alloc] init];
albumMetadata.key = AVMetadataiTunesMetadataKeyAlbum;
albumMetadata.keySpace = AVMetadataKeySpaceiTunes;
albumMetadata.locale = [NSLocale currentLocale];
albumMetadata.value = uTakeTheMicAlbum;

AVMutableMetadataItem *songMetadata = [[AVMutableMetadataItem alloc] init];
songMetadata.key = AVMetadataiTunesMetadataKeySongName;
songMetadata.keySpace = AVMetadataKeySpaceiTunes;
songMetadata.locale = [NSLocale currentLocale];
songMetadata.value = textFieldMixFile.text;

AVMutableMetadataItem *imageMetadata = [[AVMutableMetadataItem alloc] init];
imageMetadata.key = AVMetadataiTunesMetadataKeyCoverArt;
imageMetadata.keySpace = AVMetadataKeySpaceiTunes;
imageMetadata.locale = [NSLocale currentLocale];
imageMetadata.value = imageData; //imageData is NSData of UIImage.
NSArray *metadata = [NSArray arrayWithObjects:artistMetadata, albumMetadata, songMetadata, imageMetadata, nil];

AVAssetExportSession *exportSession = [AVAssetExportSession
                                       exportSessionWithAsset:composition
                                       presetName:AVAssetExportPresetAppleM4A];

if (nil == exportSession) return NO;

exportSession.metadata = metadata;
exportSession.outputURL = audioFileOutput;
exportSession.outputFileType = AVFileTypeAppleM4A;

[exportSession exportAsynchronouslyWithCompletionHandler:^
 {
     if (AVAssetExportSessionStatusCompleted == exportSession.status)
     {

        [self performSelectorOnMainThread:@selector(performAction) withObject:nil waitUntilDone:NO];

     }
     else if (AVAssetExportSessionStatusFailed == exportSession.status)
     {
         [self performSelectorOnMainThread:@selector(hideSpinningWheel) withObject:nil waitUntilDone:NO];
         [[NSTSharedData instance] showAlertForTitle:@"Error!" andMessage:[NSString stringWithFormat:@"%@.",[[exportSession error] localizedDescription]]];
         //NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
     }
 }];

return YES;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何通过 url (Glide) 从服务器的音频文件中获取专辑封面

来自分类Dev

从音频文件Uri获取专辑封面

来自分类Dev

从音频文件Uri获取专辑封面

来自分类Dev

如何显示专辑封面?

来自分类Dev

如何显示专辑封面?

来自分类Dev

如何在FFMPEG中组合多个音频文件时添加静止图像

来自分类Dev

ffmpeg:如何在单个命令中合并音频文件并添加背景音乐?

来自分类Dev

如何在WordPress中显示嵌入式音频文件的标题

来自分类Dev

如何在iOS中显示3个音频文件的相同进度条

来自分类Dev

如何在 jwplayer 中显示音频文件的媒体控件(播放、暂停)?

来自分类Dev

如何在Matlab中规范音频文件

来自分类Dev

如何在语音通道中播放特定的音频文件?

来自分类Dev

如何在WASAPICaptureSharedEventDriven中以原始格式保存音频文件

来自分类Dev

如何在 ios 中播放 .opus 音频文件

来自分类Dev

如何在javascript中调整音频文件的音量

来自分类Dev

如何从IsolatedStorage中读取音频文件?

来自分类Dev

如何从音频文件中识别歌曲?

来自分类Dev

如何在Python中使用Soundfile将具有指定SNR的高斯噪声添加到音频文件中?

来自分类Dev

在 Content Provider 中删除专辑的专辑封面

来自分类Dev

如何查看音频CD中的隐藏音频文件

来自分类Dev

专辑封面可绘制对象未显示在ListView中[Android]

来自分类Dev

批处理脚本可根据音频文件名将图像另存为专辑封面

来自分类Dev

在m4a文件中嵌入专辑封面?

来自分类Dev

如何在C ++中通过FFmpeg将原始文件转换为音频文件(.wav)

来自分类Dev

如何从C中的音频文件中过滤特定频率

来自分类Dev

如何从C中的音频文件中过滤特定频率

来自分类Dev

如何在MATLAB中读取2声道音频文件

来自分类Dev

如何在网页中多次播放同一音频文件?

来自分类Dev

如何在Python中读取类似于Matlab audioread的音频文件?

Related 相关文章

  1. 1

    如何通过 url (Glide) 从服务器的音频文件中获取专辑封面

  2. 2

    从音频文件Uri获取专辑封面

  3. 3

    从音频文件Uri获取专辑封面

  4. 4

    如何显示专辑封面?

  5. 5

    如何显示专辑封面?

  6. 6

    如何在FFMPEG中组合多个音频文件时添加静止图像

  7. 7

    ffmpeg:如何在单个命令中合并音频文件并添加背景音乐?

  8. 8

    如何在WordPress中显示嵌入式音频文件的标题

  9. 9

    如何在iOS中显示3个音频文件的相同进度条

  10. 10

    如何在 jwplayer 中显示音频文件的媒体控件(播放、暂停)?

  11. 11

    如何在Matlab中规范音频文件

  12. 12

    如何在语音通道中播放特定的音频文件?

  13. 13

    如何在WASAPICaptureSharedEventDriven中以原始格式保存音频文件

  14. 14

    如何在 ios 中播放 .opus 音频文件

  15. 15

    如何在javascript中调整音频文件的音量

  16. 16

    如何从IsolatedStorage中读取音频文件?

  17. 17

    如何从音频文件中识别歌曲?

  18. 18

    如何在Python中使用Soundfile将具有指定SNR的高斯噪声添加到音频文件中?

  19. 19

    在 Content Provider 中删除专辑的专辑封面

  20. 20

    如何查看音频CD中的隐藏音频文件

  21. 21

    专辑封面可绘制对象未显示在ListView中[Android]

  22. 22

    批处理脚本可根据音频文件名将图像另存为专辑封面

  23. 23

    在m4a文件中嵌入专辑封面?

  24. 24

    如何在C ++中通过FFmpeg将原始文件转换为音频文件(.wav)

  25. 25

    如何从C中的音频文件中过滤特定频率

  26. 26

    如何从C中的音频文件中过滤特定频率

  27. 27

    如何在MATLAB中读取2声道音频文件

  28. 28

    如何在网页中多次播放同一音频文件?

  29. 29

    如何在Python中读取类似于Matlab audioread的音频文件?

热门标签

归档