使用Android的MediaPlayer类播放资源视频
我将视频 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 控制面板的假设确实是错误的。尽管如此,我还无法通过 MediaPlayer
类播放存储在 res/raw
中的视频;当我调用 prepare ()
或音频播放但视频不播放时,它总是失败。正因为如此,我的问题仍然存在。
如果有人遇到我关于如何使用 VideoView 的问题,让我帮您省去寻找如何在 res/raw
中检索视频的麻烦。以下代码段假设您在布局文件 main.xml
中有一个 ID 为 view
的 VideoView
:
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 路径的引用:参考链接
我'我将其保持打开状态,因为我询问是否可以使用 MediaPlayer 而不是 VideoView
在 res/raw
中播放视频。即使我使用VideoView
让它按照我想要的方式工作,据我所知,MediaPlayer
仍然可以做到VideoView
不能。不过我可能是错的,所以如果您只需要显示视频而不需要任何高级内容,我很确定 VideoView
就足够了。
哦,感谢@Ravi 的所有帮助。
I save a video, test.mp4
, in my project's res/raw
directory. I have my SurfaceView
declared as such:
<SurfaceView android:id="@+id/surface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</SurfaceView>
Then, in my activity's onCreate ()
method, I have the following:
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();
}
I don't get why it doesn't show the video, even if I set the display. I'm running on API 7 (2.1.1) and I get the audio. I need a Video-only type of interface, without the control panel. As far as I know, VideoView
, although simpler than MediaPlayer
, has a built-in control layer. Also, I don't see a way to use VideoView
on a resource. If there are workarounds for these two issues I have, or if my assumption of VideoView
is wrong, I'd appreciate being told.
UPDATE: I am currently using the VideoView
class. My assumption really was wrong about automatically having a UI control panel. Still, I have NOT been able to play videos stored in res/raw
via the MediaPlayer
class; it always fails when I call prepare ()
or the audio plays, but the video doesn't. Because of this, my question still stands.
If anyone runs into my question about how I used VideoView, let me save you some trouble in looking for how to retrieve a video in res/raw
. The following snippet assumes you have a VideoView
with an id of view
in a layout file, 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 ();
}
}
Here's the link that I used as a reference to get the uri path: reference link
I'm leaving this open because I asked if I could play video in res/raw
using MediaPlayer, and not VideoView
. Even if I got it to work the way I wanted using VideoView
, as far as I know, there are still things MediaPlayer
can do that VideoView
can't. I could be wrong though, so if you just need to show a video and don't need anything advanced, I'm pretty sure VideoView
will suffice.
Oh, and thanks to @Ravi for all the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为更好的方法是使用 VideoView 而不是 MediaPlayer,因为它为你包裹了播放器、表面和支架,如果你在VideoView中看不到视频,那么可能该视频文件无法在android上播放。
I think a better approach would be to use VideoView instead of MediaPlayer, as it wraps the player, the surface and the holder for you, if you fail to see the video in VideoView, then maybe the video file cannot be played on android.
创建 Surface 时,您需要调用
setDisplay
API。实现
SurfaceHolder.Callback
并设置要推送缓冲区的表面类型setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)
另外,您在哪里调用
setDataSource
API?您可以随时查看 VideoView .java 来看看媒体播放器是如何实现的:)
You need to call the
setDisplay
API when the surface is created.Implement the
SurfaceHolder.Callback
and set the type of surface to push bufferssetType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)
Also where are you calling the
setDataSource
API?You can always take a look at the VideoView.java to see how the media player is implemented :)