2015-10-01 188 views
0

我有Android媒體播放器的問題。我希望它能夠從res/raw /文件夾播放視頻。我想我已經正確設置了它,但是我得到的只是黑屏。我可以在logcat中看到發現了視頻(所有信息,如分辨率在那裏都可見)。你有什麼想法可能是錯的?Android媒體播放器,視頻沒有播放

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

} 

}

+0

嘗試'mp = MediaPlayer.create(this,「android.resource://」+ getPackageName()+「/」+ R.raw.video);' –

+0

仍然不工作錯誤:(32,方法MediaPlayer.create(Context,Uri)不適用 (參數不匹配;字符串不能轉換爲Uri) 方法MediaPlayer.create(Context,int)方法MediaPlayer.create(Context,Uri)不適用 方法MediaPlayer.create不適用 (參數不匹配;字符串不能轉換爲int) – Bartos

回答

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

我也爲這個視頻播放器添加了一些mediacontrollers。希望這可以幫助

相關問題