**在Android应用程序中请建议我们如何设置视频的持续时间...我的视频长度目前超过2分钟。 * 1.videoview不提供setDuration ... 2.如何在videoview中设置持续时间 ***
public class MainActivity extends Activity {
VideoView video;
MediaPlayer mp;
Button play;
int playtime=10000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video=(VideoView)findViewById(R.id.videoView1);
video.setLongClickable(true);
play=(Button)findViewById(R.id.btn1);
Uri uri=Uri.parse("android.resource://" + getPackageName() +"/"+R.drawable.b);
video.setVideoURI(uri);
video.setMediaController(new MediaController(this));
//video.setDuration(playtime);
MediaController mp=new MediaController(this);
play.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v)
{
if(video.getDuration()<=playtime)
{
video.setVisibility(v.VISIBLE);
video.requestFocus();
video.start();
play.setEnabled(false);
}
else
{
video.stopPlayback();
}
return true;
}
});
video.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
play.setEnabled(true);
video.setVisibility(View.GONE);
play.setFocusableInTouchMode(true);
play.requestFocus();
}
});
}
2. Xml and here is the XML Snippet which is ok..
<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"
tools:context=".MainActivity" >
<VideoView
android:id="@+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:visibility="gone"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="play"/>
</RelativeLayout>
答案 0 :(得分:0)
你需要将MediaPlayerControl实现为MediaController,就像这样。
mp.setMediaPlayer(new MediaPlayerControl() {
@Override
public void start() {
}
@Override
public void seekTo(int pos) {
}
@Override
public void pause() {
}
@Override
public boolean isPlaying() {
return false;
}
@Override
public int getDuration() {
return 0; // Pass your duration here
}
@Override
public int getCurrentPosition() {
return 0;
}
@Override
public int getBufferPercentage() {
return 0;
}
@Override
public boolean canSeekForward() {
return false;
}
@Override
public boolean canSeekBackward() {
return false;
}
@Override
public boolean canPause() {
return false;
}
});
替换此行
video.setMediaController(new MediaController(this));
//video.setDuration(playtime);
MediaController mp=new MediaController(this);
使用
MediaController mp=new MediaController(this);
video.setMediaController(mp);