├── .gitignore ├── .npmignore ├── README.md ├── android ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── player │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ └── src │ │ └── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── com │ │ └── xeodou │ │ └── rctplayer │ │ ├── ReactAudio.java │ │ └── ReactPlayerManager.java └── settings.gradle ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | /build 7 | /captures 8 | 9 | .DS_Store 10 | *.swp 11 | 12 | # Gradle 13 | build 14 | .gradle/ 15 | 16 | # Intellij project files 17 | *.iml 18 | *.ipr 19 | *.iws 20 | .idea/ 21 | 22 | # Android 23 | local.properties 24 | 25 | # Xcode 26 | .DS_Store 27 | build/ 28 | *.pbxuser 29 | !default.pbxuser 30 | *.mode1v3 31 | !default.mode1v3 32 | *.mode2v3 33 | !default.mode2v3 34 | *.perspectivev3 35 | !default.perspectivev3 36 | *.xcworkspace 37 | !default.xcworkspace 38 | xcuserdata 39 | profile 40 | *.moved-aside 41 | DerivedData 42 | .idea/ 43 | npm-debug.log 44 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # Android 2 | local.properties 3 | .idea 4 | .gradle 5 | build 6 | *.iml 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### React-Native-Player 2 | 3 | > Media Player for React-Native 4 | 5 | *Only Android support now.* 6 | 7 | #### Integrate 8 | 9 | ##### Android 10 | 11 | * Install via npm 12 | `npm i react-native-player --save-dev` 13 | 14 | * Add dependency to `android/settings.gradle` 15 | ``` 16 | ... 17 | include ':react-native-player' 18 | project(':react-native-player').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-player/android/player') 19 | ``` 20 | 21 | * Add `android/app/build.gradle` 22 | ``` 23 | ... 24 | dependencies { 25 | ... 26 | compile project(':react-native-player') 27 | } 28 | ``` 29 | * Register module in `MainActivity.java` 30 | ``` 31 | import com.xeodou.rctplayer.*; // <--- import 32 | 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | mReactRootView = new ReactRootView(this); 37 | 38 | mReactInstanceManager = ReactInstanceManager.builder() 39 | .setApplication(getApplication()) 40 | .setBundleAssetName("index.android.bundle") 41 | .setJSMainModuleName("index.android") 42 | .addPackage(new ReactPlayerManager()) // <------- here 43 | .addPackage(new MainReactPackage()) 44 | .setUseDeveloperSupport(BuildConfig.DEBUG) 45 | .setInitialLifecycleState(LifecycleState.RESUMED) 46 | .build(); 47 | 48 | mReactRootView.startReactApplication(mReactInstanceManager, "doubanbook", null); 49 | 50 | setContentView(mReactRootView); 51 | } 52 | ``` 53 | 54 | #### Usage 55 | ``` 56 | var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter'); 57 | var Subscribable = require('Subscribable'); 58 | var RCTAudio = require('react-native-player'); 59 | 60 | 61 | var doubanbook = React.createClass({ 62 | 63 | mixins: [Subscribable.Mixin], 64 | 65 | componentWillMount: function() { 66 | this.addListenerOn(RCTDeviceEventEmitter, 67 | 'error', 68 | this.onError); 69 | this.addListenerOn(RCTDeviceEventEmitter, 70 | 'end', 71 | this.onEnd); 72 | this.addListenerOn(RCTDeviceEventEmitter, 73 | 'ready', 74 | this.onReady); 75 | }, 76 | 77 | componentDidMount: function() { 78 | 79 | }, 80 | 81 | onError: function(err) { 82 | console.log(err) 83 | }, 84 | 85 | onEnd: function() { 86 | console.log("end") 87 | }, 88 | 89 | onReady: function() { 90 | console.log("onReady") 91 | }, 92 | 93 | start: function() { 94 | RCTAudio.start() 95 | }, 96 | 97 | pause: function() { 98 | RCTAudio.pause() 99 | }, 100 | 101 | stop: function() { 102 | RCTAudio.stop() 103 | }, 104 | 105 | buttonClicked: function() { 106 | RCTAudio.prepare("https://api.soundcloud.com/tracks/223379813/stream?client_id=f4323c6f7c0cd73d2d786a2b1cdae80c", true) 107 | }, 108 | 109 | render: function() { 110 | return ( 111 | 112 | 115 | Prepare! 116 | 117 | 118 | 121 | Pause! 122 | 123 | 124 | 127 | Start! 128 | 129 | 130 | 131 | ); 132 | } 133 | }) 134 | 135 | ``` 136 | 137 | #### LICENSE 138 | MIT 139 | -------------------------------------------------------------------------------- /android/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.3.0' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /android/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xeodou/react-native-player/0f7b198713f129f095cf0e3b8a0dfc63791cd009/android/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /android/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Oct 26 23:05:37 CST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip 7 | -------------------------------------------------------------------------------- /android/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /android/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /android/player/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.1" 6 | 7 | defaultConfig { 8 | minSdkVersion 16 9 | targetSdkVersion 23 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compile fileTree(dir: 'libs', include: ['*.jar']) 23 | compile 'com.facebook.react:react-native:0.13.+' 24 | compile 'com.google.android.exoplayer:exoplayer:r1.5.1' 25 | } 26 | -------------------------------------------------------------------------------- /android/player/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xeodou/react-native-player/0f7b198713f129f095cf0e3b8a0dfc63791cd009/android/player/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /android/player/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Oct 26 22:52:50 CST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip 7 | -------------------------------------------------------------------------------- /android/player/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /android/player/src/main/java/com/xeodou/rctplayer/ReactAudio.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: xeodou 3 | * @Date: 2015 4 | */ 5 | 6 | package com.xeodou.rctplayer; 7 | 8 | 9 | import android.net.Uri; 10 | import android.os.Build; 11 | import android.support.annotation.Nullable; 12 | 13 | import com.facebook.infer.annotation.Assertions; 14 | import com.facebook.react.bridge.Arguments; 15 | import com.facebook.react.bridge.ReactApplicationContext; 16 | import com.facebook.react.bridge.ReactContextBaseJavaModule; 17 | import com.facebook.react.bridge.ReactMethod; 18 | import com.facebook.react.bridge.WritableMap; 19 | import com.facebook.react.bridge.Callback; 20 | import com.facebook.react.modules.core.DeviceEventManagerModule; 21 | import com.google.android.exoplayer.ExoPlaybackException; 22 | import com.google.android.exoplayer.ExoPlayer; 23 | import com.google.android.exoplayer.MediaCodecAudioTrackRenderer; 24 | import com.google.android.exoplayer.extractor.ExtractorSampleSource; 25 | import com.google.android.exoplayer.upstream.Allocator; 26 | import com.google.android.exoplayer.upstream.DataSource; 27 | import com.google.android.exoplayer.upstream.DefaultAllocator; 28 | import com.google.android.exoplayer.upstream.DefaultUriDataSource; 29 | import com.google.android.exoplayer.util.PlayerControl; 30 | import com.google.android.exoplayer.chunk.Format; 31 | 32 | 33 | public class ReactAudio extends ReactContextBaseJavaModule implements ExoPlayer.Listener { 34 | 35 | public static final String REACT_CLASS = "ReactAudio"; 36 | 37 | private static final int BUFFER_SEGMENT_SIZE = 64 * 1024; 38 | private static final int BUFFER_SEGMENT_COUNT = 256; 39 | 40 | 41 | private ExoPlayer player = null; 42 | private PlayerControl playerControl = null; 43 | private ReactApplicationContext context; 44 | 45 | public ReactAudio(ReactApplicationContext reactContext) { 46 | super(reactContext); 47 | this.context = reactContext; 48 | } 49 | 50 | @Override 51 | public String getName() { 52 | return REACT_CLASS; 53 | } 54 | 55 | private void sendEvent(String eventName, 56 | @Nullable WritableMap params) { 57 | this.context 58 | .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) 59 | .emit(eventName, params); 60 | } 61 | 62 | @ReactMethod 63 | public void removeListener() { 64 | // this could be used on React component unmount 65 | player.removeListener(this); 66 | } 67 | 68 | private static String getDefaultUserAgent() { 69 | StringBuilder result = new StringBuilder(64); 70 | result.append("Dalvik/"); 71 | result.append(System.getProperty("java.vm.version")); // such as 1.1.0 72 | result.append(" (Linux; U; Android "); 73 | 74 | String version = Build.VERSION.RELEASE; // "1.0" or "3.4b5" 75 | result.append(version.length() > 0 ? version : "1.0"); 76 | 77 | // add the model for the release build 78 | if ("REL".equals(Build.VERSION.CODENAME)) { 79 | String model = Build.MODEL; 80 | if (model.length() > 0) { 81 | result.append("; "); 82 | result.append(model); 83 | } 84 | } 85 | String id = Build.ID; // "MASTER" or "M4-rc20" 86 | if (id.length() > 0) { 87 | result.append(" Build/"); 88 | result.append(id); 89 | } 90 | result.append(")"); 91 | return result.toString(); 92 | } 93 | 94 | private MediaCodecAudioTrackRenderer buildRender(String url, String agent, boolean auto) { 95 | Uri uri = Uri.parse(url); 96 | 97 | Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE); 98 | 99 | DataSource dataSource = new DefaultUriDataSource(context, agent); 100 | ExtractorSampleSource sampleSource = new ExtractorSampleSource(uri, dataSource, allocator, 101 | BUFFER_SEGMENT_COUNT * BUFFER_SEGMENT_SIZE); 102 | 103 | MediaCodecAudioTrackRenderer render = new MediaCodecAudioTrackRenderer(sampleSource); 104 | 105 | return render; 106 | } 107 | 108 | @ReactMethod 109 | public void prepare(String url, boolean auto) { 110 | if (player != null ) { 111 | player.release(); 112 | player = null; 113 | } 114 | 115 | player = ExoPlayer.Factory.newInstance(1); 116 | playerControl = new PlayerControl(player); 117 | 118 | String agent = getDefaultUserAgent(); 119 | MediaCodecAudioTrackRenderer render = this.buildRender(url, agent, auto); 120 | player.prepare(render); 121 | player.addListener(this); 122 | player.setPlayWhenReady(auto); 123 | } 124 | 125 | @ReactMethod 126 | public void start() { 127 | Assertions.assertNotNull(player); 128 | playerControl.start(); 129 | } 130 | 131 | @ReactMethod 132 | public void pause() { 133 | Assertions.assertNotNull(player); 134 | playerControl.pause(); 135 | } 136 | 137 | @ReactMethod 138 | public void resume() { 139 | Assertions.assertNotNull(player); 140 | playerControl.start(); 141 | } 142 | 143 | @ReactMethod 144 | public void isPlaying(Callback cb) { 145 | Assertions.assertNotNull(player); 146 | cb.invoke(playerControl.isPlaying()); 147 | } 148 | 149 | @ReactMethod 150 | public void getDuration(Callback cb) { 151 | Assertions.assertNotNull(player); 152 | cb.invoke(playerControl.getDuration()); 153 | } 154 | 155 | @ReactMethod 156 | public void getCurrentPosition(Callback cb) { 157 | Assertions.assertNotNull(player); 158 | cb.invoke(playerControl.getCurrentPosition()); 159 | } 160 | 161 | @ReactMethod 162 | public void getBufferPercentage(Callback cb) { 163 | Assertions.assertNotNull(player); 164 | cb.invoke(playerControl.getBufferPercentage()); 165 | } 166 | 167 | @ReactMethod 168 | public void stop() { 169 | Assertions.assertNotNull(player); 170 | player.release(); 171 | player = null; 172 | } 173 | 174 | @ReactMethod 175 | public void seekTo(int timeMillis) { 176 | Assertions.assertNotNull(player); 177 | playerControl.seekTo(timeMillis); 178 | } 179 | 180 | @Override 181 | public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { 182 | WritableMap params = Arguments.createMap(); 183 | switch (playbackState) { 184 | // event list from official demo example 185 | case ExoPlayer.STATE_BUFFERING: 186 | sendEvent("buffering", params); 187 | break; 188 | case ExoPlayer.STATE_ENDED: 189 | player.release(); 190 | player = null; 191 | sendEvent("end", params); 192 | break; 193 | case ExoPlayer.STATE_IDLE: 194 | sendEvent("idle", params); 195 | break; 196 | case ExoPlayer.STATE_PREPARING: 197 | sendEvent("preparing", params); 198 | break; 199 | case ExoPlayer.STATE_READY: 200 | sendEvent("ready", params); 201 | break; 202 | } 203 | } 204 | 205 | public void onLoadCompleted(int sourceId, long bytesLoaded, int type, int trigger, Format format, 206 | long mediaStartTimeMs, long mediaEndTimeMs, long elapsedRealtimeMs, long loadDurationMs) { 207 | // to make sure media is loaded 208 | WritableMap params = Arguments.createMap(); 209 | sendEvent("loadCompleted", params); 210 | } 211 | 212 | @Override 213 | public void onPlayWhenReadyCommitted() { 214 | 215 | } 216 | 217 | @Override 218 | public void onPlayerError(ExoPlaybackException error) { 219 | WritableMap params = Arguments.createMap(); 220 | params.putString("msg", error.getMessage()); 221 | sendEvent("error", params); 222 | } 223 | } 224 | -------------------------------------------------------------------------------- /android/player/src/main/java/com/xeodou/rctplayer/ReactPlayerManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: xeodou 3 | * @Date: 2015 4 | */ 5 | package com.xeodou.rctplayer; 6 | 7 | import com.facebook.react.ReactPackage; 8 | import com.facebook.react.bridge.JavaScriptModule; 9 | import com.facebook.react.bridge.NativeModule; 10 | import com.facebook.react.bridge.ReactApplicationContext; 11 | import com.facebook.react.uimanager.ViewManager; 12 | 13 | import java.util.ArrayList; 14 | import java.util.Collections; 15 | import java.util.List; 16 | 17 | 18 | public class ReactPlayerManager implements ReactPackage { 19 | 20 | @Override 21 | public List> createJSModules() { 22 | return Collections.emptyList(); 23 | } 24 | 25 | @Override 26 | public List createViewManagers(ReactApplicationContext reactContext) { 27 | return Collections.emptyList(); 28 | } 29 | 30 | @Override 31 | public List createNativeModules( 32 | ReactApplicationContext reactContext) { 33 | List modules = new ArrayList<>(); 34 | modules.add(new ReactAudio(reactContext)); 35 | 36 | return modules; 37 | } 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /android/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':player' 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* 2 | * react-native-audio - index.android.js 3 | * Copyright(c) 2015 xeodou 4 | * MIT Licensed 5 | */ 6 | 7 | var { NativeModules } = require('react-native'); 8 | module.exports = NativeModules.ReactAudio; 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-player", 3 | "version": "0.0.9", 4 | "description": "Media player for react-native", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "npm test" 8 | }, 9 | "repository": "git@github.com:xeodou/react-native-player.git", 10 | "keywords": [ 11 | "react-native", 12 | "android", 13 | "TarsosDSP" 14 | ], 15 | "author": "xeodou@gmail.com", 16 | "license": "MIT", 17 | "devDependencies": {} 18 | } 19 | --------------------------------------------------------------------------------