Last active
July 17, 2020 13:20
-
-
Save kyo504/63174400a4b11dc59697096cb4fc2af8 to your computer and use it in GitHub Desktop.
Android native module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.eegoma.RNAudioPlayer"> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<application | |
android:allowBackup="true"> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.library' | |
android { | |
compileSdkVersion 23 | |
buildToolsVersion "23.0.1" | |
defaultConfig { | |
minSdkVersion 16 | |
targetSdkVersion 22 | |
versionCode 1 | |
versionName "1.0" | |
ndk { | |
abiFilters "armeabi-v7a", "x86" | |
} | |
} | |
lintOptions { | |
warning 'InvalidPackage' | |
} | |
} | |
dependencies { | |
compile 'com.facebook.react:react-native:+' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.eegoma.RNAudioPlayer; | |
import android.media.AudioManager; | |
import android.media.MediaPlayer; | |
import android.net.Uri; | |
import android.support.annotation.Nullable; | |
import com.facebook.react.bridge.Arguments; | |
import com.facebook.react.bridge.Callback; | |
import com.facebook.react.bridge.ReactApplicationContext; | |
import com.facebook.react.bridge.ReactContextBaseJavaModule; | |
import com.facebook.react.bridge.ReactMethod; | |
import com.facebook.react.bridge.WritableMap; | |
import com.facebook.react.modules.core.DeviceEventManagerModule; | |
import java.io.IOException; | |
public class RNAudioPlayerModule extends ReactContextBaseJavaModule implements MediaPlayer.OnPreparedListener { | |
ReactApplicationContext reactContext; | |
MediaPlayer mMediaPlayer; | |
public RNAudioPlayerModule(ReactApplicationContext reactContext) { | |
super(reactContext); | |
this.reactContext = reactContext; | |
this.mMediaPlayer = new MediaPlayer(); | |
this.mMediaPlayer.setOnPreparedListener(this); | |
} | |
@Override | |
public String getName() { | |
return "RNAudioPlayer"; | |
} | |
private void sendEvent(String eventName, @Nullable WritableMap params) { | |
this.reactContext | |
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) | |
.emit(eventName, params); | |
} | |
@ReactMethod | |
public void play(String url) { | |
try { | |
Uri uri = Uri.parse(url); | |
mMediaPlayer.reset(); | |
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); | |
mMediaPlayer.setDataSource(uri.toString()); | |
mMediaPlayer.prepareAsync(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@ReactMethod | |
public void pause() { | |
mMediaPlayer.pause(); | |
WritableMap params = Arguments.createMap(); | |
params.putString("state", "PAUSED"); | |
sendEvent("onPlaybackStateChanged", params); | |
} | |
@ReactMethod | |
public void isPlaying(Callback cb) { | |
cb.invoke(mMediaPlayer.isPlaying()); | |
} | |
@Override | |
public void onPrepared(MediaPlayer mp) { | |
mMediaPlayer.start(); | |
WritableMap params = Arguments.createMap(); | |
params.putString("state", "PLAYING"); | |
sendEvent("onPlaybackStateChanged", params); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.eegoma.RNAudioPlayer; | |
import com.facebook.react.ReactPackage; | |
import com.facebook.react.bridge.JavaScriptModule; | |
import com.facebook.react.bridge.NativeModule; | |
import com.facebook.react.bridge.ReactApplicationContext; | |
import com.facebook.react.uimanager.ViewManager; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public class RNAudioPlayerPackage implements ReactPackage { | |
@Override | |
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) { | |
List<NativeModule> modules = new ArrayList<>(); | |
modules.add(new RNAudioPlayerModule(reactContext)); | |
return modules; | |
} | |
@Override | |
public List<Class<? extends JavaScriptModule>> createJSModules() { | |
return Collections.emptyList(); | |
} | |
@Override | |
public List<ViewManager> createViewManagers( | |
ReactApplicationContext reactContext) { | |
return Collections.emptyList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment