在Android应用程序中播放视频时出错

时间:2016-07-08 18:05:41

标签: android video

我正在尝试在我的Android应用程序中播放MP4但由于某种原因它无效。

我已经看到了android上的所有格式和文件包含: https://developer.android.com/guide/appendix/media-formats.html

我可以使用在线或离线转换器来制作此视频吗?

我的主要:

public class MainActivity extends AppCompatActivity {

LinearLayout screen;
Handler handler;
int fadeTime;
Button BtnTextColor;
Random rnd = new Random();
Button BackgroundButton;
final Context context = this;
private Button button;
private EditText result;
Button ChangeName;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);




            VideoView mediaPlayer = (VideoView)findViewById(R.id.videoView1);
            String uriPath2 = "android.resource://com.example.toyo.playvideo/"+R.raw.video;
            Uri uri2 = Uri.parse(uriPath2);
            mediaPlayer.setVideoURI(uri2);
            mediaPlayer.requestFocus();
            mediaPlayer.start();

fadeTime = -20; //text fade timer

    android.support.v7.app.ActionBar theactionbar = getSupportActionBar(); //ActionBar
    screen = (LinearLayout) findViewById(R.id.screen); //Background Screen


    //hide action bar (top)
    theactionbar.hide();


    //fades from white to blue

    ObjectAnimator colorFade = ObjectAnimator.ofObject(screen, "backgroundColor", new ArgbEvaluator(), Color.argb(0, 0, 0, 0), 0xff7F87CF);
    colorFade.setDuration(3000);
    colorFade.start();

    //fade text

    Animation animFadeIn;
    animFadeIn = AnimationUtils.loadAnimation(this, R.anim.text_fade);
    animFadeIn.start();
    animFadeIn = new AlphaAnimation(0.0f, 1.0f);
    animFadeIn.setDuration(8000);
    animFadeIn.start();


    DelayedDialog();


    // Button Listeners
    BackgroundButton = (Button) findViewById(R.id.Change_Background);
    ChangeName = (Button) findViewById(R.id.ChangeWelcome);


    ChangeName.setOnClickListener(new View.OnClickListener() {
        //Enter Name
        @Override
        public void onClick(View v) {

        Dialog();

                }
            });


}

public void onRadioButtonClicked(View view) {

    // R. Button checked
    final boolean checked = ((RadioButton) view).isChecked();


    // Which R. Button was checked
    switch (view.getId()) {
        //Rapid R.Button selected
        case R.id.BackgroundRapidChange:
            if (checked) {

                BackgroundButton.setOnClickListener(new View.OnClickListener() {
                    //Rapid Background Switch


                    @Override
                    public void onClick(View v) {

                        RapidBackgroundColor();

                        BtnTextColor = (Button) findViewById(R.id.Change_Text);
                        final TextView tt = (TextView) findViewById(R.id.Welcome_Text);

                        //Rapid Text Switch
                        BtnTextColor.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                RapidTextColor();

                            }
                        });
                    }
                });

                if(checked == false) {
                Dialog();
                }
            }



            break;

        //Slow R. Button selected
        case R.id.BackgroundSlowChange:
            if (checked) {

                //Slow Background Switch
                BackgroundButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {

                        SlowBackgroundChangeColor();


                        BtnTextColor = (Button) findViewById(R.id.Change_Text);

                        //Slow Text Switch
                        BtnTextColor.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                SlowTextColor();
                            }
                        });
                    }
                });


            }

            break;
    }


}


public void SlowBackgroundChangeColor() {

    int Changecolor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    ObjectAnimator ChangecolorFade = ObjectAnimator.ofObject(screen, "backgroundColor", new ArgbEvaluator(),
            Color.argb(Changecolor, Changecolor, Changecolor, Changecolor), Changecolor);


    //slow change
    ChangecolorFade.setDuration(2000);
    ChangecolorFade.start();

}

public void SlowTextColor() {
    final TextView tt = (TextView) findViewById(R.id.Welcome_Text);
    int Changecolor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    int ChangecolorText = Color.argb(225, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));


    ValueAnimator colorAnim = ObjectAnimator.ofInt(tt, "textColor", ChangecolorText, Changecolor);
    colorAnim.setDuration(2000);
    colorAnim.setEvaluator(new ArgbEvaluator());

}

