关于在Android媒体播放器应用中播放媒体文件

时间:2010-04-14 22:56:10

标签: android media-player ioexception

我是Android开发的新手。我刚开始创建自己的媒体播放器应用程序,查看Android SDK中给出的代码示例。当我尝试播放本地媒体文件(m.3gp)时,我收到IOException错误:: error(1,-4)。在这方面请有人帮助我。

这是我的代码。

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

谢谢, Mangesh Kumar K。

1 个答案:

答案 0 :(得分:1)

将m.3gp文件复制到sdcard,然后相应地设置路径 private String path =“/ sdcard / m.3gp”; 否则快速测试模拟器将m.3gp文件推送到数据文件夹之类的 adb push m.3gp / data 然后设置路径就好 private String path =“/ data / m.3gp”;

希望这能解决您的问题。