Android MediaPlayer不显示视频,只播放音频

时间:2012-01-26 06:40:49

标签: android video media-player emulation surfaceview

我在/sdcard/abc/a.3gp位置有一个视频文件。现在,当我尝试使用我刚刚创建的媒体播放器在Android模拟器中播放文件时,我只获得音频,视频不会显示。屏幕保持空白(当它应该播放视频时)。

以下是我正在使用的媒体播放器代码:

package com.example.helloandroid;
import com.example.helloandroid.R;
import android.app.Activity;
import android.media.AudioManager;
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.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
import android.widget.VideoView;

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

private static final String TAG = "MediaPlayerDemo";

private int mVideoWidth;

private int mVideoHeight;

private MediaPlayer mMediaPlayer;

private SurfaceView mPreview;

private SurfaceHolder holder;

private String path;

private Bundle extras;

private static final String MEDIA = "media";

private static final int LOCAL_AUDIO = 1;

private static final int STREAM_AUDIO = 2;

private static final int RESOURCES_AUDIO = 3;

private static final int LOCAL_VIDEO = 4;

private static final int STREAM_VIDEO = 5;

private boolean mIsVideoSizeKnown = false;

private boolean mIsVideoReadyToBePlayed = false;


/**
 * 
 * Called when the activity is first created.
 */
@Override

public void onCreate(Bundle icicle) {

    super.onCreate(icicle);

    setContentView(R.layout.main);

    mPreview = (SurfaceView) findViewById(R.id.surface_view);

    holder = mPreview.getHolder();

    holder.addCallback(this);

    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    extras = getIntent().getExtras();


}

private void playVideo() {

    doCleanUp();
    try {

             path = "/sdcard/abc/a.3gp";
             if (path == "") {

                    // Tell the user to provide a media file URL.
                    Toast

                            .makeText(

                                    HelloAndroidActivity.this,

                                    "Please edit MediaPlayerDemo_Video Activity, "
                                            + "and set the path variable to your media file path."

                                            + " Your media file must be stored on sdcard.",

                                    Toast.LENGTH_LONG).show();
                }                                   


        // Create a new media player and set the listeners

        mMediaPlayer = new MediaPlayer();

        mMediaPlayer.setDataSource(path);

        mMediaPlayer.setDisplay(holder);

        mMediaPlayer.prepare();

        mMediaPlayer.setOnBufferingUpdateListener(this);

        mMediaPlayer.setOnCompletionListener(this);

        mMediaPlayer.setOnPreparedListener(this);

        mMediaPlayer.setOnVideoSizeChangedListener(this);

        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    }

     catch (Exception e) {

        Log.e(TAG, "error: " + e.getMessage(), e);

    }
}


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");

    playVideo();



}

@Override

protected void onPause() {

    super.onPause();

    releaseMediaPlayer();
    doCleanUp();

}

@Override

protected void onDestroy() {

    super.onDestroy();

    releaseMediaPlayer();
    doCleanUp();

}

private void releaseMediaPlayer() {

    if (mMediaPlayer != null) {

        mMediaPlayer.release();

        mMediaPlayer = null;

    }
}


private void doCleanUp() {

    mVideoWidth = 0;

    mVideoHeight = 0;

    mIsVideoReadyToBePlayed = false;

    mIsVideoSizeKnown = false;

}

private void startVideoPlayback() {

    Log.v(TAG, "startVideoPlayback");

    holder.setFixedSize(mVideoWidth, mVideoHeight);

    mMediaPlayer.start();

   }
  }

............................................... .....................

Layout-> main.xml文件是:

      <?xml version="1.0" encoding="utf-8"?>
      <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/home_container"  
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent">

       <VideoView 
       android:id="@+id/surface_view" 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       />

       </FrameLayout>

............................................... .............................

我不确定错误是在xml文件中还是在代码中。

此外,在日志中没有错误的痕迹。

请帮帮我。

感谢。

3 个答案:

答案 0 :(得分:1)

不幸的是,它似乎是众所周知的Android模拟器的缺点之一。它对媒体播放的支持非常有限,因此大多数情况下视频效果不佳。甚至谷歌工程师也建议使用真实设备而不是模拟器来测试与MediaPlayer相关的问题。

答案 1 :(得分:0)

在.xml文件中使用SurfaceView而不是VideoView

  <?xml version="1.0" encoding="utf-8"?>
  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/home_container"  
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent">

   <SurfaceView 
   android:id="@+id/surface_view" 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />

   </FrameLayout>

一旦解决了问题,请告诉我们。

答案 2 :(得分:0)

为什么不使用videoview,它是媒体播放器的包装器,易于设置 P / s:我在您的代码mMediaPlayer.setDisplay(holder);中看到这是废话,您应该在mMediaPlayer.setDisplay(surfaceHolder);内使用surfaceCreated或在playvideo()

中将surfaceHolder作为参数传递