我正在使用此Java代码在Android中播放视频。但它只显示黑屏。
我该如何解决这个问题?
package org.apache.android.media;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class VideoViewDemo extends Activity implements OnErrorListener,
OnBufferingUpdateListener, OnCompletionListener,
MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {
private static final String TAG = "VideoPlayer";
private MediaPlayer mp;
private SurfaceView mPreview;
private EditText mPath;
private SurfaceHolder holder;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;
private boolean mIsVideoSizeKnown = false;
private boolean mIsVideoReadyToBePlayed = false;
public File cacheDir;
public Context con;
/**
* Called when the activity is first created.
*/
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
cacheDir = this.getCacheDir();
// Set up the play/pause/reset/stop buttons
mPreview = (SurfaceView) findViewById(R.id.surface);
mPath = (EditText) findViewById(R.id.path);
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
// mPath.setText("http://cityslicker.user.slicker.tech.googlewave.com.s3.amazonaws.com/video.slicker.tech.googlewave.com.3.21.110.9.23.11.914.mp4");
mPath.setText("http://daily3gp.com/vids/747.3gp");
mPlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.pause();
}
}
});
mReset.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.seekTo(0);
}
}
});
mStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.stop();
mp.release();
}
}
});
// Set the transparency
getWindow().setFormat(PixelFormat.TRANSPARENT);
// Set a size for the video screen
holder = mPreview.getHolder();
holder.addCallback(this);
// holder.setFixedSize(400, 300);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// playVideo();
}
private void playVideo() {
try {
final String path = mPath.getText().toString();
Log.v(TAG, "path: " + path);
// If the path has not changed, just start the media player
if (path.equals(current) && mp != null) {
mp.start();
return;
}
current = path;
// Create a new media player and set the listeners
mp = new MediaPlayer();
// setDataSource(path);
// File file = new File(setDataSource(path));
// FileInputStream fis = new FileInputStream(file);
// chk for file is exists or not
if (false) {
File file = new File(this.getCacheDir(),
"mediaplayertmp28220.3gp");
FileInputStream fis = new FileInputStream(file);
// String audioFilePath = getFilesDir().getAbsolutePath() +
// File.separator + "test.mp4";
mp.setDataSource(fis.getFD());
// Set the surface for the video output
mp.setDisplay(holder);
mp.prepareAsync();
} else {
/*
* Runnable r = new Runnable() { public void run() { try {
*
* // setDataSource(path);
*
* } catch (IOException e) { Log.e(TAG, e.getMessage(), e); }
* Log.v(TAG, "Duration: ===>"); //mp.start();
*
* } }; new Thread(r).start();
*/
}
// mp.prepare();
mp.setDataSource(path);
mp.prepare();
mp.setDisplay(holder);
mp.setOnErrorListener(this);
mp.setOnBufferingUpdateListener(this);
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
// holder.setFixedSize(200, 200);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
// mp.start();
// mp.setAudioStreamType(2);
/*
* mMediaPlayer.setDisplay(holder); mMediaPlayer.prepare();
* mMediaPlayer.setOnBufferingUpdateListener(this);
* mMediaPlayer.setOnCompletionListener(this);
* mMediaPlayer.setOnPreparedListener(this);
* mMediaPlayer.setOnVideoSizeChangedListener(this);
* mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
*/
// Set the data source in another thread
// which actually downloads the mp3 or videos
// to a temporary location
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
if (mp != null) {
mp.stop();
mp.release();
}
}
}
/**
* If the user has specified a local url, then we download the url stream to
* a temporary location and then call the setDataSource for that local file
*
* @param path
* @throws IOException
*/
private void setDataSource(String path) throws IOException {
String tempPath = null;
FileInputStream fis;
InputStream stream;
{
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("mediaplayertmp", ".mp4",
this.getCacheDir());
tempPath = temp.getAbsolutePath();
fis = new FileInputStream(temp);
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[1024];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
}
mp.setDataSource(fis.getFD());
mp.setDisplay(holder);
mp.prepareAsync();
try {
stream.close();
} catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
}
public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
Log.e(TAG, "onError---> what:" + what + " extra:" + extra);
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
return true;
}
return false;
}
public void onBufferingUpdate(MediaPlayer arg0, int percent) {
Log.d(TAG, "onBufferingUpdate called ---> percent:" + percent
+ " pos:" + arg0.getCurrentPosition());
}
public void onCompletion(MediaPlayer arg0) {
Log.d(TAG, "onCompletion called");
mp.stop();
mp.release();
}
public void onPrepared(MediaPlayer mediaplayer) {
Log.d(TAG, "onPrepared called");
mIsVideoReadyToBePlayed = true;
mIsVideoSizeKnown = true;
if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
startVideoPlayback();
}
}
private void startVideoPlayback() {
Log.v(TAG, "startVideoPlayback");
holder.setFixedSize(200, 200);
mp.start();
}
public void surfaceCreated(SurfaceHolder surfaceholder) {
Log.d(TAG, "surfaceCreated called");
}
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");
}
}
我的Logcat有 -
08-22 11:26:42.566: DEBUG/AndroidRuntime(487): >>>>>>>>>>>>>> AndroidRuntime START /data/dalvik-cache/data@app@org.apache.android.media-2.apk@classes.dex 08-22 11:26:45.326: DEBUG/PackageManager(58): New package installed in /data/app/org.apache.android.media-2.apk 08-22 11:26:45.507: INFO/ActivityManager(58): Force stopping package org.apache.android.media uid=10048 08-22 11:26:45.656: DEBUG/dalvikvm(58): GC_EXPLICIT freed 7280 objects / 478776 bytes in 135ms 08-22 11:26:45.856: WARN/RecognitionManagerService(58): no available voice recognition services found 08-22 11:26:46.236: DEBUG/dalvikvm(58): GC_EXPLICIT freed 4010 objects / 223072 bytes in 155ms 08-22 11:26:46.286: INFO/installd(34): unlink /data/dalvik-cache/data@app@org.apache.android.media-1.apk@classes.dex 08-22 11:26:46.297: DEBUG/AndroidRuntime(487): Shutting down VM 08-22 11:26:46.307: DEBUG/dalvikvm(487): Debugger has detached; object registry had 1 entries 08-22 11:26:46.336: INFO/AndroidRuntime(487): NOTE: attach of thread 'Binder Thread #3' failed 08-22 11:26:47.426: DEBUG/AndroidRuntime(500): >>>>>>>>>>>>>> AndroidRuntime START prepare() 08-22 11:27:12.644: INFO/Prefetcher(33): [0x14090] cache below low water mark, filling cache. 08-22 11:27:13.996: INFO/AwesomePlayer(33): prefetcher is done preparing 08-22 11:27:14.006: VERBOSE/MediaPlayerDemo(506): onVideoSizeChanged called 08-22 11:27:14.016: DEBUG/MediaPlayerDemo(506): onPrepared called 08-22 11:27:14.016: VERBOSE/MediaPlayerDemo(506): startVideoPlayback 08-22 11:27:14.036: DEBUG/AudioSink(33): bufferCount (4) is too small and increased to 12 08-22 11:27:14.066: ERROR/MemoryHeapBase(33): error opening /dev/pmem_adsp: No such file or directory 08-22 11:27:14.066: INFO/SoftwareRenderer(33): Creating physical memory heap failed, reverting to regular heap. 08-22 11:27:14.347: WARN/AudioFlinger(33): write blocked for 108 msecs, 1461 delayed writes, thread 0xb3f0 08-22 11:27:15.126: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:7 08-22 11:27:16.066: DEBUG/dalvikvm(291): GC_EXPLICIT freed 30 objects / 1472 bytes in 797ms 08-22 11:27:16.226: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:34 08-22 11:27:17.326: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:34 08-22 11:27:18.386: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:32 08-22 11:27:18.477: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol 08-22 11:27:19.362: WARN/AudioFlinger(33): write blocked for 74 msecs, 1497 delayed writes, thread 0xb3f0 08-22 11:27:19.426: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:30 08-22 11:27:20.346: DEBUG/dalvikvm(373): GC_EXPLICIT freed 409 objects / 23448 bytes in 108ms 08-22 11:27:20.486: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:30 08-22 11:27:21.586: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:29 08-22 11:27:22.607: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:33 08-22 11:27:23.747: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:34 08-22 11:27:24.427: WARN/AudioFlinger(33): write blocked for 87 msecs, 1537 delayed writes, thread 0xb3f0 08-22 11:27:24.817: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:34 08-22 11:27:25.856: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:39 08-22 11:27:26.907: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:40 08-22 11:27:27.996: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:41 08-22 11:27:29.107: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:41 08-22 11:27:29.437: WARN/AudioFlinger(33): write blocked for 95 msecs, 1577 delayed writes, thread 0xb3f0 08-22 11:27:30.217: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:42 08-22 11:27:31.288: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:44 08-22 11:27:32.416: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:45 08-22 11:27:33.466: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:45 08-22 11:27:34.536: WARN/AudioFlinger(33): write blocked for 98 msecs, 1619 delayed writes, thread 0xb3f0 08-22 11:27:34.587: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:46 08-22 11:27:35.676: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:48 08-22 11:27:36.796: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:50 08-22 11:27:37.866: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:51 08-22 11:27:38.976: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:52 08-22 11:27:39.556: WARN/AudioFlinger(33): write blocked for 81 msecs, 1660 delayed writes, thread 0xb3f0 08-22 11:27:40.076: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:53 08-22 11:27:41.196: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:52 08-22 11:27:42.326: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:50 08-22 11:27:43.406: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:48 08-22 11:27:44.427: DEBUG/MediaPlayerDemo(506): onBufferingUpdate percent:46 08-22 11:27:44.636: WARN/AudioFlinger(33): write blocked for 86 msecs, 1700 delayed writes, thread 0xb3f0 08-22 11:27:44.926: DEBUG/MediaPlayerDemo(506): onCompletion called 08-22 11:27:44.946: WARN/TimedEventQueue(33): Event 627 was not found in the queue, already cancelled?
答案 0 :(得分:1)
首先。这是使用模拟器还是在真实设备上?
您确定该视频符合视频要求吗?看看this
如果您发现任何错误,请包含一个logcat,您可能会这样做。 只是说这是一个黑屏并没有帮助我们帮助你。当视频编码太昂贵或者它是在模拟器上时,我经历了几个黑屏,因为模拟器可能无法解码您的视频,即使它是在要求之下,因为它是在您的计算机上运行的模拟器。
答案 1 :(得分:1)
我在这个问题上花了一点时间,最后在尝试搞乱设置后意识到模拟器不像手持设备那样触发全屏视图。在发出全屏请求后,我现在看到了视频。
的好消息