我有一个图片按钮和视频网址。我希望用户onClick图像按钮时播放该视频。如果该视频流结束,我希望该视频在同一活动上播放,那么我必须返回相同的活动。
VideoView videoView; ImageView btnWatchVideo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_promotions_feed);
btnWatchVideo = (ImageView) findViewById(R.id.btnWatchVideo);
videoView = (VideoView) findViewById(R.id.ltVideoUrl);
String path1 = "https://d1e6yi6s3cx2ur.cloudfront.net/videos/0/_20160316_ios-user.m4v";
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri uri = Uri.parse(path1);
videoView.setMediaController(mc);
videoView.setVideoURI(uri);
btnWatchVideo.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
videoView.start();
}
});
此代码无效。任何人都可以建议我如何做到这一点。
答案 0 :(得分:1)
尝试按照developer.android.com上的MediaPlayer教程进行操作:
https://developer.android.com/guide/topics/media/mediaplayer.html
答案 1 :(得分:0)
您需要像这样更改代码
package com.mobileappdev.spotstvhdlivestreamcric.activites;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import com.mobileappdev.spotstvhdlivestreamcric.R;
import com.mobileappdev.spotstvhdlivestreamcric.mediaplayer.ResizeSurfaceView;
import com.mobileappdev.spotstvhdlivestreamcric.mediaplayer.VideoControllerView;
import java.io.IOException;
public class MediaPlayerActivity extends AppCompatActivity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, VideoControllerView.MediaPlayerControlListener, MediaPlayer.OnVideoSizeChangedListener, MediaPlayer.OnCompletionListener {
private final static String TAG = MediaPlayerActivity.class.getSimpleName();
private ResizeSurfaceView mVideoSurface;
private MediaPlayer mMediaPlayer;
private VideoControllerView controller;
private int mVideoWidth;
private int mVideoHeight;
private View mContentView;
private View mLoadingView;
private boolean mIsComplete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_player);
getSupportActionBar().hide();
mVideoSurface = (ResizeSurfaceView) findViewById(R.id.videoSurface);
mContentView = findViewById(R.id.video_container);
mLoadingView = findViewById(R.id.loading);
SurfaceHolder videoHolder = mVideoSurface.getHolder();
videoHolder.addCallback(this);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnVideoSizeChangedListener(this);
//(FrameLayout) findViewById(R.id.videoSurfaceContainer)
controller = new VideoControllerView.Builder(this, this)
.withVideoTitle("ADD VIDEO TITLE")
.withVideoSurfaceView(mVideoSurface)//to enable toggle display controller view
.canControlBrightness(true)
.canControlVolume(true)
.canSeekVideo(true)
.exitIcon(R.drawable.video_top_back)
.pauseIcon(R.drawable.ic_media_pause)
.playIcon(R.drawable.ic_media_play)
.shrinkIcon(R.drawable.ic_media_fullscreen_shrink)
.stretchIcon(R.drawable.ic_media_fullscreen_stretch)
.build((FrameLayout) findViewById(R.id.videoSurfaceContainer));//layout container that hold video play view
mLoadingView.setVisibility(View.VISIBLE);
try {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(this, Uri.parse("https://d1e6yi6s3cx2ur.cloudfront.net/videos/0/_20160316_ios-user.m4v"));
// mMediaPlayer.setDataSource(this, Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"));
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnCompletionListener(this);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mVideoSurface.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
controller.toggleControllerView();
return false;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// controller.show();
return false;
}
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
mVideoHeight = mp.getVideoHeight();
mVideoWidth = mp.getVideoWidth();
if (mVideoHeight > 0 && mVideoWidth > 0)
mVideoSurface.adjustSize(mContentView.getWidth(), mContentView.getHeight(), mMediaPlayer.getVideoWidth(), mMediaPlayer.getVideoHeight());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (mVideoWidth > 0 && mVideoHeight > 0)
mVideoSurface.adjustSize(getDeviceWidth(this), getDeviceHeight(this), mVideoSurface.getWidth(), mVideoSurface.getHeight());
}
private void resetPlayer() {
if (mMediaPlayer != null) {
mMediaPlayer.reset();
mMediaPlayer.release();
mMediaPlayer = null;
}
}
public static int getDeviceWidth(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics mDisplayMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(mDisplayMetrics);
return mDisplayMetrics.widthPixels;
}
public static int getDeviceHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics mDisplayMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(mDisplayMetrics);
return mDisplayMetrics.heightPixels;
}
// Implement SurfaceHolder.Callback
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepareAsync();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
resetPlayer();
}
// End SurfaceHolder.Callback
// Implement MediaPlayer.OnPreparedListener
@Override
public void onPrepared(MediaPlayer mp) {
//setup video controller view
mLoadingView.setVisibility(View.GONE);
mVideoSurface.setVisibility(View.VISIBLE);
mMediaPlayer.start();
mIsComplete = false;
}
// End MediaPlayer.OnPreparedListener
/**
* Implement VideoMediaController.MediaPlayerControl
*/
@Override
public int getBufferPercentage() {
return 0;
}
@Override
public int getCurrentPosition() {
if (null != mMediaPlayer)
return mMediaPlayer.getCurrentPosition();
else
return 0;
}
@Override
public int getDuration() {
if (null != mMediaPlayer)
return mMediaPlayer.getDuration();
else
return 0;
}
@Override
public boolean isPlaying() {
if (null != mMediaPlayer)
return mMediaPlayer.isPlaying();
else
return false;
}
@Override
public boolean isComplete() {
return mIsComplete;
}
@Override
public void pause() {
if (null != mMediaPlayer) {
mMediaPlayer.pause();
}
}
@Override
public void seekTo(int i) {
if (null != mMediaPlayer) {
mMediaPlayer.seekTo(i);
}
}
@Override
public void start() {
if (null != mMediaPlayer) {
mMediaPlayer.start();
mIsComplete = false;
}
}
@Override
public boolean isFullScreen() {
return getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ? true : false;
}
@Override
public void toggleFullScreen() {
if (isFullScreen()) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
@Override
public void exit() {
resetPlayer();
finish();
}
@Override
public void onCompletion(MediaPlayer mp) {
mIsComplete = true;
}
}
您的视频正在播放。
答案 2 :(得分:0)
尝试对这两个活动使用这两个onCreate方法。
视频浏览活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
videoView = (VideoView) findViewById(R.id.ltVideoUrl);
String path1 = "https://d1e6yi6s3cx2ur.cloudfront.net/videos/0/_20160316_ios-user.m4v";
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri uri = Uri.parse(path1);
videoView.setMediaController(mc);
videoView.setVideoURI(uri);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
videoView.start();
}
});}
主要活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnWatchVideo = (ImageView) findViewById(R.id.btnWatchVideo);
btnWatchVideo.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, VideoActivity.class));
}
});}