gpt4 book ai didi

Android VideoView OnCompletionListener 不工作

转载 作者:行者123 更新时间:2023-11-29 00:41:38 26 4
gpt4 key购买 nike

我正在尝试实现一个简单的 VideoView 来播放文件系统中的视频。视频加载和播放都很好。但是,我想知道视频何时结束播放,以便我可以完成() Activity 并继续下一个 Activity 。这是我的。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/videoPlayer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true"
android:orientation="vertical" >
<VideoView
android:id="@+id/videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>

Activity 如下。

    public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.videoplayer);

videoView = (VideoView) findViewById(R.id.videoView);
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
Intent result = new Intent();
setResult(1, result);
finish();
}
}); // video finish listener

Intent intent = getIntent();

// Receiving the Data
String fileName = intent.getStringExtra("fileName");

videoView.setVideoPath(fileName);
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.start();}

我从另一个 Activity 中调用此 Activity 。这是 Activity 定义。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:keepScreenOn="true">

<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="@string/imageViewDescription" />
</LinearLayout>

VideoPlayer Activity 类

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

setContentView(R.layout.imageplayer);

imageView = (ImageView) findViewById(R.id.imageView);

updateUI(new File("/sdcard/Test/Test.3gp");
}

public void updateUI(File file)
{
if (file.getName().toLowerCase().endsWith(".jpg") || file.getName().toLowerCase().endsWith(".png"))
{
Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

imageView.setImageBitmap(myBitmap);
}
else if (file.getName().toLowerCase().endsWith(".3gp") || file.getName().toLowerCase().endsWith(".mp4"))
{
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), VideoPlayer.class);

//Sending data to another Activity
nextScreen.putExtra("fileName", file.getAbsolutePath());

released = false;

startActivityForResult(nextScreen, resultCode);
}
}
}

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
}

永远不会调用 onCompletion 方法,也不会调用此 Activity 调用者中的 onActivityResult() 方法。我想知道这有什么问题。我尝试同时播放“.3gp”和“.mp4”文件,结果相同。请帮忙

最佳答案

在 onPreparedListener 中调用 videoView.start() 并使用 false 值设置 mediaplayer 对象循环。我希望这会奏效..喜欢如下:

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(false); //make sure to use this statement
video.start();
Toast.makeText(mContext,String.valueOf(vtime),Toast.LENGTH_LONG).show();
}
});

关于Android VideoView OnCompletionListener 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8978708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com