我发现了类似的问题,但对我来说没什么用。 我尝试从这个网址播放视频:
http://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4
我的java代码:
VideoView videoView= (VideoView)findViewById(R.id.exerciseVideo);
Uri uri = Uri.parse(TEST_URL);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
当我运行app时,活动中不会显示任何内容,IDE也不会显示任何错误。有什么想法吗?
编辑:
我想要播放视频的活动:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.martin.fitnessapp.ExerciseDetailActivity"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/exerciseImgA"
android:layout_weight="1"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:paddingRight="8dp"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/exerciseImgB"
android:layout_weight="1"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:paddingLeft="8dp"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="@+id/exerciseDesc" />
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/exerciseVideo" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/guideImg"
android:scaleType="fitCenter"
android:adjustViewBounds="true"/>
</LinearLayout>
</ScrollView>
答案 0 :(得分:13)
试试这段代码..这段代码非常适合我..
final VideoView videoView;
videoView = (VideoView)findViewById(R.id.videoView);
videoView.setVideoPath("http://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4");
videoView.start();
答案 1 :(得分:1)
据我所知,你不应该wrap_content
使用VideoView
身高。
视频缓存后,VideoView
没有自行调整大小
答案 2 :(得分:1)
对我来说,更改 URL 来自
"http://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4"
到:
"https://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4"
让它发挥作用。
换句话说,我使用 HTTPS 而不是 HTTP。
我使用以下代码启动视频:
final VideoView videoView = findViewById(R.id.videoview); //id in your xml file
videoView.setVideoURI(Uri.parse(URL)); //the string of the URL mentioned above
videoView.requestFocus();
videoView.start();
答案 3 :(得分:0)
请为其添加Internet权限。将layout_height wrap_content更改为匹配的父项。 这是此问题的代码
public class MainActivity extends Activity {
private ProgressDialog bar;
private String path="https://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4";
private MediaController ctlr;
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.activity_main);
bar=new ProgressDialog(MainActivity.this);
bar.setTitle("Connecting server");
bar.setMessage("Please Wait... ");
bar.setCancelable(false);
bar.show();
if(bar.isShowing()) {
videoView = findViewById(R.id.v1);
Uri uri = Uri.parse(path);
videoView.setVideoURI(uri);
videoView.start();
ctlr = new MediaController(this);
ctlr.setMediaPlayer(videoView);
videoView.setMediaController(ctlr);
videoView.requestFocus();
}
bar.dismiss();
}
}
答案 4 :(得分:0)
videoView.setURI(Uri.parse(url));// use methods to set url
videoView.start();
然后接受许可
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
答案 5 :(得分:0)
public class ShowVideoActivity extends AppCompatActivity {
private static final String VIDEO_SAMPLE =
"https://www.youtube.com/watch?v=HexFqifusOk&list=RDHexFqifusOk&start_radio=1";
private VideoView mVideoView;
private TextView mBufferingTextView;
// Current playback position (in milliseconds).
private int mCurrentPosition = 0;
// Tag for the instance state bundle.
private static final String PLAYBACK_TIME = "play_time";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_video);
mVideoView = findViewById(R.id.videoview);
mBufferingTextView = findViewById(R.id.buffering_textview);
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(PLAYBACK_TIME);
}
// Set up the media controller widget and attach it to the video view.
MediaController controller = new MediaController(this);
controller.setMediaPlayer(mVideoView);
mVideoView.setMediaController(controller);
}
@Override
protected void onStart() {
super.onStart();
// Load the media each time onStart() is called.
initializePlayer();
}
@Override
protected void onPause() {
super.onPause();
// In Android versions less than N (7.0, API 24), onPause() is the
// end of the visual lifecycle of the app. Pausing the video here
// prevents the sound from continuing to play even after the app
// disappears.
//
// This is not a problem for more recent versions of Android because
// onStop() is now the end of the visual lifecycle, and that is where
// most of the app teardown should take place.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
mVideoView.pause();
}
}
@Override
protected void onStop() {
super.onStop();
// Media playback takes a lot of resources, so everything should be
// stopped and released at this time.
releasePlayer();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save the current playback position (in milliseconds) to the
// instance state bundle.
outState.putInt(PLAYBACK_TIME, mVideoView.getCurrentPosition());
}
private void initializePlayer() {
// Show the "Buffering..." message while the video loads.
mBufferingTextView.setVisibility(VideoView.VISIBLE);
// Buffer and decode the video sample.
Uri videoUri = getMedia(VIDEO_SAMPLE);
mVideoView.setVideoURI(videoUri);
// Listener for onPrepared() event (runs after the media is prepared).
mVideoView.setOnPreparedListener(
new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// Hide buffering message.
mBufferingTextView.setVisibility(VideoView.INVISIBLE);
// Restore saved position, if available.
if (mCurrentPosition > 0) {
mVideoView.seekTo(mCurrentPosition);
} else {
// Skipping to 1 shows the first frame of the video.
mVideoView.seekTo(1);
}
// Start playing!
mVideoView.start();
}
});
// Listener for onCompletion() event (runs after media has finished
// playing).
mVideoView.setOnCompletionListener(
new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Toast.makeText(ShowVideoActivity.this,
"Completed",
Toast.LENGTH_SHORT).show();
// Return the video position to the start.
mVideoView.seekTo(0);
}
});
}
// Release all media-related resources. In a more complicated app this
// might involve unregistering listeners or releasing audio focus.
private void releasePlayer() {
mVideoView.stopPlayback();
}
// Get a Uri for the media sample regardless of whether that sample is
// embedded in the app resources or available on the internet.
private Uri getMedia(String mediaName) {
if (URLUtil.isValidUrl(mediaName)) {
// Media name is an external URL.
return Uri.parse(mediaName);
} else {
// you can also put a video file in raw package and get file from there as shown below
return Uri.parse("android.resource://" + getPackageName() +
"/raw/" + mediaName);
}
}
}