I am learning Android Studio and java, my level is totaly beginner but I am trying to make video player with simple buttons: Previous
, Next
, exit
and option checkbox for random video from list.
So far it's working but just once, next clicks are just starting same video from beginning. Can someone have mercy and help me? I was looking for some codes for 3 days.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<VideoView
android:layout_width="320dp"
android:layout_height="240dp"
android:id="@+id/videoView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Następny"
android:id="@+id/next"
android:layout_below="@+id/videoView"
android:layout_alignRight="@+id/videoView"
android:layout_alignEnd="@+id/videoView" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Poprzedni"
android:id="@+id/prev"
android:layout_alignTop="@+id/next"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wyjście"
android:id="@+id/exit"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Losowo"
android:id="@+id/checkBox"
android:layout_alignTop="@+id/prev"
android:layout_centerHorizontal="true"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start/Stop"
android:id="@+id/start"
android:layout_below="@+id/checkBox"
android:layout_centerHorizontal="true" />
</RelativeLayout>
package no.norskavis.norw1;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.VideoView;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.exit);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
final VideoView videoView = (VideoView) findViewById(R.id.videoView);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.intro);
videoView.setVideoURI(video);
videoView.start();
final List listA = new ArrayList<>();
listA.add("android.resource://" + getPackageName() + "/" + R.raw.intro);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc1);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc2);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc3);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc4);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc5);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc6);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc7);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc8);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc9);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc10);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc11);
CheckBox chck= (CheckBox) findViewById(R.id.checkBox);
if (chck.isEnabled()) {
Button next = (Button) findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
Object randomItem = listA.get(new Random().nextInt(listA.size()));
@Override
public void onClick(View v) {
Uri video = Uri.parse((String) randomItem);
videoView.setVideoURI(video);
}
}
);
}
else
{ Button next = (Button) findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v){
ListIterator<String> listIterator = listA.listIterator();
Uri video = Uri.parse(String.valueOf(listIterator.nextIndex()));
videoView.setVideoURI(video);
}
}
);
}
}
I have found problems with buttons, random and next index in my app and this is how I fixed:
package no.norskavis.norw1;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.VideoView;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.exit);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
final VideoView videoView = (VideoView) findViewById(R.id.videoView);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.intro);
videoView.setVideoURI(video);
videoView.start();
final List listA = new ArrayList<>();
listA.add("android.resource://" + getPackageName() + "/" + R.raw.intro);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc1);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc2);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc3);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc4);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc5);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc6);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc7);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc8);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc9);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc10);
listA.add("android.resource://" + getPackageName() + "/" + R.raw.czesc11);
Button next = (Button) findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
CheckBox chck = (CheckBox) findViewById(R.id.checkBox);
ListIterator listIterator = listA.listIterator();
public void onClick(View v) {
Object randomItem = listA.get(new Random().nextInt(listA.size()));
if (chck.isChecked()) {
Uri video = Uri.parse(String.valueOf(randomItem));
videoView.setVideoURI(video);
videoView.start();
} else {
Uri video = Uri.parse(String.valueOf(listIterator.next()));
videoView.setVideoURI(video);
videoView.start();
}
}
});
Button start = (Button) findViewById(R.id.start);
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (videoView.isPlaying())
{
videoView.pause();
}
else
{
videoView.start();
}
}});
}
}