我正在尝试使用非常简单的代码进行一些视频播放,无论我进行什么更改,我总是遇到同样的问题: onCreate 和 onResume 的最后一行被调用,但没有调用其他任何内容,并且 Activity 以在调用 surfaceCreated 之前未捕获的异常。我究竟做错了什么?
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class VideoPlayerActivity extends Activity implements SurfaceHolder.Callback {
private MediaPlayer mMediaPlayer;
private SurfaceView vview;
private SurfaceHolder holder;
private String videoPath;
private String id;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.videoplayer);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
System.out.println("Inside VideoPlayerActivity.");
vview = (SurfaceView) findViewById(R.id.vplay_surface);
holder = vview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
System.out.println("Surface and holder configured.");
//Bundle dungle = getIntent().getExtras();
//id = dungle.getString("id");
id = "1";
videoPath = getFilesDir().getAbsolutePath().toString() + File.separator + "VID" + id + ".mp4";
System.out.println("PATH: " + videoPath);
//iniElements();
}
@Override
public void onResume() {
iniElements();
}
private void iniElements() {
mMediaPlayer = new MediaPlayer();
System.out.println("MediaPlayer object created.");
}
private void iniPlayer() {
System.out.println("Initiating player!");
try {
mMediaPlayer.setDataSource(videoPath);
mMediaPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.start();
System.out.println("Reached START PLAYER.");
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
System.out.println("Surface changed.");
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
System.out.println("Surface created!");
try {
mMediaPlayer.setDisplay(holder);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Elements configured.");
iniPlayer();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
System.out.println("Surface destroyed.");
}
@Override
public void onPause() {
System.out.println("Activity on pause.");
}
@Override
public void onDestroy () {
System.out.println("Destroying activity.");
}
}
xml文件很简单:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="268dp"
android:layout_height="202dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:orientation="horizontal" >
<SurfaceView
android:id="@+id/vplay_surface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:gravity="center" />
</RelativeLayout>
</LinearLayout>
Logcat 说:
05-06 16:39:51.286: INFO/power(2083): *** acquire_dvfs_lock : lockType : 1 freq : 1200000
05-06 16:39:51.286: INFO/ActivityManager(2083): START {intent.toShortString} from pid 18751
05-06 16:39:51.286: DEBUG/PowerManagerService(2083): acquireDVFSLockLocked : type : DVFS_MIN_LIMIT frequency : 1200000 uid : 1000 pid : 2083 tag : ActivityManager
05-06 16:39:51.286: WARN/ActivityManager(2083): mDVFSLock.acquire()
05-06 16:39:51.306: DEBUG/ActivityManager(2083): Trying to launch applicationName
05-06 16:39:51.326: DEBUG/AndroidRuntime(18751): Shutting down VM
05-06 16:39:51.326: WARN/dalvikvm(18751): threadid=1: thread exiting with uncaught exception (group=0x40c341f8)
05-06 16:39:51.331: WARN/ActivityManager(2083): Force finishing activity r.intent.getComponent().flattenToShortString()
05-06 16:39:51.336: VERBOSE/yamaha::media::VolumeCtrl(1833): VolumeCtrl::createVolume()
05-06 16:39:51.336: DEBUG/yamaha::media::VolumeCtrl(1833): Dir0 : AP Playback Music SP (0001h)
05-06 16:39:51.336: DEBUG/yamaha::media::VolumeCtrl(1833): Dir0Att : AP Playback Music SP (0001h)
05-06 16:39:51.336: DEBUG/yamaha::media::VolumeCtrl(1833): DacMaster : AP Playback Music SP (0001h)
05-06 16:39:51.336: DEBUG/yamaha::media::VolumeCtrl(1833): Sp : AP Playback Music SP (FD81h)
05-06 16:39:51.336: VERBOSE/yamaha::media::VolumeCtrl(1833): VolumeCtrl::setVolume()
05-06 16:39:51.336: DEBUG/yamaha::media::VolumeCtrl(1833): VolumeCtrl::setVolume() FM Playback: Ready
05-06 16:39:51.336: DEBUG/yamaha::media::VolumeCtrl(1833): VolumeCtrl::setVolume() VoiceCall: Ready
05-06 16:39:51.336: WARN/ActivityManager(2083): Force finishing activity r.intent.getComponent().flattenToShortString()
05-06 16:39:51.351: INFO/dumpstate(18883): Check if stand-alone
05-06 16:39:51.356: INFO/dumpstate(18883): begin
05-06 16:39:51.401: INFO/ALSAModule(1833): Initialized ALSA PLAYBACK device hifi
05-06 16:39:51.641: INFO/AudioPolicyManager(1833): stopOutput() output 1, stream 1, session 2190
05-06 16:39:51.681: INFO/AudioFlinger(1833): stop output streamType (0, 1) for 1
05-06 16:39:51.836: WARN/ActivityManager(2083): Activity pause timeout for r
提前致谢!