如何设置自定义videoView类

时间:2014-06-23 14:37:39

标签: android android-videoview

我正在使用自定义视频,我甚至无法启动它。这是非常基本的,我可能只是缺少一些小东西。提前致谢

这是我的主要课程:

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;
    }
}}

这是我自定义的videoView类:

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;
}

}

这是我的错误日志:

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

编辑:新错误日志

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

编辑2:第三错误日志:

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

新视频类:

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;
}

}

新的主要类(休息后的第一部分不变):

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

2 个答案:

答案 0 :(得分:2)

在您的XML中,您将ViewView创建为<VideoView>,因此该类将为VideoView而不是myVideoView,因此无法投射。 (如果您需要,read this

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

在您的XML中,您应该使用您的类创建View,因此它将是:

<com.example.watchvideo.myVideoView>

而不是

<VideoView>

实施所有构造函数

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

答案 1 :(得分:0)

以XML格式创建VideoView小部件。您应该在MainActivity.java文件中包含以下代码。确保更改文件名。

videoView = (VideoView) findViewById(R.id.videoView);
v = new myVideoView(this, videoView);
v.setVideoPath();
v.startVideo();

简单的myVideoView类的内容应如下所示。

public class myVideoView {
    private VideoView videoContainer;
    private String fileName;
    public myVideoView(Context context, VideoView v) {
        videoContainer = (VideoView) v;

        fileName = "android.resource://" + context.getPackageName() + "/" + R.raw.willferrell;
    }
    public void setVideoPath(String path) {
        videoContainer.setVideoPath(path);
    }
    public void startVideo() {
        videoContainer.start();
    }
}