2012-01-12 32 views
35

Tôi cần sử dụng video làm nền của mình. Trước tiên, tôi đặt tệp video trong thư mục có thể kéo và được gọi là nền của LinearLayout trong tệp main.xml. Nhưng trong khi chạy ứng dụng, tôi chỉ thấy một màn hình màu đen. Sau đó, tôi cố gắng sử dụng VideoView và gọi nó là như sau:Tích hợp tệp video trong ứng dụng android làm nền ứng dụng

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

Trong tập tin hoạt động của tôi, tôi gọi nó với đoạn mã sau:

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

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

Nhưng tôi vẫn không nhận được file video đó. Đề xuất chính của tôi là sử dụng một video bong bóng làm nền và đặt hai nút bong bóng lên đó và mang đến cho người dùng cảm giác như màn hình xem nước. Ai giúp tôi với?

Cũng là tệp video tôi muốn sử dụng từ thư mục res. Không phải từ thẻ SD hoặc bất kỳ thư mục phương tiện bên ngoài nào.

Trả lời

39

Vâng bạn của tôi, trước hết bạn không thể đặt nền cho VideoView của bạn và làm cho nó phát trong nền của màn hình của bạn.

Hãy làm theo các bước của tôi và thêm nỗ lực của bạn và bạn sẽ ở đó.

Xóa video của bạn khỏi thư mục có thể kéo và thêm nó vào thư mục thô. Vui lòng google cách tạo thư mục thô. Nó là đơn giản mặc dù. Và đặt tập tin video của bạn bên trong nó.

Trước hết, hãy tạo một SurfaceView trong xml của bạn như thế này.

<?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> 

Bây giờ, tạo ra một lớp như hình dưới đây mà có thể thực hiện SurfaceView,

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); 
     //... 
    } 
} 

Bây giờ lớp học của bạn sẽ yêu cầu phương pháp chưa thực hiện được thêm vào. Thêm các phương pháp đó bằng cách chỉ cần nhấp vào "Thêm phương chưa thực hiện"

Bây giờ bạn sẽ có thể nhìn thấy một ô tô tạo ra phương pháp như thế này,

@Override 
public void surfaceCreated(SurfaceHolder holder) { 

} 

Và bên trong phương pháp này, thêm đoạn mã dưới đây,

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

Có cách nào tôi có thể làm điều tương tự từ một dịch vụ nền? Giống như tạo trình phát video từ dịch vụ? –

+0

Còn về 'TextureView' thì sao? @AndroSelva –

+3

Điều này không hiệu quả đối với tôi, Hy vọng có ai đó đến với giải pháp tốt hơn hoặc cách hiệu quả để tích hợp tệp video. – XcodeNOOB

0

Tôi đã sử dụng

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

thay vì

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

Và sử dụng mã dưới đây để thiết lập media player.

MediaPlayer mp = new MediaPlayer(); 
SurfaceView mSurfaceView = (SurfaceView) findViewById(R.id.video_surface); 
SurfaceHolder holder = mSurfaceView.getHolder(); 
holder.addCallback(this); 
14
/** 
* 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(); 
    } 

} 

Sử dụng IntroVideoSurfaceView trên xml của bạn và đưa video của bạn trong raw/intro.mp4

+0

Bạn cần đóng afd. cuối cùng { thử { afd.close(); } bắt (IOException e) { e.printStackTrace(); } } –

6

Phiên bản đầy đủ của phiên bản sửa đổi của luigi23 với tránh một số tai nạn.

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

không có hoạt động chính. bạn có thể muốn làm cho nó fullscreen bằng cách thêm

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

Tạo một file 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(); 
    } 
} 

thêm một "slideshow.mp4" tài nguyên/thô

sửa đổi activity_main.xml bằng

<?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> 

Một số lưu ý.

Thêm video sẽ làm cho apk của bạn rất lớn, do đó bạn có thể muốn tránh điều này ... Thỉnh thoảng bị treo chưa biết xảy ra ngay cả trên điện thoại cao cấp (thiên hà s6) Nó là điều cần thiết để giữ cho các tập tin nhỏ.

+2

Mã này hoạt động khi hoạt động được tạo và chạy. Nhưng khi chúng tôi đi đến hoạt động tiếp theo và quay trở lại điều này, nó không thành công với lỗi như: java.lang.IllegalStateException tại android.media.MediaPlayer._prepare (Phương thức gốc) tại android.media.MediaPlayer.prepare (MediaPlayer.java:1135) tại – techtinkerer

+1

Tôi đã đặt mã cho onResume() {mediaplyer.resume()} vv cho onStop, onResume và onPause nhưng không chắc chắn. Nó không thành công tại mp.prepare(); – techtinkerer

+1

không có gì được hiển thị khi hoạt động được công bố. –

1

Tôi đã sử dụng mã này dành cho video chơi trên bề mặt nhìn

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

tập tin xml

<?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> 
Các vấn đề liên quan