15

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.

Following is the code of activity

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

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.

Chaitanya K
  • 1,827
  • 4
  • 32
  • 67
  • You can create your own surface view and call this surface view in your activity to play the video. Hope this may solve your problem – AndroidOptimist Dec 07 '13 at 06:42
  • I stumbled upon this question while trying for myself, turns out I only needed to call setSurface method in and OnSurfaceAvailable callback. – Raunak Sett Oct 10 '18 at 18:49

4 Answers4

0

set surface View height and width to fill parent in xml file.

  <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" />
Priya
  • 1,763
  • 1
  • 12
  • 11
0

Setting your SurfaceView layout to wrap_content will not size a video to play at the proper aspect ratio.

You can refer to this answer link

https://stackoverflow.com/a/6283676/1441666

And also this blog Play media on surfaceview using android.media.MediaPlayer

Community
  • 1
  • 1
Nirali
  • 13,571
  • 6
  • 40
  • 53
  • Yeah the link you provided was quiet helpful and the app in there was able to play same video file from SD card. But I don't want to use the `ContentProvider` i.e. `MediaStore`. so can you suggest me a link from where I can check how to make use of raw resource to play video using `MediaPlayer` class. – Chaitanya K Mar 14 '13 at 12:14
0

Well, I don't know if you need an specific situation, but have you already tried FullscreenVideoView?

It tries to abstracts all those SurfaceView problems and you can focus only in UI buttons. As It's open, you can subclass FullscreenVideoView and customize as you need.

Hamiseixas
  • 86
  • 1
  • 4
0

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.

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);
}
ivcubr
  • 1,988
  • 9
  • 20
  • 28
Brijesh Singh
  • 416
  • 4
  • 8