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 为的:view
main.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 的所有帮助。