我正在尝试为iPhone或iPad开发一个简单的音频播放器应用程序。我正在处理后台模式并控制锁定屏幕上的播放,但是我无法在锁定屏幕上设置有关当前音乐曲目(艺术作品,专辑等)的数据,就像在默认的iOS音乐播放器上一样。我怎么能做到这一点?任何帮助都提前感谢,谢谢。
答案 0 :(得分:5)
我解决了这个问题。为了在锁定屏幕iOS上显示有关音轨(艺术作品,专辑,标题等)的元数据,您应该使用MPNowPlayingInfoCenter课程。例如:
MPMediaItemArtwork *albumArtwork = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"artwork.png"]];
NSDictionary *nowPlayingInfo = @{MPMediaItemPropertyTitle: @"Title",
MPMediaItemPropertyArtist: @"Artist",
MPMediaItemPropertyAlbumTitle: @"Album",
MPMediaItemPropertyArtwork: albumArtwork};
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nowPlayingInfo];
答案 1 :(得分:0)
// set Audio file metadata Modify and get //
AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough];
session.outputURL = [NSURL fileURLWithPath:_audioPath];
session.outputFileType = AVFileTypeAppleM4A;
AVMutableMetadataItem *Titlenm = [[AVMutableMetadataItem alloc] init];
Titlenm.identifier = AVMetadataCommonIdentifierTitle;
Titlenm.dataType = (__bridge NSString *)kCMMetadataBaseDataType_UTF8; // more in CoreMedia/CMMetadata.h
Titlenm.value =@" Enter Audio Title";
AVMutableMetadataItem *Artistnm = [[AVMutableMetadataItem alloc] init];
Artistnm.identifier = AVMetadataCommonIdentifierArtist;
Artistnm.dataType = (__bridge NSString *)kCMMetadataBaseDataType_UTF8;
Artistnm.value =@" Enter Audio ArtistName";
AVMutableMetadataItem *AlbumName = [[AVMutableMetadataItem alloc] init];
AlbumName.identifier = AVMetadataCommonIdentifierAlbumName;
AlbumName.dataType = (__bridge NSString *)kCMMetadataBaseDataType_UTF8;
AlbumName.value =@" Enter Audio AlbumName";;
AVMutableMetadataItem *dateset = [[AVMutableMetadataItem alloc] init];
dateset.identifier = AVMetadataCommonIdentifierCreationDate;
dateset.dataType = (__bridge NSString *)kCMMetadataBaseDataType_UTF8;
dateset.value = self.datelbl.text;
AVMutableMetadataItem *audioArtwork = [[AVMutableMetadataItem alloc]init];
//self.audioimg.image in this line set image
audioArtwork.value = UIImagePNGRepresentation(self.audioimg.image);
audioArtwork.dataType = (__bridge NSString * _Nullable)(kCMMetadataBaseDataType_PNG);
audioArtwork.identifier = AVMetadataCommonIdentifierArtwork;
audioArtwork.extendedLanguageTag =@"und";
session.metadata = @[Titlenm, AlbumName,Artistnm,dateset,audioArtwork];
[session exportAsynchronouslyWithCompletionHandler:
^{
if (session.status == AVAssetExportSessionStatusCompleted)
{
AVAsset *asset = [AVURLAsset URLAssetWithURL:outputURL options:nil];
NSArray *metadata = [asset commonMetadata];
finalurl=outputURL;
for(AVMetadataItem* item in metadata)
{
NSString *key = [item commonKey];
NSString *value = [item stringValue];
NSLog(@"key = %@, value = %@", key,value);
}
[self performSelectorOnMainThread:@selector(myviewpresent) withObject:self waitUntilDone:YES];
}
}];