如何在android中播放assets文件夹或raw文件夹中的视频?

发布于 2024-09-05 14:03:32 字数 1075 浏览 3 评论 0原文

我正在尝试在 Android 模拟器中播放视频 我的资源文件夹和原始文件夹中都有视频 但经过一些研究后我仍然无法在模拟器中播放视频 我正在开发 android 2.1 我的视频格式是 mp4 所以我认为这不应该是一个问题 谁能给我一个示例代码,以便我能更理解一点?

问题是我需要显示视频的 VideoView 将仅使用 URI 或文件路径来指向视频。

如果我将视频保存在原始或资产文件夹中,我只能获取输入流或文件描述符,并且似乎没有任何东西可以用于初始化 VideoView。

更新

我仔细研究了 MediaPlayer 示例,并尝试使用资产文件的 FileDescriptor 启动 MediaPlayer,如下面的代码所示:

SurfaceView videoView = (SurfaceView) findViewById(gettingStarted)
SurfaceHolder holder = videoView.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
final MediaPlayer player = new MediaPlayer();
player.setDisplay(holder);

player.setDataSource(getAssets().openFd(fileName).getFileDescriptor());
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {

   @Override
   public void onPrepared(MediaPlayer mp) {
      mp.start();
   }
});

现在我得到以下异常:

java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed

似乎没有其他然后在启动时将文件复制到 SD 卡,这似乎浪费时间和内存。

I am trying to play a video in android emulator
I have the video in my assets folder as well as the raw folder
But after doing some research still i cant play video in my emulator
i am working on android 2.1
My video format is mp4 so i don't think that should be a problem
Could anyone just give me an example code so that i can understand a bit more?

The problem is that the VideoView that I need to display the Video will take only a URI or a File path to point to the Video.

If I save the video in the raw or assets folder I can only get an input stream or a file descriptor and it seems nothing of that can be used to initialize the VideoView.

Update

I took a closer look at the MediaPlayer example and tried to start a MediaPlayer with a FileDescriptor to the assets files as in the code below:

SurfaceView videoView = (SurfaceView) findViewById(gettingStarted)
SurfaceHolder holder = videoView.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
final MediaPlayer player = new MediaPlayer();
player.setDisplay(holder);

player.setDataSource(getAssets().openFd(fileName).getFileDescriptor());
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {

   @Override
   public void onPrepared(MediaPlayer mp) {
      mp.start();
   }
});

Now I get a the following exception:

java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed

It seems there is no other way then copying the file to the sdcard on startup and that seems like a waste of time and memory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

稳稳的幸福 2024-09-12 14:03:33

## 自 Android 1.6 起完美运行 ##< /a>

getWindow().setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder = new VideoView(this);
//if you want the controls to appear
videoHolder.setMediaController(new MediaController(this));
Uri video = getUriFromRawFile(context, R.raw.your_raw_file);
//if your file is named sherif.mp4 and placed in /raw
//use R.raw.sherif
videoHolder.setVideoURI(video);
setContentView(videoHolder);
videoHolder.start();

然后

public static Uri getUriFromRawFile(Context context, @ResRaw int rawResourceId) {
    return Uri.Builder()
        .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
        .authority(context.getPackageName())
        .path(String.valueOf(rawResourceId))
        .build();
}

## 查看完整教程##

## Perfectly Working since Android 1.6 ##

getWindow().setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder = new VideoView(this);
//if you want the controls to appear
videoHolder.setMediaController(new MediaController(this));
Uri video = getUriFromRawFile(context, R.raw.your_raw_file);
//if your file is named sherif.mp4 and placed in /raw
//use R.raw.sherif
videoHolder.setVideoURI(video);
setContentView(videoHolder);
videoHolder.start();

And then

public static Uri getUriFromRawFile(Context context, @ResRaw int rawResourceId) {
    return Uri.Builder()
        .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
        .authority(context.getPackageName())
        .path(String.valueOf(rawResourceId))
        .build();
}

## Check complete tutorial ##

爱你不解释 2024-09-12 14:03:33
String UrlPath="android.resource://"+getPackageName()+"/"+R.raw.ur file name;
videocontainer.setVideoURI(Uri.parse(UrlPath));
videocontainer.start();

其中 videocontainer 类型为 videoview。

String UrlPath="android.resource://"+getPackageName()+"/"+R.raw.ur file name;
videocontainer.setVideoURI(Uri.parse(UrlPath));
videocontainer.start();

