سؤال

Everywhere online I can find out some explanation related to Video files uploaded to Azure Media Services.

Based on the tutorials I wrote my own code.

After running the StoreAudio method I have:

  • New Blob on Storage
  • New Asset on Media Services
  • New Job successfully completed on Media Services
  • The created asset is Not Published
  • When I try to get from the convertedAsset properties like ID or URI I get an exception

Why are ID and URI null? Why is the content "not published"?

Code:

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();
    }
هل كانت مفيدة؟

المحلول

Adding more explanation:

I suggested you to create SAS locator is because we don't support Audio streaming for WMA file for now, therefore, Getting an Origin Streaming locator won't work for you.

The reason why the asset is "not published": you never publish the asset - getting a SAS locator or Origin locator is the way to get your asset published.

نصائح أخرى

For audio file, you could ask for a SAS locator to access to the file-SAS locator is used for progressive download.

_context.Locators.Create(LocatorType.Sas,outputAsset,AccessPermissions.Read,TimeSpan.FromDays(30));

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top