android - 关于在 Android 媒体播放器应用程序中播放媒体文件

标签 android media-player ioexception

我是安卓开发的新手。我刚开始通过查看 Android SDK 中提供的代码示例来创建自己的媒体播放器应用程序。当我尝试播放本地媒体文件 (m.3gp) 时,出现 IOException error::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.

最佳答案

将您的 m.3gp 文件复制到 sdcard,然后相应地设置路径,如 私有(private)字符串路径=“/sdcard/m.3gp”; 否则为了在模拟器上快速测试将 m.3gp 文件推送到数据文件夹,如 adb push m.3gp/数据 然后像这样设置路径 私有(private)字符串路径 = "/data/m.3gp";

希望这能解决您的问题。

关于android - 关于在 Android 媒体播放器应用程序中播放媒体文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2641621/

相关文章:

java - 进度对话框未显示

android - 如何添加自定义按钮状态

java - 音频文件未在Java中播放

java - 在文件上使用 BufferedReader 时出现 FileNotFoundException

java - 从 android 中的应用程序文件夹中读取文件

android - 将 clickEvent 添加到 int 数组

android - 无法使用分页刷新 recyclerView

c# - NAudio.MmException异常

iphone - 基于iOS 4并部署iOS 3的媒体播放器问题

java - 在 Java 中将文本文件加载到对象中