2012-06-16 21 views
5

desea cambiar el tamaño del vídeo en la vista superficialandroide cómo cambiar la anchura y la altura de vídeo en vista de la superficie de reproductor de medios

estoy reproducción de un vídeo en el Media Player con el uso de una vista de la superficie y el soporte para vídeo ..

// pero al implementar MediaPlayerControl en algunos dispositivos, digamos que la tableta Micromax, iball, Akash proporciona el error del método abstracto no disponible, tal como se rastrea mediante el seguimiento de errores de ACRA.

Ahora quiero tener solución de los problemas mencionados:

  • Como llegar controles del reproductor de medios activos en todos los dispositivos wihtout estrellarse debido al método abstracto como para controlador de medios en algunos dispositivos.

  • ¿Cómo puedo cambiar el tamaño de la vista de la superficie del video?

El código está trabajando F9 en Samsung Galaxy GT OS 2.2 p1000 y algún dispositivo más barato WorldTeck etc.

Pero en ZiiO 7 creativa versión del sistema operativo es 2.2.1 (480 * 800)densidad de resolución es 160 dpi parece que el video se amplía y se recorta de los bordes.

El código se muestra a continuación:

/* Media Player Control is commented due to force close in some devices say *micromax,Creative ziio, akash etc */ 

    public class VideoViewerActivity extends Activity implements 
      OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, 
      OnVideoSizeChangedListener, SurfaceHolder.Callback { 
     // implements ERROR in some devices , MediaPlayerControl 
     private static final String TAG = "MediaPlayerDemo"; 
     private int mVideoWidth; 
     private int mVideoHeight; 
     private MediaPlayer mMediaPlayer; 
     private SurfaceView mPreview; 
     private SurfaceHolder holder; 
     private String path = ""; 
     private Bundle extras; 
     private static final String MEDIA = "media"; 
     private static final int LOCAL_AUDIO = 1; 
     private static final int STREAM_AUDIO = 2; 
     private static final int RESOURCES_AUDIO = 3; 
     private static final int LOCAL_VIDEO = 4; 
     private static final int STREAM_VIDEO = 5; 
     private boolean mIsVideoSizeKnown = false; 
     private boolean mIsVideoReadyToBePlayed = false; 
     /* ERROR In Abstract method implementation in the other devices */ 
     // private MediaController mediaController; 
     private Context context; 
     private final Handler handler = new Handler(); 

     /** 
     * 
     * Called when the activity is first created. 
     */ 
     @Override 
     public void onCreate(Bundle icicle) { 
      super.onCreate(icicle); 
      setContentView(R.layout.video_layout); 
      context = this; 
      path = getIntent().getExtras().getString("media"); 

      Log.v("arpit", "path " + path); 
      mPreview = (SurfaceView) findViewById(R.id.surfaceview1); 
      holder = mPreview.getHolder(); 
      holder.addCallback(this); 
      holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
      extras = getIntent().getExtras(); 

     } 

     private void playVideo() { 
      doCleanUp(); 
      try { 

       // Create a new media player and set the listeners 
       File f = new File(path); 
       // FileInputStream in = new FileInputStream(f); 

       RandomAccessFile randomAccessFile = new RandomAccessFile(f, "r"); 
       // randomAccessFile.skipBytes(1024); 
       FileDescriptor fileDescriptor = randomAccessFile.getFD(); 

       mMediaPlayer = new MediaPlayer(); 
       mMediaPlayer.setDataSource(fileDescriptor, Constant.key1.length 
         + Data.deviceID.length(), f.length()); 

       mMediaPlayer.setDisplay(holder); 
       mMediaPlayer.prepare(); 
       mMediaPlayer.setOnBufferingUpdateListener(this); 
       mMediaPlayer.setOnCompletionListener(this); 
       mMediaPlayer.setOnPreparedListener(this); 
       mMediaPlayer.setOnVideoSizeChangedListener(this); 
       mMediaPlayer.setScreenOnWhilePlaying(true); 

       // mediaController = new MediaController(this); 
       mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 

      } catch (Exception e) { 
       Log.e(TAG, "error: " + e.getMessage(), e); 
       Toast.makeText(context, "supported video was not found", 
         Toast.LENGTH_SHORT).show(); 
       finish(); 
      } 
     } 

     public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { 
      Log.v(TAG, "onVideoSizeChanged called"); 
      if (width == 0 || height == 0) { 
       Log.e(TAG, "invalid video width(" + width + ") or height(" + height 
         + ")"); 
       return; 
      } 
      mIsVideoSizeKnown = true; 
      mVideoWidth = width; 
      mVideoHeight = height; 
      if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) { 
       startVideoPlayback(); 
      } 
     } 


     public void surfaceDestroyed(SurfaceHolder surfaceholder) { 
      Log.d(TAG, "surfaceDestroyed called"); 
     } 

     public void surfaceCreated(SurfaceHolder holder) { 
      Log.d(TAG, "surfaceCreated called"); 
      playVideo(); 

     } 

     private void doCleanUp() { 
      mVideoWidth = 0; 
      mVideoHeight = 0; 
      mIsVideoReadyToBePlayed = false; 
      mIsVideoSizeKnown = false; 
     } 

     private void startVideoPlayback() { 
      Log.v(TAG, "startVideoPlayback"); 
//also tried and used the below line but nothing good 


// holder.setFixedSize(480, 800); 

holder.setFixedSize(mVideoWidth, mVideoHeight); 

mMediaPlayer.start(); 

     } 


    } 
+0

intente [mi respuesta] (http://stackoverflow.com/questions/8293909/surfaceview-height-width-gets-ignored/20798009#20798009). Esto le ayudará. –

Respuesta

1

Hola arpit yo también estaba trabajando en los mismos dispositivos que usted ha mencionado para la reproducción de archivos de vídeo, I tiene el mismo problema, aplicar un control de reproductor de vídeo (Método abstracto no disponible).

Según mi R & D sobre este tema llego a la conclusión de que el sistema operativo Android personalizada y el hardware de estos dispositivos no son compatibles el uno al otro, ya que son de muy mala calidad, en comparación con los dispositivos de Samsung, tantos de métodos, aunque están en el sistema operativo Android, pero no son compatibles con dichos dispositivos.

Así que creo que no hay soluciones para tales problemas en estos dispositivos.

Por favor intente enfocarse usando dispositivos estándar mientras desarrolla cualquier proyecto. esto puede ayudarte mucho.

Gracias. :)

2

Solía ​​TextureView en lugar de vista de la superficie (que permite rotar de vídeo) y establecer la setScale {X | Y}() apropiadamente en:

@Override 
public void onVideoSizeChanged(MediaPlayer mp, int w, int h) { 
    if (w > 0 && h > 0) { 
     RelativeLayout.LayoutParams l; 
     DisplayMetrics metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     l = new RelativeLayout.LayoutParams(metrics.heightPixels, metrics.widthPixels); 
     l.addRule(RelativeLayout.CENTER_IN_PARENT, -1); 
     float scale = (metrics.heightPixels * 1.0f)/(metrics.widthPixels * 1.0f); 
     videoView.setScaleX(scale); 
     videoView.setLayoutParams(l); 
     Log.d(YourApp.TAG, "video size: " + mp.getVideoWidth() + "x" + mp.getVideoHeight() + " vertical scale: " + scale); 
    } 
} 

Tenga en cuenta que he girado mi video, este es por eso que establecí la escala para X.

Cuestiones relacionadas