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;
|
}
|
/// <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;
|
}
|
|
}
|