无处不在网上我可以找到与上传到Azure媒体服务的视频文件相关的一些解释。
根据教程,我编写了自己的代码。
运行StoreAudio方法后,我有:
为什么ID和URI为空?为什么内容“未发布”?
代码:
public string StoreAudio(int ID, byte[] file)
{
try
{
var blobContainerName = AudioBookContainer; //+ AudioChapterID % 1000?
var fileName = ID + ".mp3";
var mediaBlobContainer = blobClient.GetContainerReference(blobContainerName);
mediaBlobContainer.CreateIfNotExists();
using (MemoryStream ms = new MemoryStream(file))
{
var reference = mediaBlobContainer.GetBlockBlobReference(fileName);
reference.UploadFromStream(ms);
}
IAsset asset = _context.Assets.Create(fileName, AssetCreationOptions.None);
IAccessPolicy writePolicy = _context.AccessPolicies.Create("writePolicy", TimeSpan.FromMinutes(120), AccessPermissions.Write);
ILocator destinationLocator = _context.Locators.CreateLocator(LocatorType.Sas, asset, writePolicy);
Uri uploadUri = new Uri(destinationLocator.Path);
string assetContainerName = uploadUri.Segments[1];
CloudBlobContainer assetContainer = blobClient.GetContainerReference(assetContainerName);
var sourceCloudBlob = mediaBlobContainer.GetBlockBlobReference(fileName);
sourceCloudBlob.FetchAttributes();
if (sourceCloudBlob.Properties.Length > 0)
{
IAssetFile assetFile = asset.AssetFiles.Create(fileName);
var destinationBlob = assetContainer.GetBlockBlobReference(fileName);
destinationBlob.DeleteIfExists();
destinationBlob.StartCopyFromBlob(sourceCloudBlob);
destinationBlob.FetchAttributes();
if (sourceCloudBlob.Properties.Length != destinationBlob.Properties.Length)
throw new Exception("Error copying");
}
destinationLocator.Delete();
writePolicy.Delete();
asset = _context.Assets.Where(a => a.Id == asset.Id).FirstOrDefault(); //At this point, you can create a job using your asset.
var encodedAsset = EncodeToWMA(asset);
return encodedAsset.Id;
//var ismAssetFiles = encodedAsset.AssetFiles.ToList().Where(f => f.Name.EndsWith(".ism", StringComparison.OrdinalIgnoreCase)).ToArray();
//if (ismAssetFiles.Count() != 1)
// throw new ArgumentException("The asset should have only one, .ism file");
//ismAssetFiles.First().IsPrimary = true;
//ismAssetFiles.First().Update();
asset.Delete();
return encodedAsset.Uri.AbsoluteUri;
}
catch(Exception exx)
{
return exx.Message + exx.InnerException;
}
}
private static IMediaProcessor GetLatestMediaProcessorByName(string mediaProcessorName)
{
var processor = _context.MediaProcessors.Where(p => p.Name == mediaProcessorName).ToList().OrderBy(p => new Version(p.Version)).LastOrDefault();
if (processor == null)
throw new ArgumentException(string.Format("Unknown media processor", mediaProcessorName));
return processor;
}
public static IAsset EncodeToWMA(IAsset asset)
{
IJob job = _context.Jobs.Create("Convert MP3 to WMA");
IMediaProcessor processor = GetLatestMediaProcessorByName("Windows Azure Media Encoder");
ITask task = job.Tasks.AddNew("My encoding task", processor, "WMA High Quality Audio", TaskOptions.None);
task.InputAssets.Add(asset);
task.OutputAssets.AddNew(asset.Name.Replace(".mp3", ".wma"), AssetCreationOptions.None);
job.Submit();
Task progressJobTask = job.GetExecutionProgressTask(CancellationToken.None);
progressJobTask.Wait();
return task.OutputAssets.First();
}
答案 0 :(得分:1)
对于音频文件,您可以要求SAS定位器访问文件 - SAS定位器用于渐进式下载。
_context.Locators.Create(LocatorType.Sas,outputAsset,AccessPermissions.Read,TimeSpan.FromDays(30));
答案 1 :(得分:1)
添加更多解释:
我建议您创建SAS定位器是因为我们现在不支持WMA文件的音频流,因此,获取Origin Streaming定位器将不适合您。
资产“未发布”的原因:您从不发布资产 - 获取SAS定位器或Origin定位器是获取资产的方式。