Android Studio 视频播放器按钮和列表索引
Android Studio Video Player buttons and list index
我正在学习 Android Studio 和 java,我的水平完全是初学者,但我正在尝试使用简单的按钮制作视频播放器:Previous
、Next
、 exit
和列表中随机视频的选项复选框。
目前为止一切正常,但只有一次,下一次点击只是从头开始播放同一视频。有没有人可以怜悯和帮助我?我找了 3 天的代码。
<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);
}
}
);
}
}
我发现我的应用程序中的按钮、随机和下一个索引存在问题,我是这样解决的:
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();
}
}});
}
}
我正在学习 Android Studio 和 java,我的水平完全是初学者,但我正在尝试使用简单的按钮制作视频播放器:Previous
、Next
、 exit
和列表中随机视频的选项复选框。
目前为止一切正常,但只有一次,下一次点击只是从头开始播放同一视频。有没有人可以怜悯和帮助我?我找了 3 天的代码。
<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);
}
}
);
}
}
我发现我的应用程序中的按钮、随机和下一个索引存在问题,我是这样解决的:
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();
}
}});
}
}