using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using AVFoundation; using Foundation; using MediaPlayer; namespace Shared { [System.Serializable] public class MusicInfo { static MusicInfo() { System.Threading.Tasks.Task.Run(() => { chekcAuthOfMusic(); }); } static void chekcAuthOfMusic() { var authStatus = MPMediaLibrary.AuthorizationStatus; if (authStatus == MPMediaLibraryAuthorizationStatus.NotDetermined) { MPMediaLibrary.RequestAuthorization((status) => { if (status == MPMediaLibraryAuthorizationStatus.Authorized) { getNativeSongs(); } }); } else if (authStatus == MPMediaLibraryAuthorizationStatus.Authorized) { getNativeSongs(); } } static void getNativeSongs() { var itemsFromGenericQuery = new MPMediaQuery().Items; if (itemsFromGenericQuery == null) { return; } foreach (MPMediaItem song in itemsFromGenericQuery) { var musicInfo = new MusicInfo { SourceType = "Local", AlbumId = "0", }; musicInfo.Title = song.ValueForProperty(new NSString("title"))?.ToString().TrimStart('<').TrimEnd('>').Replace("&", ""); if (musicInfo.Title == null) { musicInfo.Title = "Unkown"; } musicInfo.Artist = song.ValueForProperty(new NSString("artist"))?.ToString().TrimStart('<').TrimEnd('>').Replace("&", ""); if (musicInfo.Artist == null) { musicInfo.Artist = "Unkown"; } var ipodUrl = song.ValueForProperty(new NSString("assetURL"))?.ToString().TrimStart('<').TrimEnd('>').Replace("&", ""); if (ipodUrl == null) { ipodUrl = ""; } musicInfo.Album = song.ValueForProperty(new NSString("albumTitle"))?.ToString().TrimStart('<').TrimEnd('>').Replace("&", ""); if (musicInfo.Album == null) { musicInfo.Album = "Unkown"; } musicInfo.Album = musicInfo.Album.Replace("&", "&"); musicInfo.Duration = song.ValueForProperty(new NSString("playbackDuration"))?.ToString().TrimStart('<').TrimEnd('>').Replace("&", ""); if (musicInfo.Duration == null) { musicInfo.Duration = "0"; } else { musicInfo.Duration = ((int)(float.Parse(musicInfo.Duration) * 1000)).ToString(); } //musicInfo.URL = getFileUrl(ipodUrl); musicInfo.ID = getFileUrl(ipodUrl); if (!string.IsNullOrEmpty(musicInfo.ID)) { MusicInfoList.Add(musicInfo); } } var sharedInstance = GlobalInfo.SharedInstance; } static string getTempPath() { var dirs = NSSearchPath.GetDirectories(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomain.User, true); //var dirs = NSSearchPath.GetDirectories(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User, true); if (dirs == null || dirs.Length == 0) { return ""; } return dirs[0]; } //Error MT5211: Native linking failed, undefined Objective-C class: HTTPServer.The symbol '_OBJC_CLASS_$_HTTPServer' could not be found in any of the libraries or frameworks linked with your application. (MT5211) (Demo.IOS) static string getFileUrl(string theURL) { if (string.IsNullOrEmpty(theURL)) { return ""; } var range = theURL.IndexOf("ipod-library:"); var substring = theURL.Substring(range); var nameRange = theURL.IndexOf("?id="); var nameStringPart = theURL.Substring(nameRange + 4); var fileName = /*"SongFile_" +*/ nameStringPart; var filePath = getTempPath() + "/" + fileName; if (!NSFileManager.DefaultManager.FileExists(filePath)) { var theResourceURL = NSUrl.FromString(substring); var songAsset = AVUrlAsset.Create(theResourceURL);// new AVUrlAsset(theResourceURL);// [AVURLAsset URLAssetWithURL: theResourceURL options: nil]; var exportSession = new AVAssetExportSession(songAsset, AVAssetExportSessionPreset.AppleM4A); exportSession.OutputUrl = new NSUrl(filePath, false);// [NSURL fileURLWithPath: filePath]; exportSession.OutputFileType = "com.apple.m4a-audio"; exportSession.ExportAsynchronously(() => { if (exportSession.Status == AVAssetExportSessionStatus.Completed) { Shared.HDLUtils.WriteLine("音乐文件复制完成"); } else { Shared.HDLUtils.WriteLine("音乐文件复制失败"); } });//248838 } //return $"http://{new Net.NetWiFi().IpAddress}:9876/Path={fileName}"; return fileName; } /// /// 歌曲名称 /// public string Title; /// /// 歌曲的歌手名 /// public string Artist; /// /// 歌曲的专辑名 /// public string Album; /// /// 歌曲专辑ID /// public string AlbumId; /// /// 歌曲文件的路径 /// public string Data; /// /// 歌曲的总播放时长 /// public string Size; /// /// 歌曲的总播放时长 /// public string Duration; /// /// 音乐ID /// public string ID; /// /// 网络地址 /// public string URL; /// /// 文件名 /// public string filename; /// /// DLNA列表名 /// public string dlnalistName; /// /// 图片 /// public string Image; /// /// 电台类型 /// public string Cntype; /// /// 电台类型 /// public string Cnkey; /// /// 是否是目录 /// public bool IsDirectory = true; /// /// USB Local URL /// public string SourceType; public string SourceName; readonly static List musicInfoList = new List { }; /// /// 获取所有的 /// public static List MusicInfoList { get { return musicInfoList; } } public object Tag; } }