20

在此处输入图像描述

我已经与其他人一起寻找现有的潜在解决方案,formats而那些仍然以提到的错误响应。

最后,用相同的设备录制视频并将其用作此应用程序的资源,它仍然无法正常工作。

设备:SGS2,联想a820

视频类型:MPEG-4 video (video/mp4)

    videoView = (VideoView)findViewById(R.id.videoView);
    videoView.setVideoPath("android.resource://raw/sample.mp4");
    videoView.start();
4

8 回答 8

19

请参考下面的代码片段......问题出在路径声明上......

 String uriPath = "android.resource://"+getPackageName()+"/"+R.raw.aha_hands_only_cpr_english;
        Uri uri = Uri.parse(uriPath);
        mVideoView.setVideoURI(uri);

就是这样...

于 2014-07-14T05:48:55.867 回答
11

我尝试了之前提到的所有内容,但事实证明播放 mp4 文件需要 Internet 权限。

<uses-permission android:name="android.permission.INTERNET" />
于 2016-04-19T11:05:22.873 回答
1
public class videoplayer extends Activity {
    private static final String Videos_URL = "*your URI*";

    private VideoView myVideoView;
    private int position = 0;
    private ProgressDialog progressDialog;
    private MediaController mediaControls;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the layout from video_main.xml
        setContentView(R.layout.activity_main);

        if (mediaControls == null) {
            mediaControls = new MediaController(this);
        }

        // Find your VideoView in your video_main.xml layout
        myVideoView = (VideoView) findViewById(R.id.videoView);

        // Create a progressbar
        progressDialog = new ProgressDialog(this);
        // Set progressbar title
        progressDialog.setTitle("ABCDEFGH");
        // Set progressbar message
        progressDialog.setMessage("Loading...");

        progressDialog.setCancelable(false);
        // Show progressbar
        progressDialog.show();

        try {
            Uri video = Uri.parse(Videos_URL);
            myVideoView.setVideoURI(video);
            myVideoView.setMediaController(mediaControls);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

        myVideoView.requestFocus();
        myVideoView.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                progressDialog.dismiss();
                myVideoView.seekTo(position);
                if (position == 0) {
                    myVideoView.start();
                } else {
                    myVideoView.pause();
                }
            }
        });

    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
        myVideoView.pause();
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        position = savedInstanceState.getInt("Position");
        myVideoView.seekTo(position);
    }
}
于 2016-02-15T10:45:13.867 回答
1

确保解码器(目标 sdk)支持您使用的视频格式。您可以使用 VLC Player 将视频格式转换为所需的格式。就我而言,我将 MP4 转换为 WebM 文件并将其加载到 VideoView 中。

这是获取文件路径和播放视频的方法。

String path = "android.resource://" + getPackageName() + "/" + R.raw.sample;
VideoView videoView = (VideoView)findViewById(R.id.videoView);
videoView.setVideoURI(Uri.parse(path));
videoView.start()

来源: 视频格式和编解码器支持 https://developer.android.com/guide/topics/media/media-formats.html

于 2017-02-08T02:07:15.677 回答
0

对于联想a820,以下是需要的:

- MP4/WMV/H.264/H.263 player
- MP3/WAV/WMA/eAAC+ player

确保您的视频符合上述编解码器格式。

于 2014-07-14T05:44:40.730 回答
0

尝试以下代码..

videoView = (VideoView)this.findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
videoView.start();
于 2014-07-14T05:47:37.090 回答
0

只需用此代码替换您的代码,它就会工作:

VideoView videoView = findViewById(R.id.videoView);
        videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.sample);
        videoView.start();
于 2020-06-16T00:54:48.977 回答
-1

试试下面的代码它的工作原理............

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

videoView.setVideoPath("android.resource://"+getPackageName()+"/"+R.raw.videoname;

videoView.start();
于 2018-01-03T06:28:09.517 回答