在我的应用程序中,我想使用Gluon-mobile VideoService播放URL或本地存储在android设备上的文件中的视频。
这对于URL可以正常使用,但是(在我的环境中)不适用于android设备上存储的文件,
例如/sdcard/DCIM/tmp/1834.mp4.
LogCat显示
V/MediaPlayerService(2431): Create new media retriever from pid 10868
W/AndroidVideoService(10868): Invalid video file: /sdcard/DCIM/tmp/1834.mp4
V/MediaPlayer(10868): resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
我可以使用独立的Android Video-Player在该位置播放文件。
我还尝试将文件以编程方式复制到
提供的目录中StorageService.getPublicStorage("Movies")
(->
/storage/emulated/0/Movies
)或StorageService.getPrivateStorage()
(-> /data/user/0/mypackage/files
)加上“ / tmp /” +“ 1834.mp4”
并从那里通过应用程序播放它,但是LogCat消息再次显示
W/AndroidVideoService(...): Invalid video file ...
VideoService.getPlaylist()的Javadoc说
媒体文件(视频和音频)可以是有效的URL ,也可以是 可以在资源文件夹中提供。
所以无法播放本地存储在android设备上的媒体文件吗?
这是我的build.gradle文件的相关部分:
buildscript {
// ...
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.3.16'
}
}
dependencies {
compile 'com.gluonhq:charm:5.0.1'
// ...
}
// ...
jfxmobile {
downConfig {
version = '3.8.0'
// Do not edit the line below. Use Gluon Mobile Settings in your project context menu instead
plugins 'display', 'lifecycle', 'statusbar', 'storage', 'video'
}
// ...
}
我的手机装有Android 8.1。
添加了用于测试目的的源代码:
int i = 0;
try {
File privateAppStorage = Services.get(StorageService.class)
.flatMap(StorageService::getPrivateStorage)
.orElseThrow(() -> new FileNotFoundException("Could not access private storage."));
String outputfilename = privateAppStorage + "/1834.mp4";
if(i == 0) { // to skip copying set i != 0 on 2nd run
// copy video
File input = new File("/sdcard/DCIM/tmp/1834.mp4");
int li = (int) input.length();
byte[] bFile = new byte[li];
FileInputStream fis = new FileInputStream(input);
fis.read(bFile);
fis.close();
File output = new File(outputfilename);
FileOutputStream fos = new FileOutputStream(output);
fos.write(bFile);
fos.flush();
fos.close();
li = (int) output.length();
/* test copying routine
File testoutput = new File("/sdcard/DCIM/tmp/1834_2.mp4");
FileOutputStream tfos = new FileOutputStream(testoutput);
tfos.write(bFile);
tfos.flush();
tfos.close();
li = (int) testoutput.length();
*/ // end test copying routine
}
// play video
Optional<VideoService> service = Services.get(VideoService.class);
if(service.isPresent()){
VideoService videoService = service.get();
videoService.setControlsVisible(true);
videoService.setFullScreen(true);
Status status = videoService.statusProperty().get();
ObservableList<String> sl = videoService.getPlaylist();
if(sl.size() > 0)
sl.set(0, outputfilename);
else
videoService.getPlaylist().add(outputfilename);
videoService.show();
}
} catch ( IOException e) {
e.printStackTrace();
}
答案 0 :(得分:0)
这是我为自己的环境找到的解决方法。 可以说要播放的文件是
var newArray = admins.map((admin) => ({ name: admin }));
致电
之前String filename = "/sdcard/DCIM/tmp/1834.mp4";
使用以下语句复制文件:
VideoService.getPlaylist.add(filename);
这会绕过我的问题中提到的File privateAppStorage = Services.get(StorageService.class)
.flatMap(StorageService::getPrivateStorage)
.orElseThrow(() -> new FileNotFoundException("Could not access private storage."));
File assets = new File(privateAppStorage.getAbsolutePath() + "/assets");
boolean assets_exists = assets.exists();
if(!assets.exists())
{
assets_exists = assets.mkdir();
}
if(assets_exists && assets.canWrite())
{
File input = new File(filename);
int li = (int) input.length();
byte[] bFile = new byte[li];
FileInputStream fis = new FileInputStream(input);
fis.read(bFile);
fis.close();
File copiedToAssets = new File(assets.getAbsolutePath() + "/" + filename.replaceAll("/", "_"));
FileOutputStream fos = new FileOutputStream(copiedToAssets);
fos.write(bFile);
fos.flush();
fos.close();
}
,并且可以播放视频。