簡體   English   中英

關於在Android Media Player應用程序中播放媒體文件

[英]Regarding playing media file in Android media player application

我是android開發的新手。 我只是通過查看Android SDK中提供的代碼示例開始創建自己的媒體播放器應用程序。 當我嘗試播放本地媒體文件(m.3gp)時,出現IOException錯誤::錯誤(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 String path =“ /sdcard/m.3gp”; 否則,為了在模擬器上進行快速測試,將m.3gp文件推送到adb數據文件夾,例如推送m.3gp / data,然后將路徑設置為private String path =“ /data/m.3gp”;

希望這能解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM