mirror of
https://github.com/status-im/react-native-transparent-video.git
synced 2026-08-27 11:51:08 +00:00
fix: support loading raw resources on Android
This commit is contained in:
@@ -327,6 +327,30 @@ public class AlphaMovieView extends GLTextureView {
|
||||
}
|
||||
}
|
||||
|
||||
public void setVideoFromResourceId(Context context, int resId) {
|
||||
reset();
|
||||
|
||||
try {
|
||||
AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
|
||||
if (afd == null) return;
|
||||
|
||||
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
|
||||
|
||||
FileDescriptor fileDescriptor = afd.getFileDescriptor();
|
||||
long startOffset = afd.getStartOffset();
|
||||
long endOffset = afd.getLength();
|
||||
mediaPlayer.setDataSource(fileDescriptor, startOffset, endOffset);
|
||||
|
||||
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
|
||||
retriever.setDataSource(fileDescriptor, startOffset, endOffset);
|
||||
|
||||
onDataSourceSet(retriever);
|
||||
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public void setVideoFromFile(FileDescriptor fileDescriptor) {
|
||||
reset();
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.transparentvideo;
|
||||
|
||||
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.Gravity;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
@@ -59,6 +61,12 @@ public class TransparentVideoViewManager extends SimpleViewManager<LinearLayout>
|
||||
view.addView(alphaMovieView);
|
||||
}
|
||||
alphaMovieView.setPacked(true);
|
||||
alphaMovieView.setVideoByUrl(src.getString("uri"));
|
||||
String file = src.getString("uri");
|
||||
try {
|
||||
Integer rawResourceId = Utils.getRawResourceId(reactContext, file);
|
||||
alphaMovieView.setVideoFromResourceId(reactContext, rawResourceId);
|
||||
} catch (RuntimeException e) {
|
||||
alphaMovieView.setVideoByUrl(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.transparentvideo;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
|
||||
public class Utils {
|
||||
public static int getRawResourceId(Context context, String resourceName) {
|
||||
return context.getResources().getIdentifier(resourceName, "raw", context.getPackageName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user