0

我正在尝试使用 android studio 添加在按钮单击时播放的视频。但是,当我单击按钮时,模拟器屏幕上会出现“抱歉,无法播放此视频”消息框。

你能帮我看看我哪里出错了。下面是我接近目标的代码

试用视频.java

package android.com.trialvideo;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

public class TrialVideoActivity extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

 /**       // Video view: to view our video
        VideoView video = (VideoView) findViewById(R.id.surface_view);

        //set video path to our video(in this case man-cheetah-gazalle.3gp)
        video.setVideoPath("/raw/jeewan.mp4");
        video.start();

    **/    
     final Button play =(Button)findViewById(R.id.play);
        play.setOnClickListener(new OnClickListener(){
            public void onClick(View V){
                videoPlayer();

            }
        });}

        public void videoPlayer(){

            getWindow().setFormat(PixelFormat.TRANSLUCENT);

            VideoView videoHolder = (VideoView)findViewById(R.id.surface_view);

            videoHolder.setMediaController(new MediaController(this));

            videoHolder.setVideoPath("/TrialVideo/raw/lic.3gp");

            videoHolder.requestFocus();
            videoHolder.start(); 

         }
    }

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   <Button 
        android:layout_height="50dip" 
        android:text="play" 
        android:id="@+id/play" 
        android:layout_width="50dip" 
        >
        </Button>



<VideoView android:id="@+id/surface_view" 
        android:layout_width="475px"
        android:layout_height="440px"
    />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   <Button 
        android:layout_height="50dip" 
        android:text="play" 
        android:id="@+id/play" 
        android:layout_width="50dip" 
        >
        </Button>



<VideoView android:id="@+id/surface_view" 
        android:layout_width="475px"
        android:layout_height="440px"
    />

</LinearLayout>
4

2 回答 2

2

嗨试试下面的代码:

视频播放.java

public class VideoPlaying extends Activity {
    private MediaController mc;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    VideoView vd = (VideoView) findViewById(R.id.VideoView);
    Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.VideoName);
    mc = new MediaController(this);
    vd.setMediaController(mc);
    vd.requestFocus();
    vd.setVideoURI(uri);
    vd.start();
    }
    }

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<VideoView android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/VideoView"></VideoView>

</LinearLayout>

将视频放在原始文件夹中并运行代码。有时视频无法在模拟器上正确显示,请尝试在实际设备上进行检查。

于 2011-06-22T12:43:16.320 回答
0

Android 没有 C: 驱动器。您需要将视频文件放在设备上(例如,将其复制到设备的外部存储器),然后提供VideoView文件的适当路径(例如,使用Environment.getExternalStorageDirectory())。

于 2011-06-22T12:44:34.113 回答