Skip to content
This repository has been archived by the owner on Jan 20, 2023. It is now read-only.

Commit

Permalink
Android : new FlutterPlugin API & AndroidX migration
Browse files Browse the repository at this point in the history
  • Loading branch information
rxlabz committed Apr 3, 2020
1 parent ca0deac commit 61bc4cc
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 92 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AudioPlayer

A Flutter audio plugin (Swift/Java) to play remote or local audio files on iOS / Android / MacOS / Web.
A Flutter audio plugin (Swift/Java) to play remote or local audio files on iOS / Android / MacOS and Web.

[Online demo](https://rxlabz.github.io/audioplayer/)

Expand Down Expand Up @@ -30,7 +30,7 @@ To use this plugin :
dependencies:
flutter:
sdk: flutter
audioplayer: 0.7.0
audioplayer: 0.8.0
audioplayer_web: 0.7.0
```
Expand Down
3 changes: 3 additions & 0 deletions audioplayer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.8.0
- Android : new FlutterPlugin API & AndroidX migration

## 0.7.0
- add Web support

Expand Down
4 changes: 0 additions & 4 deletions audioplayer/android/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,3 @@
.DS_Store
/build
/captures

/gradle
/gradlew
/gradlew.bat
17 changes: 10 additions & 7 deletions audioplayer/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
group 'bz.rxla.audioplayer'
version '1.0-SNAPSHOT'
version '1.0'

buildscript {
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:3.5.0'
}
}

allprojects {
rootProject.allprojects {
repositories {
google()
jcenter()
}
}

apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
compileSdkVersion 28

defaultConfig {
minSdkVersion 16
targetSdkVersion 25
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
}
}

dependencies {
implementation 'com.android.support:support-annotations:28.0.0'
}
3 changes: 3 additions & 0 deletions audioplayer/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
5 changes: 5 additions & 0 deletions audioplayer/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
4 changes: 1 addition & 3 deletions audioplayer/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bz.rxla.audioplayer"
android:versionCode="1"
android:versionName="0.1.0">
package="bz.rxla.audioplayer">
</manifest>
Original file line number Diff line number Diff line change
@@ -1,71 +1,82 @@
package bz.rxla.audioplayer;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Handler;
import android.util.Log;

import androidx.annotation.NonNull;

import java.io.IOException;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.PluginRegistry.Registrar;

import java.io.IOException;
import java.util.HashMap;

import android.content.Context;
import android.os.Build;

/**
* Android implementation for AudioPlayerPlugin.
*/
public class AudioplayerPlugin implements MethodCallHandler {
/** AudioplayerPlugin */
public class AudioplayerPlugin implements FlutterPlugin, MethodCallHandler {
private static final String ID = "bz.rxla.flutter/audio";

private final MethodChannel channel;
private final AudioManager am;
private MethodChannel channel;
private AudioManager am;
private final Handler handler = new Handler();
private MediaPlayer mediaPlayer;

public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), ID);
channel.setMethodCallHandler(new AudioplayerPlugin(registrar, channel));
}

private AudioplayerPlugin(Registrar registrar, MethodChannel channel) {
this.channel = channel;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), ID);
channel.setMethodCallHandler(this);
Context context = registrar.context().getApplicationContext();

Context context = flutterPluginBinding.getApplicationContext();
this.am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}

// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
// plugin registration via this function while apps migrate to use the new Android APIs
// post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
//
// It is encouraged to share logic between onAttachedToEngine and registerWith to keep
// them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
// depending on the user's project. onAttachedToEngine or registerWith must both be defined
// in the same class.
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "audioplayer");
channel.setMethodCallHandler(new AudioplayerPlugin());
}

@Override
public void onMethodCall(MethodCall call, MethodChannel.Result response) {
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
switch (call.method) {
case "play":
play(call.argument("url").toString());
response.success(null);
result.success(null);
break;
case "pause":
pause();
response.success(null);
result.success(null);
break;
case "stop":
stop();
response.success(null);
result.success(null);
break;
case "seek":
double position = call.arguments();
seek(position);
response.success(null);
result.success(null);
break;
case "mute":
Boolean muted = call.arguments();
mute(muted);
response.success(null);
result.success(null);
break;
default:
response.notImplemented();
result.notImplemented();
}
}

Expand Down Expand Up @@ -144,6 +155,11 @@ public boolean onError(MediaPlayer mp, int what, int extra) {
handler.post(sendData);
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}

private final Runnable sendData = new Runnable(){
public void run(){
try {
Expand Down
13 changes: 3 additions & 10 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,19 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 27
compileSdkVersion 28

lintOptions {
disable 'InvalidPackage'
}

defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "bz.rxla.audioplayerexample"
applicationId "bz.rxla.audioplayer_example"
minSdkVersion 16
targetSdkVersion 27
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
Expand All @@ -53,9 +52,3 @@ android {
flutter {
source '../..'
}

dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
2 changes: 1 addition & 1 deletion example/android/app/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bz.rxla.example">
package="bz.rxla.audioplayer_example">
<!-- Flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
Expand Down
38 changes: 23 additions & 15 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bz.rxla.audioplayerexample">

<!-- The INTERNET permission is required for development. Specifically,
flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>

package="bz.rxla.audioplayer_example">
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
Expand All @@ -20,20 +13,35 @@
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in @style/LaunchTheme). -->
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<!-- Displays an Android View that continues showing the launch screen
Drawable until Flutter paints its first frame, then this splash
screen fades out. A splash screen is useful to avoid any visual
gap between the end of Android's launch screen and the painting of
Flutter's first frame. -->
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package bz.rxla.audioplayer_example;

import io.flutter.embedding.android.FlutterActivity;

public class MainActivity extends FlutterActivity {
}

This file was deleted.

This file was deleted.

10 changes: 10 additions & 0 deletions example/android/app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Theme applied to the Android Window while the process is starting -->
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
<!-- Show a splash screen on the activity. Automatically removed when
Flutter draws its first frame -->
<item name="android:windowBackground">@drawable/launch_background</item>
</style>
<!-- Theme applied to the Android Window as soon as the process has started.
This theme determines the color of the Android Window while your
Flutter UI initializes, as well as behind your Flutter UI while its
running.
This Theme is only used starting with V2 of Flutter's Android embedding. -->
<style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
<item name="android:windowBackground">@android:color/white</item>
</style>
</resources>
2 changes: 1 addition & 1 deletion example/android/app/src/profile/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bz.rxla.example">
package="bz.rxla.audioplayer_example">
<!-- Flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.android.tools.build:gradle:3.5.0'
}
}

Expand Down
3 changes: 3 additions & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip

0 comments on commit 61bc4cc

Please sign in to comment.