Skip to content

Commit

Permalink
sample: add the position to test
Browse files Browse the repository at this point in the history
fix: can't stop in android
  • Loading branch information
Railway committed Nov 15, 2022
1 parent 0ee672b commit 5000fa1
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
6 changes: 6 additions & 0 deletions MauiAudio.Sample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@
Padding="30,0"
VerticalOptions="Center">
<Entry Text="{Binding Url}"/>
<Label Text="{Binding CurrentTime}">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ChangeTimeCommand}"/>
</Label.GestureRecognizers>
</Label>
<Button Text="Start" Command="{Binding PlayCommand}"/>
<Button Text="Play in Stream" Command="{Binding PlayInStreamCommand}"/>
<Button Text="Stop Play" Command="{Binding StopPlayCommand}"/>
</VerticalStackLayout>
</ScrollView>

Expand Down
10 changes: 10 additions & 0 deletions MauiAudio.Sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@ public MainPage(MainPageViewModel vm)
InitializeComponent();
BindingContext = vm;
}
protected override void OnAppearing()
{
base.OnAppearing();
viewModel.timer.Start();
}
protected override void OnDisappearing()
{
viewModel.timer.Stop();
base.OnDisappearing();
}
}

28 changes: 28 additions & 0 deletions MauiAudio.Sample/MainPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CommunityToolkit.Mvvm.Input;
using MauiAudio.Sample.Services;
using Microsoft.Maui.Dispatching;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -11,11 +12,17 @@ namespace MauiAudio.Sample;
public partial class MainPageViewModel
{
PlayerService playerService;
public IDispatcherTimer timer;
[ObservableProperty]
string currentTime;
[ObservableProperty]
string url= "https://dkihjuum4jcjr.cloudfront.net/ES_ITUNES/Thunderbird/ES_Thunderbird.mp3";
public MainPageViewModel(PlayerService player)
{
playerService = player;
timer = Application.Current.Dispatcher.CreateTimer();
timer.Interval = TimeSpan.FromSeconds(0.2);
timer.Tick += OnTimeChanging;
}
[RelayCommand]
void Play()
Expand All @@ -28,4 +35,25 @@ async void PlayInStream()
var stream = await FileSystem.OpenAppPackageFileAsync("sample.mp3");
await playerService.PlayAsync(new() { Stream=stream, Name = "TempUrl", Author = "TempAuthor" });
}

private void OnTimeChanging(object sender, EventArgs e)
{
var current = TimeSpan.FromSeconds(playerService.CurrentPosition).ToString("hh\\:mm\\:ss");
var duration = TimeSpan.FromSeconds(playerService.Duration).ToString("hh\\:mm\\:ss");
CurrentTime = $"{current}/{duration}";
}
[RelayCommand]
async void ChangeTime()
{
var result=await App.Current.MainPage.DisplayPromptAsync("change time", $"all time: {playerService.Duration}", placeholder: playerService.CurrentPosition.ToString());
if (result != null)
{
await playerService.ChangePosition(double.Parse(result));
}
}
[RelayCommand]
async void StopPlay()
{
await playerService.dispose();
}
}
14 changes: 9 additions & 5 deletions MauiAudio.Sample/Services/PlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,18 @@ private async Task InternalPauseAsync()

private async Task InternalPlayAsync(double position = 0)
{
var canPlay = Connectivity.Current.NetworkAccess==NetworkAccess.Internet?true:false;
//var canPlay = Connectivity.Current.NetworkAccess==NetworkAccess.Internet?true:false;

if (!canPlay)
{
return;
}
//if (!canPlay)
//{
// return;
//}

await audioService.PlayAsync(position);
IsPlaying = true;
}
public async Task dispose()
{
await audioService.DisposeAsync();
}
}
4 changes: 2 additions & 2 deletions MauiAudio/MauiAudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
<Compile Remove="**\windows\**\*.cs" />
<None Include="**\windows\**\*.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>
<ItemGroup>
<!--<ItemGroup>
<Folder Include="Platforms\iOS\" />
<Folder Include="Platforms\MacCatalyst\" />
</ItemGroup>
</ItemGroup>-->
<ItemGroup>
<MauiImage Include="Platforms\Android\Resources\drawable\music.png" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions MauiAudio/NativeAudioService.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public Task SetCurrentTime(double position)

public Task DisposeAsync()
{
instance.Binder?.Dispose();
return Task.CompletedTask;
instance.Binder?.GetMediaPlayerService().Stop();
return Task.CompletedTask;
}

public async Task InitializeAsync(MediaPlay media)
Expand Down

0 comments on commit 5000fa1

Please sign in to comment.