视频初始化在后台花费的时间太长

时间:2017-04-27 15:57:26

标签: android video surfaceview

启动视频需要很长时间,在控制台中我会一次又一次地收到此错误,直到视频最终启动:

I/NuPlayerRenderer: possible video time jump of 0ms or uninitialized media clock, retrying in 100ms 

这是我的Activity类:

public class EntryView extends AppCompatActivity implements SurfaceHolder.Callback {


    SurfaceView imageLogo;
    MediaPlayer mp;

    @Override
    protected void attachBaseContext(Context newBase) {
        ContextWrapper contextWrapper = CalligraphyContextWrapper.wrap(newBase);
        super.attachBaseContext(contextWrapper);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        overridePendingTransition(0, 0);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entry);
        imageLogo = (SurfaceView) findViewById(R.id.image_logo);
        imageLogo.getHolder().addCallback(this);
        mp = new MediaPlayer();
        ButterKnife.bind(this);
    }

    // UI CALLBACKS ------------------------------------------------------------------------------

    @OnClick({R.id.button_login, R.id.button_register_normal})
    void openScreen(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.button_login:
                intent = new Intent(EntryView.this, LoginView.class);
                break;
            case R.id.button_register_normal:
                intent = new Intent(EntryView.this, RegisterView.class);
                break;
        }
        startActivity(intent);
        overridePendingTransition(R.anim.slide_in_from_right, R.anim.scale_out);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
                + R.raw.sample);

        try {
            mp.setDataSource(this, video);
            mp.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }


        //Get the dimensions of the video
        int videoWidth = mp.getVideoWidth();
        int videoHeight = mp.getVideoHeight();

        //Get the width of the screen
        int screenWidth = getWindowManager().getDefaultDisplay().getWidth();

        //Get the SurfaceView layout parameters
        android.view.ViewGroup.LayoutParams lp = imageLogo.getLayoutParams();

        //Set the width of the SurfaceView to the width of the screen
        lp.width = screenWidth;

        //Set the height of the SurfaceView to match the aspect ratio of the video
        //be sure to cast these as floats otherwise the calculation will likely be 0
        lp.height = (int) (((float)videoHeight / (float)videoWidth) * (float)screenWidth);

        //Commit the layout parameters
        imageLogo.setLayoutParams(lp);

        //Start video
        mp.setDisplay(holder);
        mp.setLooping(true);
        mp.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

这是xml文件:

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.omnidoctor.omniapp.screens.entry.EntryView">


    <SurfaceView
        android:id="@+id/image_logo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="10dip"/>

    <View
        android:id="@+id/spacer"
        android:layout_marginTop="85dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_heightPercent="20%" />

    <Button
        android:id="@+id/button_register_normal"
        fontPath="fonts/Arciform.ttf"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/spacer"
        android:background="@drawable/background_button_white"
        android:minHeight="20dp"
        android:paddingBottom="20dp"
        android:paddingTop="20dp"
        android:text="@string/register_normal_text"
        android:textAllCaps="false"
        android:textColor="@color/state_color_blue"
        android:textSize="16sp"
        app:layout_marginLeftPercent="10%"
        app:layout_marginRightPercent="10%"
        tools:ignore="MissingPrefix" />

    <Button
        android:id="@+id/button_login"
        fontPath="fonts/Arciform.ttf"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@id/button_register_normal"
        android:layout_marginTop="14dp"
        android:minHeight="20dp"
        android:minWidth="0dp"
        android:textColor="@color/state_color_blue"
        android:background="@drawable/background_button_white"
        android:paddingBottom="20dp"
        android:paddingTop="20dp"
        android:text="@string/log_in"
        android:textAllCaps="false"
        android:textSize="15sp"
        app:layout_marginLeftPercent="10%"
        app:layout_marginRightPercent="10%"
        tools:ignore="MissingPrefix" />

</android.support.percent.PercentRelativeLayout>

正如您所看到的,我正在使用SurfaceView,但我也尝试使用VideoView,我也遇到了同样的问题。视频只有1.5MB,需要30秒,因此不是很大。

编辑

我忘了告诉那是一部mp4视频。我不确定这是否有事情要做以防万一。

0 个答案:

没有答案