wxr
2020-06-09 77e7b5223dd04a6e036dc952efb38f2b770a6828
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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("&", "&amp;");
 
                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)
                    {
                        System.Console.WriteLine("音乐文件复制完成");
                    }
                    else
                    {
                        System.Console.WriteLine("音乐文件复制失败");
                    }
                });//248838
            }
            //return $"http://{new Net.NetWiFi().IpAddress}:9876/Path={fileName}";
            return fileName;
        }
        /// <summary>
        /// 歌曲名称
        /// </summary>
        public string Title;
        /// <summary>
        /// 歌曲的歌手名 
        /// </summary>
        public string Artist;
        /// <summary>
        /// 歌曲的专辑名     
        /// </summary>
        public string Album;
        /// <summary>
        /// 歌曲专辑ID
        /// </summary>
        public string AlbumId;
        /// <summary>
        /// 歌曲文件的路径
        /// </summary>
        public string Data;
        /// <summary>
        /// 歌曲的总播放时长
        /// </summary>
        public string Size;
        /// <summary>
        /// 歌曲的总播放时长
        /// </summary>
        public string Duration;
        /// <summary>
        /// 音乐ID
        /// </summary>
        public string ID;
        /// <summary>
        /// 网络地址
        /// </summary>
        public string URL;
        /// <summary>
        ///   文件名
        /// </summary>
        public string filename;
        /// <summary>
        /// DLNA列表名
        /// </summary>
        public string dlnalistName;
        /// <summary>
        /// 图片
        /// </summary>
        public string Image;
        /// <summary>
        /// 电台类型
        /// </summary>
        public string Cntype;
        /// <summary>
        /// 电台类型
        /// </summary>
        public string Cnkey;
        /// <summary>
        /// 是否是目录
        /// </summary>
        public bool IsDirectory = true;
 
        /// <summary>
        /// USB  Local URL 
        /// </summary>
        public string SourceType;
        public string SourceName;
 
 
        readonly static List<MusicInfo> musicInfoList = new List<MusicInfo> { };
        /// <summary>
        /// 获取所有的
        /// </summary>
        public static List<MusicInfo> MusicInfoList
        {
            get
            {
                return musicInfoList;
            }
        }
 
        public object Tag;
    }
    
}