views:

887

answers:

1

Hi.

I am new to android development. I just started with creating my own media player application by looking at the code samples given in Android SDK. While I am trying to play a local media file (m.3gp), I am getting IOException error :: error(1,-4). Please can somebody help me in this regard.

Here is my code.

package com.mediaPlayer;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.view.SurfaceHolder;
import android.util.Log;


public class MediaPlayer1 extends Activity implements OnBufferingUpdateListener, OnCompletionListener,OnPreparedListener, OnVideoSizeChangedListener,SurfaceHolder.Callback {

 private static final String TAG = "MediaPlayerByMangesh";

 // Widgets in the application
 private Button btnPlay;
 private Button btnPause;
 private Button btnStop;

 private MediaPlayer mMediaPlayer;
 private String path = "m.3gp";
 private SurfaceHolder holder;
 private int mVideoWidth;
    private int mVideoHeight;
 private boolean mIsVideoSizeKnown = false;
 private boolean mIsVideoReadyToBePlayed = false;


 // For the id of radio button selected
 private int radioCheckedId = -1;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  Log.d(TAG, "Entered OnCreate:");
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  Log.d(TAG, "Creatinging Buttons:");

  btnPlay = (Button) findViewById(R.id.btnPlay);
  btnPause = (Button) findViewById(R.id.btnPause);

  // On app load, the Pause button is disabled
  btnPause.setEnabled(false);

  btnStop = (Button) findViewById(R.id.btnStop);
  btnStop.setEnabled(false);


  /*
   * Attach a OnCheckedChangeListener to the radio group to monitor radio
   * buttons selected by user
   */

  Log.d(TAG, "Watching for Click");
  /* Attach listener to the Calculate and Reset buttons */
  btnPlay.setOnClickListener(mClickListener);
  btnPause.setOnClickListener(mClickListener);
  btnStop.setOnClickListener(mClickListener);
 }

 /*
  * ClickListener for the Calculate and Reset buttons. Depending on the
  * button clicked, the corresponding method is called.
  */
 private OnClickListener mClickListener = new OnClickListener() {

  @Override
  public void onClick(View v) {

  switch (v.getId()) {
  case R.id.btnPlay:
   Log.d(TAG, "Clicked Play Button");
   Log.d(TAG, "Calling Play Function");
   Play();   
   break;
  case R.id.btnPause:
   Pause();
   break;
  case R.id.btnStop:
   Stop();
   break;
  }
  }

 };

 /**
  * Play the Video.
  */
 private void Play() {

   // Create a new media player and set the listeners
        mMediaPlayer = new MediaPlayer();

        Log.d(TAG, "Entered Play function:");
        try
        {
        mMediaPlayer.setDataSource(path);
        }
        catch(IOException ie)
        {
         Log.d(TAG, "IO Exception:" + path);        
        }

        mMediaPlayer.setDisplay(holder);
        try
        {
         mMediaPlayer.prepare();
        }
        catch(IOException ie)
        {
         Log.d(TAG, "IO Exception:" + path);        
        }

        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        //mMediaPlayer.setOnVideoSizeChangedListener(this);
        //mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
   }

 public void onBufferingUpdate(MediaPlayer arg0, int percent) {
        Log.d(TAG, "onBufferingUpdate percent:" + percent);

    }

    public void onCompletion(MediaPlayer arg0) {
        Log.d(TAG, "onCompletion called");
    }

   public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
        Log.v(TAG, "onVideoSizeChanged called");
        if (width == 0 || height == 0) {
            Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
            return;
        }
        mIsVideoSizeKnown = true;
        mVideoWidth = width;
        mVideoHeight = height;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }

    public void onPrepared(MediaPlayer mediaplayer) {
        Log.d(TAG, "onPrepared called");
        mIsVideoReadyToBePlayed = true;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }

 public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
        Log.d(TAG, "surfaceChanged called");

    }

    public void surfaceDestroyed(SurfaceHolder surfaceholder) {
        Log.d(TAG, "surfaceDestroyed called");
    }


    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated called");
        Play();


    }

    private void startVideoPlayback() {
        Log.v(TAG, "startVideoPlayback");
        holder.setFixedSize(176, 144);
        mMediaPlayer.start();
    }

 /**
  * Pause the Video
  */
 private void Pause() {
  ;

  /*
   * If all fields are populated with valid values, then proceed to
   * calculate the tips
   */
   }


 /**
  * Stop the Video.
  */
 private void Stop() {
  ;

  /*
   * If all fields are populated with valid values, then proceed to
   * calculate the tips
   */
   }



 /**
  * Shows the error message in an alert dialog
  *
  * @param errorMessage
  *            String the error message to show
  * @param fieldId
  *            the Id of the field which caused the error. This is required
  *            so that the focus can be set on that field once the dialog is
  *            dismissed.
  */
 private void showErrorAlert(String errorMessage, final int fieldId) {
  new AlertDialog.Builder(this).setTitle("Error")
    .setMessage(errorMessage).setNeutralButton("Close",
      new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog,
         int which) {
        findViewById(fieldId).requestFocus();
       }
      }).show();
 }
}

Thanks, Mangesh Kumar K.

A: 

copy your m.3gp file to sdcard and then set the path accordingly like private String path = "/sdcard/m.3gp"; else for quick testing on emulator push m.3gp file to data folder like adb push m.3gp /data and then set the path like private String path = "/data/m.3gp";

hope this will resolve your problem.

Anand