简体   繁体   English

MediaPlayer / VideoView无法播放视频?

[英]MediaPlayer / VideoView not playing video?

I'm new to android development, self taught so can expect a few errors here and there but none so irritating as this. 我是android开发的新手,自学成才,可以在这里和那里看到一些错误,但是没有一个如此令人讨厌。 I've looked over my code a thousand times, searched high and low across multiple websites, books and forums for an answer but I still get the same error so this is a last resort. 我已经查看了我的代码一千次,在多个网站,书籍和论坛中上下搜索以获得答案,但是我仍然遇到相同的错误,因此这是不得已的方法。

I just want to play a hardcoded path to a video in an activity, which is part of my video portfolio app. 我只想在活动中播放视频的硬编码路径,这是我的视频作品集应用程序的一部分。 (The hardcoded path is just for testing, later I will call each video from the related button press, but only after I sort the player out!). (硬编码的路径仅用于测试,稍后我将通过相关的按钮调用来调用每个视频,但前提是我要对播放器进行分类!)。

Here is my code: 这是我的代码:

String path = "android.resource://mysite/res/raw/video1"; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_videoplayer);

    VideoView view = (VideoView) this.findViewById(R.id.vv);
    MediaController controller = (MediaController) new MediaController(this);

    controller.setMediaPlayer(view);
    view.setVideoPath(path);
    view.requestFocus();
    view.start();
}

This snippet is inside my VideoPlayer class, where vv is the VideoView in the XML and video1 is the video to be played. 此代码段位于我的VideoPlayer类中,其中vv是XML中的VideoView,而video1是要播放的视频。 The video is h.264 mp4, 1 minute long and 3mb in size and can be played normally through the default player. 该视频为h.264 mp4,长1分钟,大小为3mb,可以通过默认播放器正常播放。

XML: XML:

<VideoView
    android:id="@+id/vv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

The activity loads but returns the error "Can't play this video". 活动已加载,但返回错误“无法播放此视频”。

Any help I greatly appreciate. 任何帮助我都非常感谢。

     VideoView view = (VideoView) this.findViewById(R.id.vv);

        view.setVideoPath(path);
        view.setMediaController(new MediaController(this));
        view.requestFocus();
        view.start();

Use like this. 这样使用。 hope this will give you some solution. 希望这会给您一些解决方案。

According Android's official documentation Video decoding support for MP4(h.264) added from android 3.0+, so I think you are playing this video on version below 3.0. 根据Android的官方文档,从Android 3.0+开始添加了对MP4(h.264)的视频解码支持,因此我认为您正在3.0以下的版本上播放此视频。 Try it on the device which has android os version 3.0+. 在具有android os 3.0+版本的设备上尝试。

Try this.. 尝试这个..

video0=(VideoView)findViewById(R.id.vv);
video0.setMediaController(new MediaController(this));
video0.setVideoURI(Uri.parse("android.resource://" +getPackageName()+ "/" +R.raw.video1));
video0.requestFocus();
video0.start();

Did you try 你试过了吗
MediaController mc = new MediaController(this); MediaController mc =新的MediaController(this); mc.setAnchorView(vv); mc.setAnchorView(vv);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM