deathsonic deathsonic - 4 months ago 8
Android Question

Error playing Video in Android Application

I am trying to play a MP4 inside my android app but for some reason it doesn't work.

I have seen all the formats and file containters for android on :
https://developer.android.com/guide/appendix/media-formats.html

Is there an online or offline converter I can use to make this video work?

My Main :

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


And this is my Layout 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" />




It is a bit long and quite messy, it's my first Android App

Answer

Well, From what i managed to notice the following line caused the issue;

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

The R file gives a human readable values for android resources which is mainly a kind of a translation of Ids/numbers given by android system to these resources

The line was read like the following;

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

concatenating a number not the video file name to the url, besides you can either read the file from internal resources by R.raw.fileName, or use the online URL of it

another note;

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

use the following instead of the previous;

<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" />

give the video container fixed width/height or minimum width/height as videos take a bit to load and you might get confused if it's working or not if the size was large and took it too long to load

Good luck, wish my answer was helpful