4

test.mp4我在我的项目res/raw目录中保存了一个视频。我有SurfaceView这样的声明:

  <SurfaceView android:id="@+id/surface"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">
  </SurfaceView>

然后,在我的活动onCreate ()方法中,我有以下内容:

private MediaPlayer mp;
private SurfaceView view;
private SurfaceHolder holder;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    view = (SurfaceView) findViewById (R.id.surface);
    holder = view.getHolder();
    holder.setKeepScreenOn (true); 
    mp = MediaPlayer.create(this.getBaseContext (), R.raw.test);
    mp.setDisplay (holder);
    mp.setLooping (true);
    mp.start();
}

我不明白为什么它不显示视频,即使我设置了显示。我在 API 7 (2.1.1) 上运行,我得到了音频。我需要一个纯视频类型的界面,没有控制面板。据我所知,VideoView虽然比 简单,但MediaPlayer有一个内置的控制层。另外,我看不到VideoView在资源上使用的方法。如果我有这两个问题的解决方法,或者我的假设VideoView是错误的,我会很感激被告知。


更新:我目前正在使用该VideoView课程。关于自动拥有 UI 控制面板,我的假设确实是错误的。尽管如此,我仍然无法播放res/raw通过MediaPlayer课程存储的视频;当我打电话或播放音频时它总是失败prepare (),但视频没有。因此,我的问题仍然存在。

如果有人遇到关于我如何使用 VideoView 的问题,让我为您省去一些麻烦,让您在寻找如何在res/raw. 以下代码段假定您在布局文件中VideoView有一个 id 为的:viewmain.xml

public class generalActivity extends Activity {
    @Override
    public void onCreate (Bundle icicle) {
        super.onCreate (icicle);
        this.setContent (R.layout.main);

        // You know the that view is an instance of VideoView, so cast it as such
        VideoView v = (VideoView) this.findViewById (R.id.view);

        // This is the name of the video WITHOUT the file extension.
        // In this example, the name of the video is 'test.mp4'
        String videoName = "test"

        // You build the URI to your video here
        StringBuilder uriPathBuilder = new StringBuilder ();
        uriPathBuilder.append ("android.resource://");
        uriPathBuilder.append (this.getPackageName ());
        uriPathBuilder.append (File.separator);
        uriPathBuilder.append ("raw");
        uriPathBuilder.append (File.separator);
        uriPathBuilder.append (videoName);
        Uri uri = Uri.parse (uriPathBuilder.toString ());

        view.setVideoURI (uri);
        view.start ();
    }
}

这是我用作获取 uri 路径的参考的链接:参考链接

我将其保持打开状态,因为我问我是否可以res/raw使用 MediaPlayer 播放视频,而不是VideoView. 即使我让它以我想要的方式工作VideoView,据我所知,仍然有一些事情MediaPlayer可以做VideoView而不能做。不过我可能是错的,所以如果你只需要显示一个视频而不需要任何高级的东西,我很确定VideoView就足够了。

哦,感谢@Ravi 的所有帮助。

4

2 回答 2

3

我认为更好的方法是使用VideoView而不是 MediaPlayer,因为它为您包装了播放器、表面和支架,如果您在 VideoView 中看不到视频,那么视频文件可能无法在 android 上播放。

于 2011-08-24T06:05:21.887 回答
0

您需要在setDisplay创建表面时调用 API。

实现SurfaceHolder.Callback并设置要推送缓冲区的表面类型setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)

另外,您在哪里调用setDataSourceAPI?

您可以随时查看VideoView.java以了解媒体播放器是如何实现的 :)

于 2011-08-24T07:59:26.637 回答