forked from BeautifulPilgrim/MauiAudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NativeAudioService.android.cs
120 lines (103 loc) · 3.73 KB
/
NativeAudioService.android.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using Android.Graphics;
using Android.Media;
using MauiAudio.Platforms.Android;
using MauiAudio.Platforms.Android.CurrentActivity;
using AndroidApp = Android.App;
namespace MauiAudio;
public class NativeAudioService : INativeAudioService
{
IAudioActivity instance;
private MediaPlayer mediaPlayer => instance != null &&
instance.Binder.GetMediaPlayerService() != null ?
instance.Binder.GetMediaPlayerService().mediaPlayer : null;
public bool IsPlaying => mediaPlayer?.IsPlaying ?? false;
public double Duration=>mediaPlayer?.Duration/1000 ?? 0;
public double CurrentPosition => mediaPlayer?.CurrentPosition / 1000 ?? 0;
public event EventHandler<bool> IsPlayingChanged;
public event EventHandler PlayEnded;
public event EventHandler PlayNext;
public event EventHandler PlayPrevious;
public Task InitializeAsync(string audioURI)
{
return InitializeAsync(new MediaPlay() { URL=audioURI });
}
public Task PauseAsync()
{
if (IsPlaying)
{
return instance.Binder.GetMediaPlayerService().Pause();
}
return Task.CompletedTask;
}
public async Task PlayAsync(double position = 0)
{
await instance.Binder.GetMediaPlayerService().Play();
await instance.Binder.GetMediaPlayerService().Seek((int)position * 1000);
}
public Task SetMuted(bool value)
{
instance?.Binder.GetMediaPlayerService().SetMuted(value);
return Task.CompletedTask;
}
public Task SetVolume(int value)
{
instance?.Binder.GetMediaPlayerService().SetVolume(value);
return Task.CompletedTask;
}
public Task SetCurrentTime(double position)
{
return instance.Binder.GetMediaPlayerService().Seek((int)position * 1000);
}
public Task DisposeAsync()
{
instance.Binder?.Dispose();
return Task.CompletedTask;
}
public async Task InitializeAsync(MediaPlay media)
{
if (instance == null)
{
var activity = CrossCurrentActivity.Current;
instance = activity.Activity as IAudioActivity;
}
else
{
instance.Binder.GetMediaPlayerService().isCurrentEpisode = false;
instance.Binder.GetMediaPlayerService().UpdatePlaybackStateStopped();
}
instance.Binder.GetMediaPlayerService().IsPlayingChanged += IsPlayingChanged;
instance.Binder.GetMediaPlayerService().TaskPlayEnded += PlayEnded;
instance.Binder.GetMediaPlayerService().TaskPlayNext += PlayNext;
instance.Binder.GetMediaPlayerService().TaskPlayPrevious += PlayPrevious;
this.instance.Binder.GetMediaPlayerService().PlayingChanged += (object sender, bool e) =>
{
Task.Run(async () => {
if (e)
{
await this.PlayAsync();
}
else
{
await this.PauseAsync();
}
});
IsPlayingChanged?.Invoke(this, e);
};
if(media.Image!=null) instance.Binder.GetMediaPlayerService().Cover= await GetImageBitmapFromUrl(media.Image);
else instance.Binder.GetMediaPlayerService().Cover = null;
instance.Binder.GetMediaPlayerService().AudioUrl =media.URL;
}
private async Task<Bitmap> GetImageBitmapFromUrl(string url)
{
Bitmap imageBitmap = null;
using (var webClient = new HttpClient())
{
var imageBytes = await webClient.GetByteArrayAsync(url);
if (imageBytes != null && imageBytes.Length > 0)
{
imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}
}
return imageBitmap;
}
}