【问题标题】:Android Media player, video's not playingAndroid 媒体播放器,视频无法播放
【发布时间】:2015-10-01 13:09:58
【问题描述】:

我在使用 android 媒体播放器时遇到问题。我希望它播放 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) {

}

}

【问题讨论】:

  • 试试mp = MediaPlayer.create(this, "android.resource://"+getPackageName()+"/"+R.raw.video );
  • still not working Error:(32, 25) 错误:找不到合适的方法 create(EnterActivity,String) 方法 MediaPlayer.create(Context,Uri) 不适用(参数不匹配;字符串不能转换为 Uri) 方法 MediaPlayer.create(Context,int) 不适用(参数不匹配;字符串不能转换为 int)

标签: android android-mediaplayer


【解决方案1】:

这是我使用 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();
    }
}

我还为这个视频播放器添加了一些媒体控制器。希望这会有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    相关资源
    最近更新 更多