![](/img/trans.png)
[英]camera2 api -> mediaCodec -> mediaMuxer -> mp4 file
[英]Why does a newly created Android Camera2 MP4 File fail to play on some players, but not others
我遇到一個問題,即在使用外部庫( 鏈接 )並編寫新的MP4文件時,導致它在多個播放器上無法播放,而在其他播放器上則完全可以播放。
從理論上講,它應該可以立即在所有這些游戲上播放,但事實並非如此。 我正在創建視頻文件,如下所示:
String timeStamp = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss",
Locale.getDefault()).format(new Date());
String imageFileName = "MyApp_" + timeStamp + "";
File video;
File storageDir = getExternalFilesDir(Environment.DIRECTORY_MOVIES);
try {
if(this.saveVideoToGallery){
storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
if(!storageDir.exists()){
boolean createdFolder = false;
try {
createdFolder = storageDir.mkdir();
} catch (Exception e){
e.printStackTrace();
}
if(!createdFolder){
storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
}
}
video = new File(storageDir, imageFileName + ".mp4");
} else {
if(!storageDir.exists()) {
try {
storageDir.mkdir();
} catch (Exception e) {}
}
video = File.createTempFile(
imageFileName, /* prefix */
".mp4", /* suffix */
storageDir /* directory */
);
}
} catch (IOException ioe){
ioe.printStackTrace();
video = new File(storageDir + imageFileName + ".mp4");
}
請注意,布爾值
saveVideoToGallery
只是一個可配置的全局變量,允許寫入外部目錄。
事實證明,該問題與命名架構/文件名邏輯有關。 我最初是這樣寫我的名字的:
String timeStamp = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss",
Locale.getDefault()).format(new Date());
但是,當我刪除冒號(':')並改用下划線(“ _”)時,
String timeStamp = new SimpleDateFormat("yyyy-MM-dd-HH_mm_ss",
Locale.getDefault()).format(new Date());
它解決了這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.