[英]Playing video using MediaPlayer class
我正在嘗試使用MediaPlayer
類來播放視頻文件。 問題是視頻根本沒有顯示,雖然我可以聽到視頻播放中的聲音。
以下是活動代碼
public class MainActivity extends Activity implements OnClickListener {
private SurfaceView surfaceView;
private Button btnPlay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
addListener();
}
private void init() {
// TODO Auto-generated method stub
surfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
btnPlay = (Button) findViewById(R.id.btnPlay);
}
private void addListener() {
// TODO Auto-generated method stub
btnPlay.setOnClickListener(this);
}
MediaPlayer mediaPlayer = null;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnPlay:
try{
if (mediaPlayer != null) {
mediaPlayer.reset();
mediaPlayer.release();
}else{
getWindow().setFormat(PixelFormat.UNKNOWN);
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.wildlife);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
surfaceHolder.setFixedSize(176, 144);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.start();}
}catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
}
以下是布局xml的代碼
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/btnPlay"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/surfaceView1"
android:layout_alignTop="@+id/surfaceView1"
android:text="@string/play" />
</RelativeLayout>
請告訴我需要做什么? 提前致謝。
設置表面視圖高度和寬度以填充xml文件中的父級。
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
將SurfaceView布局設置為wrap_content 不會調整視頻大小以適當的寬高比播放。
您可以參考此答案鏈接
好吧,我不知道你是否需要特定的情況,但你已經嘗試過FullscreenVideoView嗎?
它試圖抽象所有SurfaceView問題,你只能專注於UI按鈕。 當它打開時,您可以繼承FullscreenVideoView並根據需要進行自定義。
您可以覆蓋SurfaceView
類的onSizeChanged()
函數以獲取和設置視圖的大小。
protected void onSizeChanged (int w, int h, int oldw, int oldh)
當屏幕大小發生變化時,將調用此類。
作為第二種選擇,您可以通過設置布局參數來設置大小(不確定這是否適用於ScrollView):
@Override
public void surfaceCreated(SurfaceHolder holder) {
android.view.ViewGroup.LayoutParams lp = this.getLayoutParams();
lp.width = 1000; // required width
lp.height = 1000; // required height
this.setLayoutParams(lp);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.