How to: Stream Audio on Android using Azure Media

2019-08-30 01:56发布

问题:

I've been researching but didn't find any tutorial on how to play an audio stream from Azure Media Services on Android apps ?

I've went through those tutorials http://azure.microsoft.com/en-us/documentation/articles/media-services-dotnet-get-started/ http://code.msdn.microsoft.com/Windows-Azure-Media-040435f8

But they download an asset not stream it.

There seems to be sth called PlayReady, but I need to license it !! http://www.microsoft.com/playready/features/ClientOptions.aspx

Can anyone guide me to any code/tutorial or where to start such that I could play an audio stream uploaded on Azure Media Services ?

Is it possible that I could obtain a streaming url from azure media services for my uploaded audio file, and then I would just play that normally using the MediaPlayer API on Android ?

回答1:

Azure Media Services is designed to process and serve streamed video and doesn't support audio-only media.

You might want to check to see if the new Azure CDN offering is providing smooth streaming yet.

Otherwise the only way to really enable this would be to setup an appropriate streaming server potentially running on an Azure VM as the websites and web role PaaS offerings don't have streaming capabilities baked into them.

Failing this you could potentially look at the AWS CloudFront Streaming offering if you need a public cloud solution.



回答2:

You can use AMS REST API or SDK for .NET to upload an MP3 file.

You can then produce an MP4 file by encoding with Azure Media Encoder and using the AAC Good Quality Audio preset.

Make sure to add at least one On-Demand streaming unit.

The following example uses SDK for .NET Extensions.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.MediaServices.Client;
using System.Threading;

namespace AudioTest
{
    class Program
    {
        // Read values from the App.config file.
        private static readonly string _mediaServicesAccountName =
            ConfigurationManager.AppSettings["MediaServicesAccountName"];
        private static readonly string _mediaServicesAccountKey =
            ConfigurationManager.AppSettings["MediaServicesAccountKey"];

        // Field for service context.
        private static CloudMediaContext _context = null;
        private static MediaServicesCredentials _cachedCredentials = null;


        static void Main(string[] args)
        {

            // Create and cache the Media Services credentials in a static class variable.
            _cachedCredentials = new MediaServicesCredentials(
                            _mediaServicesAccountName,
                            _mediaServicesAccountKey);
            // Used the cached credentials to create CloudMediaContext.
            _context = new CloudMediaContext(_cachedCredentials);


            // 1. Create a new asset by uploading a mezzanine file from a local path.
            IAsset inputAsset = _context.Assets.CreateFromFile(
                @"testrecording.wma",
                AssetCreationOptions.None,
                (af, p) =>
                {
                    Console.WriteLine("Uploading '{0}' - Progress: {1:0.##}%", af.Name, p.Progress);
                });


            IJob job = _context.Jobs.CreateWithSingleTask(
            MediaProcessorNames.AzureMediaEncoder,
            MediaEncoderTaskPresetStrings.AACGoodQualityAudio,
            inputAsset,
            "AACGoodQualityAudio",
            AssetCreationOptions.None);

            Console.WriteLine("Submitting transcoding job...");

            // 3. Submit the job and wait until it is completed.
            job.Submit();
            job = job.StartExecutionProgressTask(
                j =>
                {
                    Console.WriteLine("Job state: {0}", j.State);
                    Console.WriteLine("Job progress: {0:0.##}%", j.GetOverallProgress());
                },
                CancellationToken.None).Result;

            Console.WriteLine("Transcoding job finished.");

            IAsset outputAsset = job.OutputMediaAssets[0];

            Console.WriteLine("Publishing output asset...");

            // 4. Publish the output asset by creating an Origin locator for adaptive streaming. 
            _context.Locators.Create(
                LocatorType.OnDemandOrigin,
                outputAsset,
                AccessPermissions.Read,
                TimeSpan.FromDays(30));


            Uri hlsUri = outputAsset.GetHlsUri();
            Uri mpegDashUri = outputAsset.GetMpegDashUri();

            // 6. Get the asset URLs.
            // Test HLS with Safari and Chrome on iOS.
            Console.WriteLine(hlsUri);
            // Test MPEG DASH with http://dashif.org/reference/players/javascript/
            Console.WriteLine(mpegDashUri);
        }
    }
}

Also, see https://social.msdn.microsoft.com/Forums/azure/en-US/4fb146f1-81f4-4da0-a157-3e2316b127fe/windows-azure-media-services-audio-only?forum=MediaServices.

thank you,

Julia