English 中文(简体)
将视频文档与roid相结合,作为参考
原标题:Integrating video file in android app as app background

我需要把视频作为我的背景。 第一夫人 我把视频文件放在可制翻的盒子里,并称作主轴的背景。 但是,在驾照时,我只看到了一个黑色屏幕。 然后,我尝试使用<条码>。

  <VideoView
    android:id="@+id/video"
    android:layout_width="320px"
    android:layout_height="240px"
    android:layout_gravity="center"
    android:background="@raw/hp"/>

在我的活动档案中,我称它有以下法典:

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

        setContentView(R.layout.main);
        VideoView video=(VideoView) findViewById(R.id.video);
        video.start();
}

But still I am not getting the video file there. My main propose is to use a bubble video as background and putting two bubble buttons on it and gives the user a feel like water view screen. Can anyone help me?

Also the video file I want to use from the res folder. Not from SD card or any outer media folder.

最佳回答

我的朋友们,首先,你可以给你打下一个背景:,并在你的屏幕上发挥作用。

请遵循我的步骤,并补充你的努力,请你们也这样做。

删除你的录像带,并将之添加到纸浆中。 请探讨如何创建一种原材料。 但这是简单的。 并将你的录像档案放入。

First of all, create a SurfaceView in your xml like this.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
            android:id="@+id/home_container"  
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent">

<SurfaceView 
        android:id="@+id/surface" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:paddingTop="10dip" />
</Framelayout>

现在,创立一个可执行<条码>的类别。

public class YourMovieActivity extends Activity implements SurfaceHolder.Callback {
    private MediaPlayer mp = null;
    //...
  SurfaceView mSurfaceView=null;

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

        mp = new MediaPlayer();
        mSurfaceView = (SurfaceView) findViewById(R.id.surface);
        mSurfaceView.getHolder().addCallback(this);
        //...
    }
}

现在,你的班子将要求增加未执行的方法。 仅点击“未执行的方法”加上这些方法

现在,你们将能够看到这样的汽车生成方法。

@Override
public void surfaceCreated(SurfaceHolder holder) {

}

在这种方法内,删除以下代码:

@Override
public void surfaceCreated(SurfaceHolder holder) {


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

    mp.setDataSource(video);
    mp.prepare();

    //Get the dimensions of the video
    int videoWidth = mp.getVideoWidth();
    int videoHeight = mp.getVideoHeight();

    //Get the width of the screen
    int screenWidth = getWindowManager().getDefaultDisplay().getWidth();

    //Get the SurfaceView layout parameters
    android.view.ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();

    //Set the width of the SurfaceView to the width of the screen
    lp.width = screenWidth;

    //Set the height of the SurfaceView to match the aspect ratio of the video 
    //be sure to cast these as floats otherwise the calculation will likely be 0
    lp.height = (int) (((float)videoHeight / (float)videoWidth) * (float)screenWidth);

    //Commit the layout parameters
    mSurfaceView.setLayoutParams(lp);        

    //Start video
    mp.setDisplay(holder);
    mp.start();
}
问题回答
/**
 * Created by zoid23 on 05/10/15.
 */
public class IntroVideoSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    private static final String TAG = "INTRO_SF_VIDEO_CALLBACK";
    private MediaPlayer mp;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public IntroVideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public IntroVideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    public IntroVideoSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public IntroVideoSurfaceView(Context context) {
        super(context);
        init();
    }

    private void init (){
        mp = new MediaPlayer();
        getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.intro);
        try {
            mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
            mp.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        int videoWidth = mp.getVideoWidth();
        int videoHeight = mp.getVideoHeight();
        int screenHeight = getHeight();
        android.view.ViewGroup.LayoutParams lp = getLayoutParams();
        lp.height = screenHeight;
        lp.width = (int) (((float)videoWidth / (float)videoHeight) * (float)screenHeight);

        setLayoutParams(lp);
        mp.setDisplay(getHolder());
        mp.setLooping(true);
        mp.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mp.stop();
    }

}

2. 使用印章 对你的Xml的看法,并将你的视频列入raw/intro.mp4

改版卢吉23,避免一些坠毁。

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

http://www.ohchr.org。 您不妨增加<>灵敏度>

  <style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
  </style>

www.un.org/Depts/DGACM/index_spanish.htm 创建档案 IntroVideoSurfaceView.java

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Build;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;

public class IntroVideoSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

  private MediaPlayer mp;
  private boolean has_started = false;

  @TargetApi(Build.VERSION_CODES.LOLLIPOP) public IntroVideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
  }

  public IntroVideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  public IntroVideoSurfaceView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public IntroVideoSurfaceView(Context context) {
    super(context);
    init();
  }

  private void init() {
    mp = new MediaPlayer();
    getHolder().addCallback(this);
  }

  @Override public void surfaceCreated(SurfaceHolder holder) {
    AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.slideshow);
    try {
      if (!has_started) {
        has_started = true;
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
      }

      mp.prepare();
      android.view.ViewGroup.LayoutParams lp = getLayoutParams();
      lp.height = getHeight();
      lp.width = getWidth();

      setLayoutParams(lp);
      mp.setDisplay(getHolder());
      mp.setLooping(true);
      mp.start();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  }

  @Override public void surfaceDestroyed(SurfaceHolder holder) {
    mp.stop();
  }
}

<>在资源/资源/资源/产出中添加“幻灯”。

modification of the activity_main.xml with

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

  <com.androidadvance.videobackground.IntroVideoSurfaceView
      android:id="@+id/surface"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      />

  <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Press Me"
      android:id="@+id/button"
      android:layout_gravity="center_horizontal|bottom"
      android:layout_margin="16dp"
      />
</FrameLayout>

www.un.org/Depts/DGACM/index_spanish.htm 一些说明:。

Adding a video will make your apk huge so you might want to avoid this... From time to time unknown crashes occur even on high end phones (galaxy s6) It s essential to keep the file small.

我已利用该守则在表面上播放录像。

public class VideoPlayOnSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    private MediaPlayer mediaPlayer;
    private boolean has_started = false;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public VideoPlayOnSurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public VideoPlayOnSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public VideoPlayOnSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public VideoPlayOnSurfaceView(Context context) {
        super(context);
        init();
    }

    private void init() {
        mediaPlayer = new MediaPlayer();
        getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.small);
        try {
            if (!has_started) {
                has_started = true;
                mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
            }

            mediaPlayer.prepare();
            android.view.ViewGroup.LayoutParams lp = getLayoutParams();
            lp.height = getHeight();
            lp.width = getWidth();

            setLayoutParams(lp);
            mediaPlayer.setDisplay(holder);
            mediaPlayer.setLooping(true);
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override public void surfaceDestroyed(SurfaceHolder holder) {
        mediaPlayer.stop();
    }
}

xml file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <YourPacakageName.VideoPlayOnSurfaceView
            android:id="@+id/surface"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:paddingTop="10dip" />
</FrameLayout>

我能够把上述职位和以下两个职位结合起来开展工作:

Android 挑 player player player

• 如何在海水体内和甲状腺中附载水器?

我使用过

AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.file_name);
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());

而不是

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

并且使用密码灯来建立媒体角色。

MediaPlayer mp = new MediaPlayer();
SurfaceView mSurfaceView = (SurfaceView) findViewById(R.id.video_surface);
SurfaceHolder holder = mSurfaceView.getHolder();
holder.addCallback(this);




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签