where videocontainer of type videoview.

め可乐爱微笑 2024-09-12 14:03:33

尝试:

AssetFileDescriptor afd = getAssets().openFd(fileName);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());

Try:

AssetFileDescriptor afd = getAssets().openFd(fileName);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
飘逸的'云 2024-09-12 14:03:33

PlayVideoActivity.java:

public class PlayVideoActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video);

        VideoView videoView = (VideoView) findViewById(R.id.video_view);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.documentariesandyou));
        videoView.start();
    }
}

activity_play_video.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </VideoView>

</LinearLayout>

PlayVideoActivity.java:

public class PlayVideoActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video);

        VideoView videoView = (VideoView) findViewById(R.id.video_view);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.documentariesandyou));
        videoView.start();
    }
}

activity_play_video.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </VideoView>

</LinearLayout>
抽个烟儿 2024-09-12 14:03:33

如果我没记错的话,当我从资产文件夹加载内容但使用数据库时,我遇到了同样的问题。看来你的资产文件夹中的东西可以有 2 个统计信息:压缩或未压缩。
如果它是压缩的,那么你需要1 Mo的内存来解压缩它,否则你会得到这种异常。由于文档不清楚,因此有多个错误报告 。因此,如果您仍然想使用您的格式,则必须使用未压缩的版本,或者为文件提供 .mp3 或 .png 等扩展名。我知道这有点疯狂,但我加载了一个扩展名为 .mp3 的数据库,它工作得很好。另一种解决方案是使用特殊选项来打包您的应用程序,以告诉它不要压缩某些扩展名。但随后您需要手动构建应用程序并添加“zip -0”选项。
未压缩资产的优点是,发布应用程序之前的 zip-align 阶段将正确对齐数据,以便在加载到内存中时可以直接映射数据。

因此,解决方案:

  • 将文件的扩展名更改为 .mp3 或 .png 并查看它是否有效
  • 手动构建您的应用程序并使用 zip-0 选项

If I remember well, I had the same kind of issue when loading stuff from the asset folder but with a database. It seems that the stuff in your asset folder can have 2 stats : compressed or not.
If it is compressed, then you are allowed 1 Mo of memory to uncompress it, otherwise you will get this kind of exception. There are several bug reports about that because the documentation is not clear. So if you still want to to use your format, you have to either use an uncompressed version, or give an extension like .mp3 or .png to your file. I know it's a bit crazy but I load a database with a .mp3 extension and it works perfectly fine. This other solution is to package your application with a special option to tell it not to compress certain extension. But then you need to build your app manually and add "zip -0" option.
The advantage of an uncompressed assest is that the phase of zip-align before publication of an application will align the data correctly so that when loaded in memory it can be directly mapped.

So, solutions :

  • change the extension of the file to .mp3 or .png and see if it works
  • build your app manually and use the zip-0 option
﹏雨一样淡蓝的深情 2024-09-12 14:03:33

您是否尝试过手动将视频放在 SDCard 上并尝试播放 SDCard 上的视频存储?

如果它有效,您可以在这里找到从 Raw 复制到 SDcard 的方法:

android-将原始文件复制到 sdcard-video-mp4

Did you try to put manually Video on SDCard and try to play video store on SDCard ?

If it's working you can find the way to copy from Raw to SDcard here :

android-copy-rawfile-to-sdcard-video-mp4.

七月上 2024-09-12 14:03:33

我找到了:

Uri raw_uri = Uri.parse(<package_name>/+R.raw.<video_file_name>);

Android 个人找到了该资源,但暂时无法播放。我的文件是 .m4v

I found it :

Uri raw_uri = Uri.parse(<package_name>/+R.raw.<video_file_name>);

Personnaly Android found the resource, but can't play it for now. My file is a .m4v

自找没趣 2024-09-12 14:03:33
VideoView myVideo = (VideoView) rootView.findViewById(R.id.definition_video_view);

//Set video name (no extension)
String myVideoName = "my_video";

//Set app package
String myAppPackage = "com.myapp";

//Get video URI from raw directory
Uri myVideoUri = Uri.parse("android.resource://"+myAppPackage+"/raw/"+myVideoName);

//Set the video URI
myVideo.setVideoURI(myVideoUri);

//Play the video
myVideo.start();

