2017-05-26 33 views
0

我正在開發Android應用程序,並且正在嘗試在Azure的媒體服務上上傳視頻。我一直在試圖在他們的文檔中找到一些有用的信息,但沒有任何信息。我發現this tutorial對於Java和我試圖添加使用搖籃的依賴,但我已經越來越錯誤在那裏同樣找不到類,沒有定義類,而不是找到的路徑等Azure中的哪個sdk庫我應該在Android中使用Media Services?

compile ('com.microsoft.windowsazure:microsoft-windowsazure-api:0.4.6'){ 
     exclude group: 'org.apache.httpcomponents', module: 'httpclient' 
     exclude group: 'commons-logging', module: 'commons-logging' 
     exclude group: 'org.codehaus.jackson', module: 'jackson-jaxrs' 
     exclude group: 'org.codehaus.jackson', module: 'jackson-xc' 
     exclude group: 'org.codehaus.jackson', module: 'jackson-core-asl' 
     exclude group: 'org.codehaus.jackson', module: 'jackson-mapper-asl' 
     exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind' 
     exclude group: 'com.fasterxml.jackson.core', module: 'jackson-annotations' 
     exclude group: 'com.fasterxml.jackson.core', module: 'jackson-core' 
     exclude group: 'com.fasterxml.jackson.datatype', module: 'jackson-datatype-joda' 
     exclude group: 'org.codehaus.jettison', module: 'jettison' 

    } 
    compile ('com.microsoft.windowsazure:microsoft-azure-api-core:0.5.0') 
    compile ('com.microsoft.windowsazure:microsoft-azure-api-media:0.5.0') 
    compile ('com.microsoft.azure.android:azure-storage-android:[email protected]') 
    compile ('com.microsoft.azure:azure:1.0.0') 
    compile ('com.microsoft.azure:azure-mobile-android:[email protected]') 
    compile ('com.microsoft.azure:azure-mgmt-storage:1.0.0') 
    compile ('com.microsoft.azure:azure-media:0.9.7') 

論文是我所使用的庫我已經在我的gradle文件的所有compile塊中使用了所有這些exclude塊,因爲它給我複製了重複文件的錯誤。我已經從這裏刪除了重複排除區塊。從Java的示例代碼中,我創建了下面的類,

public class MediaServiceHelper { 

    private String filePath, fileIdentifier; 
    private static String _filePath, _fileIdentifier; 

    // Media Services account credentials configuration 
    private static String mediaServiceUri = "https://media.windows.net/API/"; 
    private static String oAuthUri = "https://wamsprodglobal001acs.accesscontrol.windows.net/v2/OAuth2-13"; 
    private static String clientId = "account"; 
    private static String clientSecret = "key"; 
    private static String scope = "urn:WindowsAzureMediaServices"; 
    private static MediaContract mediaService; 

    // Encoder configuration 
    private static String preferredEncoder = "Media Encoder Standard"; 
    private static String encodingPreset = "Adaptive Streaming"; 


    public MediaServiceHelper() { 
    } 

    public MediaServiceHelper(String filePath, String fileIdentifier) { 
     this.filePath = filePath; 
     this.fileIdentifier = fileIdentifier; 

     _filePath = filePath; 
     _fileIdentifier = fileIdentifier; 
    } 

    public void process() { 
     try { 
      // Set up the MediaContract object to call into the Media Services account 
      Configuration configuration = MediaConfiguration.configureWithOAuthAuthentication(
        mediaServiceUri, oAuthUri, clientId, clientSecret, scope); 
      mediaService = MediaService.create(configuration); 


      // Upload a local file to an Asset 
      AssetInfo uploadAsset = uploadFileAndCreateAsset(fileIdentifier); 
      new ErrorPrinter("Uploaded Asset Id: " + uploadAsset.getId()); 


      // Transform the Asset 
      AssetInfo encodedAsset = encode(uploadAsset); 
      new ErrorPrinter("Encoded Asset Id: " + encodedAsset.getId()); 

      // Create the Streaming Origin Locator 
      String url = getStreamingOriginLocator(encodedAsset); 

      new ErrorPrinter("Origin Locator URL: " + url); 
      new ErrorPrinter("Sample completed!"); 

     } catch (Exception e) { 
      new ErrorPrinter("Exception encountered."); 
      new ErrorPrinter(e.toString()); 
     } 

    } 


    private static AssetInfo uploadFileAndCreateAsset(String fileName) 
      throws Exception{ 

     WritableBlobContainerContract uploader; 
     AssetInfo resultAsset; 
     AccessPolicyInfo uploadAccessPolicy; 
     LocatorInfo uploadLocator = null; 

     // Create an Asset 
     resultAsset = mediaService.create(Asset.create().setName(fileName).setAlternateId("altId")); 
     new ErrorPrinter("Created Asset " + fileName); 

     // Create an AccessPolicy that provides Write access for 15 minutes 
     uploadAccessPolicy = mediaService 
       .create(AccessPolicy.create("uploadAccessPolicy", 15.0, EnumSet.of(AccessPolicyPermission.WRITE))); 

     // Create a Locator using the AccessPolicy and Asset 
     uploadLocator = mediaService 
       .create(Locator.create(uploadAccessPolicy.getId(), resultAsset.getId(), LocatorType.SAS)); 

     // Create the Blob Writer using the Locator 
     uploader = mediaService.createBlobWriter(uploadLocator); 

     File file = new File(_filePath); 

     // The local file that will be uploaded to your Media Services account 
     InputStream input = new FileInputStream(file); 

     new ErrorPrinter("Uploading " + fileName); 

     // Upload the local file to the asset 
     uploader.createBlockBlob(fileName, input); 

     // Inform Media Services about the uploaded files 
     mediaService.action(AssetFile.createFileInfos(resultAsset.getId())); 
     new ErrorPrinter("Uploaded Asset File " + fileName); 

     mediaService.delete(Locator.delete(uploadLocator.getId())); 
     mediaService.delete(AccessPolicy.delete(uploadAccessPolicy.getId())); 

     return resultAsset; 
    } 

