Skip to content

Commit

Permalink
Improve asset and raw playing
Browse files Browse the repository at this point in the history
  • Loading branch information
martijn00 committed Sep 25, 2019
1 parent 5111e23 commit 6a218f8
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
3 changes: 2 additions & 1 deletion MediaManager/Media/MediaExtractorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public abstract class MediaExtractorBase : IMediaExtractor

public IList<string> ResourcePrefixes { get; } = new List<string>() {
"android.resource",
"raw"
"raw",
"assets"
};

public IList<string> VideoSuffixes { get; } = new List<string>() {
Expand Down
39 changes: 34 additions & 5 deletions MediaManager/Platforms/Android/Media/MediaExtractor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Android.Content.Res;
using Com.Google.Android.Exoplayer2.Upstream;
using MediaManager.Media;

namespace MediaManager.Platforms.Android.Media
Expand All @@ -24,14 +26,41 @@ public override IList<IMediaExtractorProvider> CreateProviders()
return providers;
}

protected override async Task<string> GetResourcePath(string resourceName)
protected override Task<string> GetResourcePath(string resourceName)
{
string path = null;
using (var stream = MediaManager.Context.Assets.Open(resourceName))
try
{
path = await CopyResourceStreamToFile(stream, "AndroidResources", resourceName).ConfigureAwait(false);
if (int.TryParse(resourceName, out int resourceId))
{
var rawDataSource = new RawResourceDataSource(MediaManager.Context);
var uri = RawResourceDataSource.BuildRawResourceUri(resourceId);
rawDataSource.Open(new DataSpec(uri));
path = rawDataSource.Uri.ToString();
}
}
return path;
catch (Exception ex)
{
path = null;
Console.WriteLine(ex.Message);
}
try
{
if (string.IsNullOrEmpty(path))
{
var assetDataSource = new AssetDataSource(MediaManager.Context);
var dataSpec = new DataSpec(global::Android.Net.Uri.Parse(resourceName));
assetDataSource.Open(dataSpec);
path = assetDataSource.Uri.ToString();
}
}
catch (Exception ex)
{
path = null;
Console.WriteLine(ex.Message);
}

return Task.FromResult(path);
}
}
}
3 changes: 2 additions & 1 deletion MediaManager/Platforms/Ios/Player/IosMediaPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ protected override void Initialize()
if (activationError != null)
Console.WriteLine("Could not activate audio session {0}", activationError.LocalizedDescription);
}
catch
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

Player.InvokeOnMainThread(() =>
Expand Down
Binary file not shown.
6 changes: 6 additions & 0 deletions Samples/ElementPlayer.Android/ElementPlayer.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@
<ItemGroup>
<AndroidResource Include="Resources\drawable\baseline_stop_24.xml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\raw\play.mp3" />
</ItemGroup>
<ItemGroup>
<AndroidAsset Include="Assets\assetplay.mp3" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Binary file not shown.

0 comments on commit 6a218f8

Please sign in to comment.