我试图让我的Android应用上的视频背靠背播放,并且只有在完整地观看每个视频后才能播放。
我已经提出以下代码:
package com.davekelley;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.media.MediaPlayer;
import android.net.Uri;
import android.widget.MediaController;
import android.widget.VideoView;
public class Watch extends Activity {
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_watch);
player = new MediaPlayer();
player.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.stop();
mp.release();
int i = 0;
if (i < 2) {
i++;
String path="http://domain.com/videos/"+i+".mp4";
playVideo(path);
}
else i=0;
}
});
player.start();
}
private void playVideo(String filename) {
try {
player.setDataSource(filename);
player.prepare();
player.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.watch, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
但是我甚至无法运行它,因为Eclipse告诉我&#34; MediaPlayer类型中的方法setOnCompletionListener(MediaPlayer.OnCompletionListener)不适用于参数(new OnCompletionListener(){})&# 34。
造成这种情况的原因是什么?我的其余代码怎么样......它会实现我想要做的事情吗?感谢。
答案 0 :(得分:0)
复制此行
String path="http://domain.com/videos/"+i+".mp4";
playVideo(path);
之后
player = new MediaPlayer();
答案 1 :(得分:0)
尝试播放第一个视频,并为媒体播放器设置OnCompletionListener,当第一个视频完成后,您可以回拨第二个视频,然后尝试使用新的播放器实例检查第二个视频,您从未设置第一个视频的数据源只是初始化播放器实例和设置OnCompletionListener,它从不调用becz未设置第一个视频数据源。
private MediaPlayer player;
private int i = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playVideo("http://domain.com/videos/"+i+".mp4\"");
}
public void playVideo(String path){
try{
player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(path);
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
player.release();
player = null;
if(i<2){
playVideo("http://domain.com/videos/"+(++i)+".mp4");
}
}
});
}catch (Exception e){
e.printStackTrace();
}
}