dy19 dy19 - 1 month ago 13
Android Question

How to set up a custom videoView class

I am using a custom videoView and I am having trouble getting it to even start up. It is pretty basic and I am probably just missing something small. Thanks in advance

Here is my main class:

public class MainActivity extends Activity {

myVideoView v;

private static final String TAG = "MEDIA";
private TextView tv;


MediaPlayer.OnCompletionListener videoListener = new MediaPlayer.OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
Log.d("onCompletion", "Total time paused: " + v.getTotalTimeMillis());
}
};


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v = new myVideoView(this);
setContentView(R.layout.fragment_main);

String fileName = "android.resource://" + getPackageName() + "/" + R.raw.leftwrist;
v = (myVideoView) findViewById(R.id.myVideoView);
v.setVideoURI(Uri.parse(fileName));
v.start();


v.setOnCompletionListener(videoListener);

}

public void PlayPause(View view) {
//Do something in response to button press
if(v.isPlaying()){
v.pause();
} else {
v.start();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}}


Here is my custom videoView class:

public class myVideoView extends VideoView{

long lastPausedTime = 0; // The time of the last pause (milliseconds)
long totalPausedTime = 0; // The total time paused (milliseconds)

public myVideoView(Context context) {
super(context);
}

@Override
public void pause() {
lastPausedTime = System.currentTimeMillis();

super.pause();
}

@Override
public void start() {
if (lastPausedTime != 0) {
totalPausedTime += System.currentTimeMillis() - lastPausedTime;
}
super.start();
}

public long getTotalTimeMillis() {
return totalPausedTime;
}

}


Here is my error log:

