簡體   English   中英

Android Media Player,視頻無法播放

[英]Android Media player, video's not playing

我在使用Android Media Player時遇到問題。 我希望它播放res / raw /文件夾中的視頻。 我想我已經正確設置了,但是我得到的只是黑屏。 我可以在logcat中看到找到了視頻(在那里可以看到所有信息,例如分辨率)。 您知道什么地方可能出問題嗎?

public class EnterActivity extends Activity implements SurfaceHolder.Callback {
    SurfaceView mSurfaceView = null;
    public static MediaPlayer mp = null;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enter);
    mp = MediaPlayer.create(this, R.raw.video);
    mSurfaceView = (SurfaceView) findViewById(R.id.video_surface);

}

@Override
public void surfaceCreated(SurfaceHolder holder) {

    //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();
    Log.d("Width Screen", screenWidth + "");

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

}

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

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

}

這是我使用Android應用程序播放簡單視頻的代碼。 我的視頻文件的名稱是“ samplevideo”。 它存在於原始文件夾中。

package in.bhartisoftwares.amit.allamitapps;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

public class videoController extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_controller);

        VideoView videoView = (VideoView) findViewById(R.id.videoView2);
        videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.samplevideo);

        MediaController mediaController = new MediaController(this); // we need session, which is combination of context and session id. So we used "this" keyword here
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
        videoView.start();
    }
}

我還向該視頻播放器添加了一些媒體控制器。 希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM