开发者

Play Video From Raw Folder

Evening,

I've just started programming with android and made a few programs and everything so I'm still kind of a novice but im trying to understand it all.

So here's my problem, I'm trying to play a video, the thing is, I got it working when I Stream it from an URL with VideoView over the internet or when i place in on my sdcard.

What I want to do now is play a video I've got in my res/raw folder, but it only plays audio and I don't understand why, it doesn't give any error in my logcat as far as I can see, also couldn't really find a solution with google since most of the answers are about VideoView and just put the video on your SDCard.

Now someone told me I had to use setDisplay (SurfaceHolder) and I've also tried that but I still only get the audio.

I hope somebody can help me to find a solution to this problem.

VideoDemo.java

package nl.melvin.videodemo; 

import android.app.Activity; 
import android.os.Bundle; 
import android.media.MediaPlayer; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 


public class videodemo extends Activity { 

    public SurfaceHolder holder; 
    public SurfaceView surfaceView; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


        MediaPlayer mp = MediaPlayer.create(this, R.raw.mac); 

        mp.setDisplay(holder); 
        mp.start(); 

      } 

} 

XML

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/LinearLayout01"  
     android:layout_width="fill_parent"开发者_高级运维  
     android:layout_height="fill_parent" > 
    <SurfaceView 
        android:id="@+id/surfaceview" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent"> 
     </SurfaceView>> 
</LinearLayout>

I've also tried Uri.parse but it says it can't play the video (.mp4 format).


I had the same problem and still couldn't load the video (only the sound). However I did it like this:

    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    videoView = new VideoView(this);
    videoView.setMediaController(new MediaController(this));
    videoView.setVideoURI(Uri.parse("android.resource://pt.beware.smdemo/" + R.raw.video));
    videoView.requestFocus();
    videoView.start();
    setContentView(videoView);


Try using a VideoView in the XML file instead of SurfaceView. VideoView extends SurfaceView specifically to handle video - SurfaceView doesn't know how to do that.

See the class hierarchy for VideoView

You should then be able to get the holder as...

mp.setDisplay(surfaceView.getHolder());

Also, your code doesn't show you assigning a reference to surfaceView as in...

surfaceView = (SurfaceView) findViewById(R.id.surfaceView);

...and for completeness sake if you use a VideoView then change the class type and variable name in your code and also the Id name in the XML.


mp.setDisplay(surfaceView.getHolder());

Also, your code doesn't show you assigning a reference to SurfaceView as in:

surfaceView = (SurfaceView) findViewById(R.id.surfaceView);

and for completeness sake if you use a VideoView then change the class type and variable name in your code and also the Id name in the XML.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