视频播放器无法正常工作-Android Studio [英] Video player not working - Android Studio

查看:158
本文介绍了视频播放器无法正常工作-Android Studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to add a video played on button click with android studio. However, when I click the button a "sorry, this video cannot be played" message box appears on the emulator screen.

您能帮我看看我要去哪里了.下面是我达到目标的代码

Can you help me see where I'm going wrong. Below is the code I approached the goal with

Trialvideo.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(); 

         }
    }

main.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>

推荐答案

尝试以下代码:

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();
    }
    }

main.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>

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

place the video on the raw folder and run the code. Sometimes video will not be correctly shown on the emulator, try to check it also on the actual device.

这篇关于视频播放器无法正常工作-Android Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