Skip to content

Commit

Permalink
fix: Audio Can't play in Android [Sample]
Browse files Browse the repository at this point in the history
update: Readme
  • Loading branch information
Railway committed Oct 20, 2022
1 parent 2847633 commit c538fde
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions MauiAudio.Sample/Platforms/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

</manifest>
29 changes: 28 additions & 1 deletion MauiAudio.Sample/Platforms/Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using MauiAudio.Platforms.Android;
using MauiAudio.Platforms.Android.CurrentActivity;

namespace MauiAudio.Sample;

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
public class MainActivity : MauiAppCompatActivity, IAudioActivity
{
MediaPlayerServiceConnection mediaPlayerServiceConnection;

public MediaPlayerServiceBinder Binder { get; set; }

public event StatusChangedEventHandler StatusChanged;
public event CoverReloadedEventHandler CoverReloaded;
public event PlayingEventHandler Playing;
public event BufferingEventHandler Buffering;

protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
CrossCurrentActivity.Current.Init(this, savedInstanceState);
NotificationHelper.CreateNotificationChannel(ApplicationContext);
if (mediaPlayerServiceConnection == null)
InitializeMedia();
}

private void InitializeMedia()
{
mediaPlayerServiceConnection = new MediaPlayerServiceConnection(this);
var mediaPlayerServiceIntent = new Intent(ApplicationContext, typeof(MediaPlayerService));
BindService(mediaPlayerServiceIntent, mediaPlayerServiceConnection, Bind.AutoCreate);
}
}
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,21 @@ builder.UseMauiAudio()
#endif
```

### Android
### Platform/Android

AndroidManifest.xml

```
<uses-permission android:name="android.permission.WAKE_LOCK" />
```

MainApplication.cs

```c#
using Android.Content;
using MauiAudio.Platforms.Android;
using MauiAudio.Platforms.Android.CurrentActivity;

public class MainActivity : MauiAppCompatActivity,IAudioActivity
{
MediaPlayerServiceConnection mediaPlayerServiceConnection;
Expand Down

0 comments on commit c538fde

Please sign in to comment.