public void RapidTextColor() {
    final TextView tt = (TextView) findViewById(R.id.Welcome_Text);
    Random rnd = new Random();
    int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    tt.setTextColor(color);
}

public void RapidBackgroundColor() {
    int Changecolor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    ObjectAnimator ChangecolorFade = ObjectAnimator.ofObject(screen, "backgroundColor", new ArgbEvaluator(),
            Color.argb(Changecolor, Changecolor, Changecolor, Changecolor), Changecolor);

    //fast change
    screen.setBackgroundColor(Changecolor);

}
public void Dialog(){
button = (Button) findViewById(R.id.buttonPrompt);
result = (EditText) findViewById(R.id.editTextResult);

LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.dialog, null);

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        context);

final AlertDialog.Builder noText = new AlertDialog.Builder(
        context);

alertDialogBuilder.setView(promptsView);

final EditText userInput = (EditText) promptsView
        .findViewById(R.id.editTextDialogUserInput);

alertDialogBuilder
        .setCancelable(false)
        .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {

                            if(userInput.getText().length() ==0){
                                Dialog();
                            }

                        TextView ChangeWelcomeText = (TextView) findViewById(R.id.Welcome_Text);
                                ChangeWelcomeText.setText("Welcome \n"  + userInput.getText());
                    }
                });

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}

public void DelayedDialog() {
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Dialog();

        }
    }, 4000);
}
}

这是我的布局xml:          

<RadioGroup android:id="@+id/backgroundoption"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RadioButton android:id="@+id/BackgroundRapidChange"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked"
        android:text="Rapid" />

    <RadioButton android:id="@+id/BackgroundSlowChange"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked"
        android:text="Slow" />

</RadioGroup>

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RadioButton android:id="@+id/Auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked"
        android:text="Auto" />

    <RadioButton android:id="@+id/Manual"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked"
        android:text="Manual" />
</RadioGroup>




<Button android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Change Background"
    android:id="@+id/Change_Background"
    android:layout_centerHorizontal="true"
    android:gravity="center|bottom"
    android:layout_gravity="center_horizontal|center"
    >
</Button>

<Button android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Change Text"
    android:id="@+id/Change_Text"
    android:layout_centerHorizontal="true"
    android:gravity="center|bottom"
    android:layout_gravity="center_horizontal|center"
    >
</Button>

<Button
    android:id="@+id/ChangeWelcome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change Name"
    >

</Button>


<TextView
    android:id="@+id/Welcome_Text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="80dp"
    android:text="Welcome"
    android:textColor="@color/white"
    android:gravity="center_horizontal"
    >


</TextView>
<Button
    android:id="@+id/buttonPrompt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Please Enter your Name" />

<EditText
    android:id="@+id/editTextResult"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

</EditText>


<VideoView
    android:id="@+id/videoView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

它有点长而且非常混乱,这是我的第一个Android应用程序

1 个答案:

答案 0 :(得分:0)

好吧,从我设法注意到以下一行导致了这个问题;

 String uriPath2 = "android.resource://com.example.toyo.playvideo/"+R.raw.video;

R文件为android资源提供了一个人类可读的值,这主要是一种由android系统给这些资源的Ids /数字的翻译

该行如下所示:

  

android.resource://com.example.toyo.playvideo/2131427330

将不是视频文件名的数字连接到网址,此外,您可以通过R.raw.fileName从内部资源读取文件,或使用其在线网址

另一个注意事项;

<VideoView
       android:id="@+id/videoView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerVertical="true"
       android:layout_centerHorizontal="true" />

使用以下代替上一个;

<VideoView
    android:id="@+id/videoView1"
    android:layout_width="match_parent"
    android:minHeight="100dp"
    android:minWidth="50dp"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

视频容器的宽度/高度或最小宽度/高度是固定的,因为视频需要加载,如果尺寸很大并且加载时间太长,您可能会感到困惑/ p> 祝你好运,希望我的回答很有帮助