onCompletion of MediaPlayer on Android device, and resizing video
I am developing an application which plays videos from a playlist. In case there is no video on the device the app dowloads it from a site and moves to the next video in the list.
It works fine on the emulator, but on the real device there is an error when the mediaplayer method "onCompletion" is called:
09-22 11:50:54.553: ERROR/AndroidRuntime(4529): FATAL EXCEPTION: main
09-22 11:50:54.553: ERROR/AndroidRuntime(4529): java.lang.NullPointerException
09-22 11:50:54.553: ERROR/AndroidRuntime(4529): at android.widget.VideoView$3.onCompletion(VideoView.java:347)
09-22 11:50:54.553: ERROR/AndroidRuntime(4529): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1304)
09-22 11:50:54.553: ERROR/AndroidRuntime(4529): at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 11:50:54.553: ERROR/AndroidRuntime(4529): at android.os.Looper.loop(Looper.java:123)
09-22 11:50:54.553: ERROR/AndroidRuntime(4529): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-22 11:50:54.553: ERROR/AndroidRuntime(4529): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 11:50:54.553: ERROR/AndroidRuntime(4529): at java.lang.reflect.Method.invoke(Method.java:521)
09-22 11:50:54.553: ERROR/AndroidRuntime(4529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-22 11:50:54.553: ERROR/AndroidRuntime(4529): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-22 11:50:54.553: ERROR/AndroidRuntime(4529): at dalvik.system.NativeStart.main(Native Method)
So the app runs and plays the 1st video, then crashes. PS the device is dreambook w7
Can anyone help please?
here is the code:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
FLcurrentVideo = 0;
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000L, 0, this);
boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER);
if (isGPS == false) startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
//
//Downloading Play List here
//
File clip=new File(Environment.getExternalStorageDirectory(),
playList[FLcurrentVideo].substring(2, playList[FLcurrentVideo].length()-1)+".mp4");
if (FLReady[FLcurrentVideo]==1) {
video=(VideoView)findViewById(R.id.video);
video.setVideoPath(clip.getAbsolutePath());
video.requestFocus();
video.start();
}
else {
if (FLReady[FLcurrentVideo] == 0) {
new CheckOutVideos(false).execute(FLcurrentVideo);
}
}
video.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer player) {
if (FLstoppedVideo==1) {
FLstoppedVideo=2;
}
else if (FLstoppedVideo==2) {
FLstoppedVideo = 0;
File clip=new File(Environment.getExternalStorageDirectory(),
playList[stoppedVideoMarker].substring(2, playList[stoppedVideoMarker].length()-1)+".mp4");
video=(VideoView)findViewById(R.id.video);
video.setVideoPath(clip.getAbsolutePath());
video.requestFocus();
video.start();
video.seekTo(stoppedVideoTime);
video.resume();
}
else if (FLstoppedVideo==0) {
isGPSplaying = false;
int FL = 1;
while (FL == 1) {
if (FLcurrentVideo<count-1) FLcurrentVideo++;
else FLcurrentVideo = 0;
File clip=new File(Environment.getExternalStorageDirectory(),
playList[FLcurrentVideo].substring(2, playList[FLcurrentVideo].length()-1)+".mp4");
if (FLReady[FLcurrentVideo]==1) {
FL=0;
video=(VideoView)findViewById(R.id.video);
video.setVideoPath(clip.getAbsolutePath());
video.requestFocus();
video.start();
}
else {
FL = 1;
if (FLReady[FLcurrentVideo] == 0) {
new CheckOutVideos(false).execute(FLcurrentVideo);
}
}
}
}
}
});
End the same problem: resizing video works fine on the emulator, but on the real device video size didn't changes, but text and image views displays at the "right" position, above video, wich didn't resized. Here is main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_width="fill_parent">
<VideoView
android:id="@+id/video"
android:layout_width="600dip" android:layout_height="match_parent">
</VideoView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_width="180dip">
<TextView
android:id="@+id/clock"
android:layout_width="wrap_content"
android:layout_height="wra开发者_如何学JAVAp_content"
android:text="time"
>
</TextView>
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="time"
>
</TextView>
<ImageView
android:id="@+id/image1"
android:layout_height="match_parent" android:layout_width="match_parent">
</ImageView>
</LinearLayout>
Thank you in advance.
SentineL
If this helps someone:
It seems, there is a some kind of bug on android 2.2 for VideoView class. The only way i foud to do something - is to do nothing. I throw away VideoView and done all what I nedded useing MediaPlayer class.
精彩评论