使用VideoView播放mp4资源

发布于 2024-12-18 10:03:04 字数 345 浏览 3 评论 0原文

我已经尝试了一切,但无法使用 VideoView 类播放视频。这是我的代码:

String fileName = "android.resource://"+  getPackageName() +"/raw/test";

VideoView vv = (VideoView) this.findViewById(R.id.videoView);
vv.setVideoURI(Uri.parse(fileName));
vv.start();

视频为 mp4 格式,存在于 raw 文件夹中。

文件的链接是否错误?

如果有任何帮助,我将非常高兴。

I've tried everything but I cant get a video to play using the VideoView class. Here's my code:

String fileName = "android.resource://"+  getPackageName() +"/raw/test";

VideoView vv = (VideoView) this.findViewById(R.id.videoView);
vv.setVideoURI(Uri.parse(fileName));
vv.start();

The video is in mp4-format and exists in the raw folder.

Is the link to the file wrong?

I would be very pleased for any help.

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

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

发布评论

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

评论(3

国产ˉ祖宗 2024-12-25 10:03:04

我建议您使用以下代码,其中我成功运行了我的应用程序

代码如下:

XML 文件:

<Button
    android:id="@+id/btnVideoGallery"
    android:layout_width="75dp"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:text="@string/gallery" />

<Button
    android:id="@+id/btnCancel"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btnVideoGallery"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="22dp"
    android:text="@string/cancel" />

<TextView
    android:id="@+id/lblDisplayImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btnCancel"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:text="@string/below_this_text_video_will_be_displayed"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#000000"
    android:textSize="13dp" />

<VideoView
    android:id="@+id/vvDisplayVideo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lblDisplayImage"
    android:layout_marginTop="15dp" />

Java 文件:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoActivity extends Activity {

    private Button btnVideoGallery,btnCancel;
    private VideoView vvDisplayVideo;
    /** The Constant PICK_VIDEO. */
    private static final int PICK_VIDEO=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_options);

        btnVideoGallery=(Button)findViewById(R.id.btnVideoGallery);
        vvDisplayVideo=(VideoView)findViewById(R.id.vvDisplayVideo);
        btnCancel=(Button)findViewById(R.id.btnCancel);
        vvDisplayVideo.setVisibility(View.GONE);

        btnVideoGallery.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent video=new Intent();
                video.setAction(Intent.ACTION_PICK);
                video.setType("video/*");
                startActivityForResult(video, PICK_VIDEO);

            }
        });

        btnCancel.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent goStartUp=new Intent(VideoActivity.this, StartUpActivity.class);
                goStartUp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(goStartUp);
                finish();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (resultCode==Activity.RESULT_OK && requestCode == PICK_VIDEO) {

            vvDisplayVideo.setVisibility(View.VISIBLE);
            vvDisplayVideo.setVideoURI(data.getData());
            vvDisplayVideo.setFocusable(true);
            MediaController mc=new MediaController(this);
            vvDisplayVideo.setMediaController(mc);
            Log.i("True", "Executed");
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        Intent goStartUp=new Intent(VideoActivity.this, StartUpActivity.class);
        goStartUp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(goStartUp);
        finish();
        return super.onKeyDown(keyCode, event);
    }
}

您还可以根据您的使用修改清单文件:

    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />

<application .....
</application>

</manifest>

I suggest you for the following Code wherein i am running my application successfully

The Code is as Follows:

XML file:

<Button
    android:id="@+id/btnVideoGallery"
    android:layout_width="75dp"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:text="@string/gallery" />

<Button
    android:id="@+id/btnCancel"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btnVideoGallery"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="22dp"
    android:text="@string/cancel" />

<TextView
    android:id="@+id/lblDisplayImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btnCancel"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:text="@string/below_this_text_video_will_be_displayed"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#000000"
    android:textSize="13dp" />

<VideoView
    android:id="@+id/vvDisplayVideo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lblDisplayImage"
    android:layout_marginTop="15dp" />

Java File:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoActivity extends Activity {

    private Button btnVideoGallery,btnCancel;
    private VideoView vvDisplayVideo;
    /** The Constant PICK_VIDEO. */
    private static final int PICK_VIDEO=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_options);

        btnVideoGallery=(Button)findViewById(R.id.btnVideoGallery);
        vvDisplayVideo=(VideoView)findViewById(R.id.vvDisplayVideo);
        btnCancel=(Button)findViewById(R.id.btnCancel);
        vvDisplayVideo.setVisibility(View.GONE);

        btnVideoGallery.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent video=new Intent();
                video.setAction(Intent.ACTION_PICK);
                video.setType("video/*");
                startActivityForResult(video, PICK_VIDEO);

            }
        });

        btnCancel.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Intent goStartUp=new Intent(VideoActivity.this, StartUpActivity.class);
                goStartUp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(goStartUp);
                finish();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (resultCode==Activity.RESULT_OK && requestCode == PICK_VIDEO) {

            vvDisplayVideo.setVisibility(View.VISIBLE);
            vvDisplayVideo.setVideoURI(data.getData());
            vvDisplayVideo.setFocusable(true);
            MediaController mc=new MediaController(this);
            vvDisplayVideo.setMediaController(mc);
            Log.i("True", "Executed");
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        Intent goStartUp=new Intent(VideoActivity.this, StartUpActivity.class);
        goStartUp.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(goStartUp);
        finish();
        return super.onKeyDown(keyCode, event);
    }
}

Also you can modify the Manifest File as per your use:

    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />

<application .....
</application>

</manifest>
尤怨 2024-12-25 10:03:04

我在你的代码中没有看到任何明显的错误。

这是我要跟踪此错误的方法:

  • 确保 mp4 文件格式正确并且可以与其他播放器一起播放
  • 从 log cat 获取播放器的代码错误
  • 尝试播放相同的视频,但从 SDcard

更新您的帖子新信息

祝你好运!

I don't see anything obviously wrong in your code.

here is what i will do to track this bug:

  • make sure the mp4 file is well formed and playable with another player
  • get the code error of the player from the log cat
  • try to play the same video but from SDcard

update your post here with those new informations

Good luck!

伪心 2024-12-25 10:03:04

这个简单的代码对我有用:
(raw 文件夹中的 my_video.mp4 和 VideoView @+id/splash)

VideoView  mVideoView = (VideoView) findViewById(R.id.splash);
Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.my_video);
mVideoView.setVideoURI(videoUri);
mVideoView.start();

This simple code works for me :
(my_video.mp4 in the raw folder and VideoView @+id/splash)

VideoView  mVideoView = (VideoView) findViewById(R.id.splash);
Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.my_video);
mVideoView.setVideoURI(videoUri);
mVideoView.start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文