gpt4 book ai didi

Android - 从应用程序的私有(private)文件夹加载视频

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:14 24 4
gpt4 key购买 nike

我有一个问题,我一直在努力寻找解决方案。我的情况如下:

我有一个应用程序可以下载压缩视频并将它们解压缩到应用程序的私有(private)文件夹中,更具体地说是解压缩到子文件夹中。例如在 /data/data/my.app.package.name.here/files/assets/assets-955。

在此文件夹中,视频已解压缩。解压缩过程成功完成,因为我可以在模拟器上运行应用程序时毫无问题地拉取和查看视频。

然后我有另一个 Activity 正在访问此文件夹,找到视频文件并尝试打开它。此时我收到一个错误“抱歉,无法播放此视频”,错误堆栈如下:

01-30 17:36:17.770: D/ContentDemoActivity(6757): File: /data/data/xxxx/files/assets/assets-955/bank_2.mp4
01-30 17:36:17.830: I/MediaPlayer(6757): prepareAsync called in state 4
01-30 17:36:17.830: E/MediaPlayer(6757): error (1, -2147483648)
01-30 17:36:17.860: E/MediaPlayer(6757): Error (1,-2147483648)
01-30 17:36:17.860: D/VideoView(6757): Error: 1,-2147483648
01-30 17:36:19.370: E/MediaPlayer(6757): stop called in state 0
01-30 17:36:19.370: E/MediaPlayer(6757): error (-38, 0)
01-30 17:36:19.370: W/MediaPlayer(6757): mediaplayer went away with unhandled events

我尝试播放视频的代码非常基础:

mView = (VideoView) findViewById(R.id.videoView);

mMediaPlayer = new MediaPlayer();

mView.requestFocus();
mHolder = mView.getHolder();

Log.d(tag, "Populating content. Assets path: " + mAssetsPath);
File f = new File(mAssetsPath);
File[] files = f.listFiles();
Log.d(tag, "File: " + files[0].toString());

mView.setVideoURI(Uri.parse(files[0].toString()));

mView.setMediaController(new MediaController(this));

Activity 的布局有一个普通的 VideoView,没有什么特别的。

最奇怪的是,出于测试目的,我使用了相同的视频,这次是从“原始”文件夹加载它,它运行顺利,没有问题。在那种情况下,虽然我必须加载它:

Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.bank_2);
mVideoView.setVideoURI(video);
mVideoView.start();

我会对下载的视频执行相同的操作,但 API 中似乎没有任何功能允许我从应用程序的私有(private)文件夹加载视频 Uri。

我通过使用文件描述符、videoView 的监听器、指示 MODE_WORLD_READABLE 的标志、videoView 尺寸的预计算等找到了各种解决方案,但都没有得到积极的结果。

简而言之,我的问题是:

  • 为什么我会收到那些根据我在网上找到的与视频文件编码问题相关的错误?
  • 在我的案例中最好使用什么,VideoView 还是 surfaceView?
  • 从应用程序的私有(private)文件夹加载视频并播放视频的理想方法是什么?

谢谢。

编辑

根据 CommonsWare 的建议,我进行了以下实现:

File f = new File(mAssetsPath);
File[] files = f.listFiles();
Log.d(tag, "File: " + files[0].toString());

URI uri = URI.create("file://" + (files[0].toString()));
File file = new File(uri);
try {
Log.d(tag, "1");
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
Log.d(tag, "2");
mMediaPlayer.setDataSource(parcel.getFileDescriptor());
Log.d(tag, "3");
mMediaPlayer.start();
Log.d(tag, "4");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Log.d(tag, "5");

不幸的是,这次我得到了以下错误:

01-31 12:40:11.480: D/ContentDemoActivity(15896): File: /data/data/com.houseofradon.meb/files/assets/assets-955/bank_2.mp4
01-31 12:40:11.480: D/ContentDemoActivity(15896): 1
01-31 12:40:11.480: D/ContentDemoActivity(15896): 2
01-31 12:40:11.500: D/ContentDemoActivity(15896): 3
01-31 12:40:11.500: E/MediaPlayer(15896): start called in state 2
01-31 12:40:11.500: E/MediaPlayer(15896): error (-38, 0)
01-31 12:40:11.500: D/ContentDemoActivity(15896): 4
01-31 12:40:11.500: D/ContentDemoActivity(15896): 5
01-31 12:40:11.530: E/MediaPlayer(15896): Error (-38,0)

因此,当媒体播放器启动时会发生一些事情。错误代码 -38 似乎没有任何具体含义,因为我发现 here .

知道我错过了什么吗???

编辑 #2

我现在使用 mediaPlayer 和 SurfaceView 以及 surfaceHolder 监听器来完成整个过程。这是代码:

mMediaPlayer = new MediaPlayer();

mSurfaceView = (SurfaceView) findViewById(R.id.surface);
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);


public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.d(tag, "surfaceChanged");

try {
mMediaPlayer.setDisplay(mHolder);
Log.d(tag, "7");
mMediaPlayer.start();
Log.d(tag, "8");
} catch (IllegalStateException e) {
e.printStackTrace();
}

Log.d(tag, "9");
}



public void surfaceCreated(SurfaceHolder holder) {
Log.d(tag, "surfaceCreated");
File f = new File(mAssetsPath);
File[] files = f.listFiles();
Log.d(tag, "File: " + files[0].toString());

URI uri = URI.create("file://" + (files[0].toString()));
File file = new File(uri);
try {
Log.d(tag, "1");
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
Log.d(tag, "2");
mMediaPlayer.setDataSource(parcel.getFileDescriptor());
Log.d(tag, "3");
mMediaPlayer.setVolume(100, 100);
Log.d(tag, "4");
mMediaPlayer.prepare();
Log.d(tag, "5");

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Log.d(tag, "6");
}

我可以听到视频的音频,但图片只是纯黑色。我几乎在视频播放结束时也收到一条错误消息:

01-31 14:26:01.300: W/AudioSystem(17165): AudioFlinger server died!
01-31 14:26:01.300: W/IMediaDeathNotifier(17165): media server died
01-31 14:26:01.300: E/MediaPlayer(17165): error (100, 0)
01-31 14:26:01.300: E/MediaPlayer(17165): Error (100,0)

我使用的是实际设备 Samsung Galaxy Tab 10.1。有什么想法吗?

最佳答案

Why do I get those errors which according to what I have found online are errors that are related with problematic encoding of the video file ?

因为媒体播放引擎运行在它自己的进程中,它没有权限读取你的文件。

What is the best things to use in my case, a VideoView or a surfaceView ?

VideoView 包含一个SurfaceView。您是使用 VideoView 还是结合使用 MediaPlayer 和 SurfaceView 由您决定。

Which is the ideal method to load a video from the application's private folder and be able to play it?

创建一个可以提供本地文件的 ContentProvider 并使用提供程序 Uri 而不是 Uri 到本地文件,或者使用 openFileOutput()MODE_WORLD_READABLE 创建本地文件。

关于Android - 从应用程序的私有(private)文件夹加载视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9067468/

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