2015-05-22 152 views
1

我開始知道我們可以在紋理視圖中播放視頻。但我只看到了如何播放通過相機捕捉的視頻,但我想播放其中可用的視頻來執行任何動畫。Android:視頻紋理視圖

我曾嘗試這個代碼,但我不知道在哪裏放置video.mp4

public class MainActivity extends Activity implements 
     TextureView.SurfaceTextureListener { 

    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.activity_main); 

     TextureView textureView = (TextureView) findViewById(R.id.textureView1); 
     textureView.setSurfaceTextureListener(this); 
    } 

    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, 
      int height) { 
     MediaPlayer mediaPlayer = new MediaPlayer(); 
     mediaPlayer.setSurface(new Surface(surface)); 

     try { 
      mediaPlayer.setDataSource("video.mp4"); 
      mediaPlayer.prepare(); 
      mediaPlayer.start(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1, 
      int arg2) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onSurfaceTextureUpdated(SurfaceTexture arg0) { 
     // TODO Auto-generated method stub 

    } 
} 

請提出任何工作代碼幫助我。或者如果我的是正確的,那麼把video.mp4放在文件夾中。

在此先感謝...

+0

你在說什麼文件夾? – pskink

+0

我嘗試通過複製原始文件夾中的video.mp4,並在資產文件夾 – Sri

+0

中使用完整路徑:以「/」開頭的內容 – pskink

回答

0

其放置在原文件夾中,並嘗試下面的一個,看看是否有幫助。

public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 
    MediaPlayer mediaPlayer = new MediaPlayer(); 
    mediaPlayer.setSurface(new Surface(surface)); 
    try { 
     Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.your_raw_file); 
     mediaPlayer.setDataSource(this, video); 
     mediaPlayer.prepare(); 
     mediaPlayer.start(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
}