06-23 10:30:33.620: E/AndroidRuntime(1183): FATAL EXCEPTION: main
06-23 10:30:33.620: E/AndroidRuntime(1183): Process: com.example.watchvideo, PID: 1183
06-23 10:30:33.620: E/AndroidRuntime(1183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.watchvideo/com.example.watchvideo.MainActivity}: java.lang.ClassCastException: android.widget.VideoView cannot be cast to com.example.watchvideo.myVideoView
06-23 10:30:33.620: E/AndroidRuntime(1183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-23 10:30:33.620: E/AndroidRuntime(1183): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-23 10:30:33.620: E/AndroidRuntime(1183): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-23 10:30:33.620: E/AndroidRuntime(1183): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-23 10:30:33.620: E/AndroidRuntime(1183): at android.os.Handler.dispatchMessage(Handler.java:102)
06-23 10:30:33.620: E/AndroidRuntime(1183): at android.os.Looper.loop(Looper.java:136)
06-23 10:30:33.620: E/AndroidRuntime(1183): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-23 10:30:33.620: E/AndroidRuntime(1183): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 10:30:33.620: E/AndroidRuntime(1183): at java.lang.reflect.Method.invoke(Method.java:515)
06-23 10:30:33.620: E/AndroidRuntime(1183): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-23 10:30:33.620: E/AndroidRuntime(1183): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-23 10:30:33.620: E/AndroidRuntime(1183): at dalvik.system.NativeStart.main(Native Method)
06-23 10:30:33.620: E/AndroidRuntime(1183): Caused by: java.lang.ClassCastException: android.widget.VideoView cannot be cast to com.example.watchvideo.myVideoView
06-23 10:30:33.620: E/AndroidRuntime(1183): at com.example.watchvideo.MainActivity.onCreate(MainActivity.java:53)
06-23 10:30:33.620: E/AndroidRuntime(1183): at android.app.Activity.performCreate(Activity.java:5231)
06-23 10:30:33.620: E/AndroidRuntime(1183): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-23 10:30:33.620: E/AndroidRuntime(1183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-23 10:30:33.620: E/AndroidRuntime(1183): ... 11 more


Edit: New error log

06-23 10:45:34.150: E/AndroidRuntime(1254): FATAL EXCEPTION: main
06-23 10:45:34.150: E/AndroidRuntime(1254): Process: com.example.watchvideo, PID: 1254
06-23 10:45:34.150: E/AndroidRuntime(1254): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.watchvideo/com.example.watchvideo.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.watchvideo.myVideoView
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.os.Handler.dispatchMessage(Handler.java:102)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.os.Looper.loop(Looper.java:136)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-23 10:45:34.150: E/AndroidRuntime(1254): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 10:45:34.150: E/AndroidRuntime(1254): at java.lang.reflect.Method.invoke(Method.java:515)
06-23 10:45:34.150: E/AndroidRuntime(1254): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-23 10:45:34.150: E/AndroidRuntime(1254): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-23 10:45:34.150: E/AndroidRuntime(1254): at dalvik.system.NativeStart.main(Native Method)
06-23 10:45:34.150: E/AndroidRuntime(1254): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.watchvideo.myVideoView
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.view.LayoutInflater.createView(LayoutInflater.java:603)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-23 10:45:34.150: E/AndroidRuntime(1254): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.app.Activity.setContentView(Activity.java:1929)
06-23 10:45:34.150: E/AndroidRuntime(1254): at com.example.watchvideo.MainActivity.onCreate(MainActivity.java:50)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.app.Activity.performCreate(Activity.java:5231)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-23 10:45:34.150: E/AndroidRuntime(1254): ... 11 more
06-23 10:45:34.150: E/AndroidRuntime(1254): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
06-23 10:45:34.150: E/AndroidRuntime(1254): at java.lang.Class.getConstructorOrMethod(Class.java:472)
06-23 10:45:34.150: E/AndroidRuntime(1254): at java.lang.Class.getConstructor(Class.java:446)
06-23 10:45:34.150: E/AndroidRuntime(1254): at android.view.LayoutInflater.createView(LayoutInflater.java:568)
06-23 10:45:34.150: E/AndroidRuntime(1254): ... 22 more


Edit 2: Third error log:

06-23 10:54:46.820: E/AndroidRuntime(1303): FATAL EXCEPTION: main
06-23 10:54:46.820: E/AndroidRuntime(1303): Process: com.example.watchvideo, PID: 1303
06-23 10:54:46.820: E/AndroidRuntime(1303): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.watchvideo/com.example.watchvideo.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.watchvideo.myVideoView
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.os.Handler.dispatchMessage(Handler.java:102)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.os.Looper.loop(Looper.java:136)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-23 10:54:46.820: E/AndroidRuntime(1303): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 10:54:46.820: E/AndroidRuntime(1303): at java.lang.reflect.Method.invoke(Method.java:515)
06-23 10:54:46.820: E/AndroidRuntime(1303): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-23 10:54:46.820: E/AndroidRuntime(1303): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-23 10:54:46.820: E/AndroidRuntime(1303): at dalvik.system.NativeStart.main(Native Method)
06-23 10:54:46.820: E/AndroidRuntime(1303): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.watchvideo.myVideoView
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.view.LayoutInflater.createView(LayoutInflater.java:603)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-23 10:54:46.820: E/AndroidRuntime(1303): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.app.Activity.setContentView(Activity.java:1929)
06-23 10:54:46.820: E/AndroidRuntime(1303): at com.example.watchvideo.MainActivity.onCreate(MainActivity.java:50)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.app.Activity.performCreate(Activity.java:5231)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-23 10:54:46.820: E/AndroidRuntime(1303): ... 11 more
06-23 10:54:46.820: E/AndroidRuntime(1303): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
06-23 10:54:46.820: E/AndroidRuntime(1303): at java.lang.Class.getConstructorOrMethod(Class.java:472)
06-23 10:54:46.820: E/AndroidRuntime(1303): at java.lang.Class.getConstructor(Class.java:446)
06-23 10:54:46.820: E/AndroidRuntime(1303): at android.view.LayoutInflater.createView(LayoutInflater.java:568)
06-23 10:54:46.820: E/AndroidRuntime(1303): ... 22 more


New Video Class:

public class myVideoView extends VideoView{

long lastPausedTime = 0; // The time of the last pause (milliseconds)
long totalPausedTime = 0; // The total time paused (milliseconds)

public myVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs);
}

@Override
public void pause() {
lastPausedTime = System.currentTimeMillis();

super.pause();
}

@Override
public void start() {
if (lastPausedTime != 0) {
totalPausedTime += System.currentTimeMillis() - lastPausedTime;
}
super.start();
}

public long getTotalTimeMillis() {
return totalPausedTime;
}

}


New main class (first part since rest is unchanged):

public class MainActivity extends Activity {

myVideoView v;

private static final String TAG = "MEDIA";
private TextView tv;


MediaPlayer.OnCompletionListener videoListener = new MediaPlayer.OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
Log.d("onCompletion", "Total time paused: " + v.getTotalTimeMillis());
}
};


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v = new myVideoView(this, null, 0);
setContentView(R.layout.fragment_main);

String fileName = "android.resource://" + getPackageName() + "/" + R.raw.leftwrist;
v = (myVideoView) findViewById(R.id.myVideoView);
v.setVideoURI(Uri.parse(fileName));
v.start();

Answer

In your XML you create the ViewView as <VideoView> so the class will be of VideoView and not myVideoView so it will fail to cast. (If you need, read this)

Caused by: java.lang.ClassCastException: android.widget.VideoView cannot be cast to com.example.watchvideo.myVideoView

In your XML you should create the View using your class, so it will be:

<com.example.watchvideo.myVideoView>

And not

<VideoView>

Implement all constructors

public myVideoView(Context context) {
  super(context);
}

public myVideoView(Context context, AttributeSet attrs) {
  super(context, attrs);
}

public myVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
}