开发者

I'm not able to set a video in the activity

Hello I wish not to disturb but I'm newer to developing program.

I'm trying to make a video playing in an activity but I get always black screen.

My video is in mp4 h264 created with ffmpeg with android profile and plays in quicktime and is set in res/raw/corsolex_1.mp4

I attach the code I'm writing to understand what I miss to do, if someone can help I'm very grateful.

Angelo

this activity is called Clipvideo1

package com.wocmultimedia.VideoEditLesson1;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;

public class Clipvideo1 extends Activity {@Override
     protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.clip1);

// Put the media file into the res/raw folder of your application
MediaPlayer mp = MediaPlayer.create(this, R.raw.corsolex_1);
mp.start();
}}

this is the XML layout called clip1.xml

<FrameLayout android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">
        <VideoView android:id="@+id/videoView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_con开发者_C百科tent"></VideoView>

    </FrameLayout>


You don't link the video to VideoView somehow.

So as you see you have 3 parts of the uri:

  1. "android.resource://"
  2. "com.wocmultimedia.VideoEditLesson1"
  3. your resource

"corsolex_1" is the name of your video

Maybe this code helps:

@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.clip1);

    VideoView videoView = (VideoView) findViewById(R.id.videoView1);

    Uri videoPath = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.corsolex_1);

    videoView.setVideoURI(videoPath);
    videoView.requestFocus();
    videoView.start();

    }


VideoView is a sub-class of SurfaceView which contains and manages a MediaPlayer object. In your XML file you declare the VideoView and in your Java code you create and manages a MediaPlayer object. They are separate and there is no relation between them in your code. So you have 2 options:

  1. Use the VideoView, as described in evilone's answer, and let it manage the MediaPlayer object. This is the best way IMHO, someone already developed the handling of the MediaPlayer object, and wrapped it up nicely for you.
  2. Use MediaPlayer, but manage it properly, including the handling and attaching of the surface and the surface holder. Look at the MediaPlayerDemo_Video in the android developer site to see how it should be done.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