gpt4 book ai didi

java - 在android中播放单独分割的视频文件{使用java进行分割}

转载 作者:行者123 更新时间:2023-12-01 18:04:27 26 4
gpt4 key购买 nike

我正在开发一个 android 项目,该项目需要我将视频文件分成几个部分(例如每个 5 mb),然后单独播放这些部分。我已成功将视频分成几个部分,但这些部分没有播放。(可能缺少播放器识别的一些播放属性)。如果有人可以提供有关如何仅在代码上下文中播放这些单独部分而无需合并(对于我的项目来说是必需的)的解决方案。附:不能使用任何第三方软件来制作Android应用程序。

这是我正在使用的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

final TextView textView = (TextView) findViewById(R.id.textView);

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("Successfull");
splitFile(new File(Environment.getExternalStorageDirectory() + "/testing1/video.mp4"));
}
}
);
}

public static void splitFile(File f) {
int partCounter = 1;//I like to name parts from 001, 002, 003, ...
//you can change it to 0 if you want 000, 001, ...

int sizeOfFiles = 1024 * 1024 * 10;// 10MB
byte[] buffer = new byte[sizeOfFiles];

try (BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(f))) {//try-with-resources to ensure closing stream
String name = f.getName();

int tmp = 0;
while ((tmp = bis.read(buffer)) > 0) {
//write each chunk of data into separate file with different number in name
File newFile = new File(f.getParent(), name + "."
+ String.format("%03d", partCounter++)+".mp4");
try (FileOutputStream out = new FileOutputStream(newFile)) {
out.write(buffer, 0, tmp);//tmp is chunk size
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

最佳答案

您只是将二进制文件分成几部分。视频文件使用称为编解码器的程序以特殊格式进行编码。如果您希望 block 可以播放,则必须根据原始编解码器确保它们的格式正确。

也许你可以看看类似 this 的内容。它展示了如何使用 ffmpeg 将视频文件分割成可播放的 block 。如果它在 Android 上可用,您也可以尝试使用它。

关于java - 在android中播放单独分割的视频文件{使用java进行分割},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37834139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com