Android Video View en otro hilo y problema con android 2.1
Quiero stream url de formulario de vídeo en la vista de vídeo de Android. Utilizo el código de api de la muestra y he hecho poca modificación en eso para alcanzar mi necesidad. Mi código es
public class VideoViewDemo extends Activity { private static final String TAG = "VideoViewDemo"; private String current; /** * TODO: Set the path variable to a streaming video URL or a local media * file path. */ private String path = "http://www.boisestatefootball.com/sites/default/files/videos/original/01%20-%20coach%20pete%20bio_4.mp4"; private VideoView mVideoView; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.videoview); mVideoView = (VideoView) findViewById(R.id.surface_view); runOnUiThread(new Runnable() { public void run() { playVideo(); } }); } private void playVideo() { try { // final String path = path; Log.v(TAG, "path: " + path); if (path == null || path.length() == 0) { Toast.makeText(VideoViewDemo.this, "File URL/path is empty", Toast.LENGTH_LONG).show(); } else { // If the path has not changed, just start the media player if (path.equals(current) && mVideoView != null) { mVideoView.start(); mVideoView.requestFocus(); return; } current = path; mVideoView.setVideoPath(getDataSource(path)); mVideoView.start(); mVideoView.setMediaController(new MediaController(this)); mVideoView.requestFocus(); } } catch (Exception e) { Log.e(TAG, "error: " + e.getMessage(), e); if (mVideoView != null) { mVideoView.stopPlayback(); } } } private String getDataSource(String path) throws IOException { if (!URLUtil.isNetworkUrl(path)) { return path; } else { URL url = new URL(path); URLConnection cn = url.openConnection(); cn.connect(); InputStream stream = cn.getInputStream(); if (stream == null) throw new RuntimeException("stream is null"); File temp = File.createTempFile("mediaplayertmp", "dat"); temp.deleteOnExit(); String tempPath = temp.getAbsolutePath(); FileOutputStream out = new FileOutputStream(temp); byte buf[] = new byte[128]; do { int numread = stream.read(buf); if (numread <= 0) break; out.write(buf, 0, numread); } while (true); try { stream.close(); } catch (IOException ex) { Log.e(TAG, "error: " + ex.getMessage(), ex); } return tempPath; } } }
Aquí usted puede ver que estoy usando uithread para transmitir vídeo. ¿Hay alguna manera de manejar esto por mi en el hilo
- VideoView getDrawingCache devuelve negro
- Android 4.2 con 4 MediaPlayers = "No se puede reproducir este video"
- Problemas para conseguir que el video se reproduzca en Recyclerview
- MediaPlayer / VideoView - Stream de una URL y luego guardar el video después de la caché?
- Cómo obtener el porcentaje de búfer de VideoView en Android
Lo que intenté es
new Thered(new Runnable() { public void run() { playVideo(); } }).start();
Pero falla
Y también Mientras se ejecuta en Android 2.2 El código First se ejecuta y muestra error(-38,0)
en Android 2.1 ¿cuál es este error? He comprobado este archivo, pero no puede averiguar qué error es esto ??
¿Puede alguien guiarme?
- Edición de marcos VideoView de android
- VideoView dentro de DialogFragment
- Android: El servidor de DrmManager murió
- ¿Deshabilitar la solicitud de VideoView de AndroidAudioFocus al reproducir un video?
- ¿Cómo quitar la pantalla negra que apper antes de jugar el vídeo mp4?
- MediaPlayer error (1, -1004) ¿Por qué?
- Comportamiento extraño - Fragmento de VideoViewer no funciona con MediaController
- VideoView - MediaController no aparece
No necesitas buscar el video completo y guardarlo en el sistema de archivos, luego ejecutarlo. El video que has mencionado tiene un tamaño de 32Mb, y tardará tanto tiempo en buscarlo a través de la red. En su lugar puede dar el enlace directo a videoview, que progresivamente buscar / buffer el video y reproducirlo. Estabas tratando de obtener los datos de video en el hilo de la interfaz de usuario, que no es aceptable. Aquí está el código corregido, puede comprobarlo.
public class VideoViewDemo extends Activity { private static final String TAG = "VideoViewDemo"; private String current; /** * TODO: Set the path variable to a streaming video URL or a local media * file path. */ private String path = "http://www.boisestatefootball.com/sites/default/files/videos/original/01%20-%20coach%20pete%20bio_4.mp4"; private VideoView mVideoView; private Handler handler; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); handler = new Handler(); setContentView(R.layout.videoview); mVideoView = (VideoView) findViewById(R.id.surface_view); /* runOnUiThread(new Runnable() { public void run() { playVideo(); } }); */ playVideo(); Log.v(TAG, "activity oncreate finished"); } private void playVideo() { try { // final String path = path; Log.v(TAG, "path: " + path); if (path == null || path.length() == 0) { Toast.makeText(VideoViewDemo.this, "File URL/path is empty", Toast.LENGTH_LONG).show(); } else { // If the path has not changed, just start the media player if (path.equals(current) && mVideoView != null) { mVideoView.start(); mVideoView.requestFocus(); return; } current = path; mVideoView.setVideoPath(path); mVideoView.start(); mVideoView.setMediaController(new MediaController(VideoViewDemo.this)); mVideoView.requestFocus(); } } catch (Exception e) { Log.e(TAG, "error: " + e.getMessage(), e); if (mVideoView != null) { mVideoView.stopPlayback(); } } } }
- ObjectInputStream – ¿Cómo esperar a nuevos datos?
- Actividad transparente en Resume no aparece android