    // Create a Job that contains a Task to transform the Asset 
    private static AssetInfo encode(AssetInfo assetToEncode) 
      throws Exception{ 

     // Retrieve the list of Media Processors that match the name 
     ListResult<MediaProcessorInfo> mediaProcessors = mediaService 
       .list(MediaProcessor.list().set("$filter", String.format("Name eq '%s'", preferredEncoder))); 

     // Use the latest version of the Media Processor 
     MediaProcessorInfo mediaProcessor = null; 
     for (MediaProcessorInfo info : mediaProcessors) { 
      if (null == mediaProcessor || info.getVersion().compareTo(mediaProcessor.getVersion()) > 0) { 
       mediaProcessor = info; 
      } 
     } 

     new ErrorPrinter("Using Media Processor: " + mediaProcessor.getName() + " " + mediaProcessor.getVersion()); 

     // Create a task with the specified Media Processor 
     String outputAssetName = String.format("%s as %s", assetToEncode.getName(), encodingPreset); 
     String taskXml = "<taskBody><inputAsset>JobInputAsset(0)</inputAsset>" 
       + "<outputAsset assetCreationOptions=\"0\"" // AssetCreationOptions.None 
       + " assetName=\"" + outputAssetName + "\">JobOutputAsset(0)</outputAsset></taskBody>"; 

     Task.CreateBatchOperation task = Task.create(mediaProcessor.getId(), taskXml) 
       .setConfiguration(encodingPreset).setName("Encoding"); 

     // Create the Job; this automatically schedules and runs it. 
     Job.Creator jobCreator = Job.create() 
       .setName(String.format("Encoding %s to %s", assetToEncode.getName(), encodingPreset)) 
       .addInputMediaAsset(assetToEncode.getId()).setPriority(2).addTaskCreator(task); 
     JobInfo job = mediaService.create(jobCreator); 

     String jobId = job.getId(); 
     new ErrorPrinter("Created Job with Id: " + jobId); 

     // Check to see if the Job has completed 
     checkJobStatus(jobId); 
     // Done with the Job 

     // Retrieve the output Asset 
     ListResult<AssetInfo> outputAssets = mediaService.list(Asset.list(job.getOutputAssetsLink())); 
     return outputAssets.get(0); 
    } 


    public static String getStreamingOriginLocator(AssetInfo asset) throws Exception { 
     // Get the .ISM AssetFile 
     ListResult<AssetFileInfo> assetFiles = mediaService.list(AssetFile.list(asset.getAssetFilesLink())); 
     AssetFileInfo streamingAssetFile = null; 
     for (AssetFileInfo file : assetFiles) { 
      if (file.getName().toLowerCase().endsWith(".ism")) { 
       streamingAssetFile = file; 
       break; 
      } 
     } 

     AccessPolicyInfo originAccessPolicy; 
     LocatorInfo originLocator = null; 

     // Create a 30-day readonly AccessPolicy 
     double durationInMinutes = 60 * 24 * 30; 
     originAccessPolicy = mediaService.create(
       AccessPolicy.create("Streaming policy", durationInMinutes, EnumSet.of(AccessPolicyPermission.READ))); 

     // Create a Locator using the AccessPolicy and Asset 
     originLocator = mediaService 
       .create(Locator.create(originAccessPolicy.getId(), asset.getId(), LocatorType.OnDemandOrigin)); 

     // Create a Smooth Streaming base URL 
     return originLocator.getPath() + streamingAssetFile.getName() + "/manifest"; 
    } 

    private static void checkJobStatus(String jobId) throws Exception{ 
     boolean done = false; 
     JobState jobState = null; 
     while (!done) { 
      // Sleep for 5 seconds 
      Thread.sleep(5000); 

      // Query the updated Job state 
      jobState = mediaService.get(Job.get(jobId)).getState(); 
      new ErrorPrinter("Job state: " + jobState); 

      if (jobState == JobState.Finished || jobState == JobState.Canceled || jobState == JobState.Error) { 
       done = true; 
      } 
     } 
    } 

} 

誰能告訴我在Android中使用的庫,所以我可以將視頻上傳到Azure的媒體服務?或者我可以使用任何其他參考來找到我的問題的解決方案?

感謝

+1

您可以參考[Azure Media服務](http://mvnrepository.com/artifact/com.microsoft.azure/azure-media/0.9.7)的maven存儲庫的依賴關係來嘗試解決您的問題,但我認爲@Dwyane George是對的,Android沒有媒體服務sdk,而java sdk可能與Android不兼容。 –

回答

2

您可以管理您的Azure的媒體服務(AMS)帳戶,透過要麼在.NET Framework SDK或Java SDK。 .NET Framework SDK提供了用於管理AMS服務的全部功能。 Java SDK支持該功能的一個子集。您可以從微軟的Java Developer Center獲得Java SDK。 AMS爲使用Java SDK提供了一些code samples

如果您想要使用Java SDK中不支持的場景,則可以直接調用AMS REST服務。有關如何從Android調用RESTful服務的更多信息,請參閱post。乾杯。

相關問題