Media Player dụ xử lý các phát lại.
Sau khi đặt video Xem trong giao diện người dùng, có được một tham chiếu đến nó trong mã của bạn. Sau đó bạn có thể gán một video để chơi bằng cách gọi setvideoPath của nó hoặc các phương pháp setvideoURi để chỉ định đường dẫn đến một tập tin địa phương, hoặc các URI của cả một nhà cung cấp nội dung hoặc dòng video từ xa:
thức VideoView videoView = (VideoView) findViewByld (R.id. videoView);
// Gán một tập tin địa phương để chơi
videoView.setVideoPath ( "/ sdcard / mycatvideo.3gp");
// Gán một URL của một video stream từ xa videoView.setVideoUri (myAwesomeStreamingSource);
Khi video được khởi tạo, bạn có thể kiểm soát phát lại của mình bằng cách sử dụng bắt đầu, phương pháp stopPlayback, tạm dừng, và seekTo. Video Xem cũng bao gồm các phương pháp setKeepScreenOn để áp dụng một màn hình Khóa Wake sẽ ngăn màn hình bị mờ đi trong khi phát lại là trong tiến trình mà không đòi hỏi một sự cho phép đặc biệt.
Liệt kê 15-2 cho thấy mã bộ xương dùng để gán một video vào một video Xem . Nó sử dụng một bộ điều khiển phương tiện để kiểm soát phát lại, như được mô tả trong phần "Kiểm soát Media Player Phát lại."
YẾT 15-2: Xem lại video bằng cách sử dụng video View Available cho // Lấy một tham chiếu đến các video View.
D5j "VideoView thức jn videoView = (VideoView) findViewByld (R.id.videoView);
// Cấu hình xem video và chỉ định một video nguồn. videoView.setKeepScreenOn (true);
videoView.setVideoPath ( "/ sdcard / mycatvideo.3gp");
// Gắn một Media khiển
MediaController mediaController = new MediaController (này); videoView.setMediaController (mediaController);
đoạn mã PA4AD_Ch15_Media_Player / src / VideoViewActivity.java
Tạo một bề mặt cho Video Playback
. Bước đầu tiên để sử dụng Media Player trực tiếp để xem nội dung video là chuẩn bị một bề mặt mà trên đó các video sẽ được hiển thị
này là thường xử lý bằng cách sử dụng đối tượng SurfaceView. Các bề mặt lớp View là một wrapper xung quanh đối tượng Chủ bề mặt, trong đó, lần lượt, là một wrapper xung quanh bề mặt được sử dụng để hỗ trợ cập nhật hình ảnh từ chủ đề nền.
Các Media Player sử dụng một đối tượng SurfaceHolder để hiển thị nội dung video, xác định theo phương setDisplay phương pháp. Để bao gồm một Chủ Surface trong cách bố trí giao diện người dùng của bạn, sử dụng lớp SurfaceView, như thể hiện trong XML bố cục mẫu trong Ví dụ 15-3.
đang được dịch, vui lòng đợi..