https://gist.github.com/jrejaud/b5eb1b013c27a1f3ae5f

VideoView myVideo = (VideoView) rootView.findViewById(R.id.definition_video_view);

//Set video name (no extension)
String myVideoName = "my_video";

//Set app package
String myAppPackage = "com.myapp";

//Get video URI from raw directory
Uri myVideoUri = Uri.parse("android.resource://"+myAppPackage+"/raw/"+myVideoName);

//Set the video URI
myVideo.setVideoURI(myVideoUri);

//Play the video
myVideo.start();

https://gist.github.com/jrejaud/b5eb1b013c27a1f3ae5f

软的没边 2024-09-12 14:03:33

我认为你需要看看这个 - 它应该有你想要的一切。

编辑:如果您不想查看链接 - 这几乎概括了您想要的内容。

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();

但我仍然建议阅读链接中的信息。

I think that you need to look at this -- it should have everything you want.

EDIT: If you don't want to look at the link -- this pretty much sums up what you'd like.

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();

But I still recommend reading the information at the link.

蹲在坟头点根烟 2024-09-12 14:03:33

听起来你可能和我有同样的问题。不使用 MP4,可以使用 3GPP 吗?我想我使用了 HandBrake 或其他东西作为视频转换器...您只需要确保您有正确的编码器,例如 H.264x 或其他东西。抱歉有点含糊,已经有一段时间了。另外,如果可能的话,不用再担心 android 2.1,而且,有些东西在模拟器中无法工作。因此,如果它可以在很多设备上运行,那么就假设它可以

在这里运行(尤其是对于不同的制造商),您可以阅读我的问题以及我如何解决该问题(很长一段时间后没有人给出答案)。我在这里更详细地解释了:
Android 媒体播放器不工作

It sounds maybe like you have the same issue as i do. instead of using MP4, is 3GPP possible? i think i used like HandBrake or something as the video converter... you just need to make sure you have the right encoder, like H.264x or something. sorry for being a little vague, it's been a while. Also, if it's possible, don't bother worrying about android 2.1 anymore, and also, something things just WILL NOT WORK IN EMULATOR. so if it works on a lot of devices, then just assume it works (especially with different manufacurers)

here, you can read my problem and how i solved the issue (after a long time and no one had an answer). i explained in a lot more detail here:
android media player not working

草莓味的萝莉 2024-09-12 14:03:33

fileName 中,您必须输入文件的相对路径(不带 /asset
例如:

player.setDataSource(
    getAssets().openFd(**"media/video.mp4"**).getFileDescriptor()
);

In the fileName you must put the relative path to the file (without /asset)
for example:

player.setDataSource(
    getAssets().openFd(**"media/video.mp4"**).getFileDescriptor()
);
苦妄 2024-09-12 14:03:33
  1. 使用MediaPlayer API 和示例代码。

  2. 将媒体文件放入 raw 文件夹中。

  3. 获取文件的文件描述符

  4. mediaplayer.setDataSource(fd,offset,length); - 它是一个三
    参数构造函数。

  5. 然后当 onPreared 时,mediaplayer.start();

  1. Use the MediaPlayer API and the sample code.

  2. Put the media file in raw folder.

  3. Get the file descriptor to the file.

  4. mediaplayer.setDataSource(fd,offset,length); - its a three
    argument constructor.

  5. Then when onPreared , mediaplayer.start();

玩套路吗 2024-09-12 14:03:33

主代码

Uri raw_uri=Uri.parse("android.resource://<package_name>/+R.raw.<video_file_name>);

myVideoView=(VideoView)findViewbyID(R.idV.Video_view);

myVideoView.setVideoURI(raw_uri);
myVideoView.setMediaController(new MediaController(this));
myVideoView.start();
myVideoView.requestFocus();

XML

<?xml version="1.0" encoding="utf-8" ?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <VideoView
            android:id="+@/Video_View"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
        />
</LinearLayout>

MainCode

Uri raw_uri=Uri.parse("android.resource://<package_name>/+R.raw.<video_file_name>);

myVideoView=(VideoView)findViewbyID(R.idV.Video_view);

myVideoView.setVideoURI(raw_uri);
myVideoView.setMediaController(new MediaController(this));
myVideoView.start();
myVideoView.requestFocus();

XML

<?xml version="1.0" encoding="utf-8" ?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <VideoView
            android:id="+@/Video_View"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
        />
</LinearLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文