Reputation: 9064
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.
Upvotes: 4
Views: 10081
Reputation: 4235
You need to call the setDisplay
API when the surface is created.
Implement the SurfaceHolder.Callback
and set the type of surface to push buffers setType(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 :)
Upvotes: 0