使用MediaPlayer类播放视频 [英] Playing video using MediaPlayer class
问题描述
我正在尝试使用MediaPlayer
类来播放视频文件.问题是尽管我能听到视频播放中的声音,但视频根本没有显示.
I am trying to make use of MediaPlayer
class for playing a video file. The problem is video is not getting displayed at all though I can hear the sound in the video playing.
以下是活动代码
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的代码
Following is the code of layout 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>
请告诉我需要做什么? 预先感谢.
please tell me what needs to be done? Thanks in advance.
推荐答案
您可以覆盖SurfaceView
类的onSizeChanged()
函数以获取并设置视图的大小.
You can override the onSizeChanged()
function of the SurfaceView
class to get and set the size of the view.
protected void onSizeChanged (int w, int h, int oldw, int oldh)
每当屏幕尺寸改变时,都将调用此类.
When ever the screen size has changed, this class is called.
作为第二种选择,您可以通过设置布局参数来设置尺寸(不确定是否适用于ScrollView):
As a second alternative, you may be able to set the size by setting the layout parameters (unsure if this works with 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);
}
这篇关于使用MediaPlayer类播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!