facebook twitter hatena line email

「Android/VideoView」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(動画プレイヤーサンプル)
行4: 行4:
 
  import android.support.v7.app.AppCompatActivity;
 
  import android.support.v7.app.AppCompatActivity;
 
  import android.os.Bundle;
 
  import android.os.Bundle;
 +
import android.view.Gravity;
 +
import android.view.ViewGroup;
 +
import android.widget.FrameLayout;
 
  import android.widget.VideoView;
 
  import android.widget.VideoView;
 
  public class MainActivity extends AppCompatActivity {
 
  public class MainActivity extends AppCompatActivity {
行12: 行15:
 
         setContentView(R.layout.activity_main);
 
         setContentView(R.layout.activity_main);
 
         mVideoView = new VideoView(this);
 
         mVideoView = new VideoView(this);
         setContentView(mVideoView);
+
         FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
 +
        layoutParams.gravity = Gravity.CENTER;
 +
        FrameLayout layout = (FrameLayout)findViewById(R.id.videoView);
 +
        layout.addView(mVideoView, layoutParams);
 
         mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
 
         mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
 
             @Override
 
             @Override
行23: 行29:
 
     }
 
     }
 
  }
 
  }
 +
 +
res/layout/activity_main.xml
 +
<FrameLayout android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="200dp"></FrameLayout>
  
 
参考:http://blog.livedoor.jp/sen_ritsu/archives/51350872.html
 
参考:http://blog.livedoor.jp/sen_ritsu/archives/51350872.html

2018年10月16日 (火) 18:31時点における版

動画プレイヤーサンプル

すぐに再生される

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
   private VideoView mVideoView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       mVideoView = new VideoView(this);
       FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
       layoutParams.gravity = Gravity.CENTER;
       FrameLayout layout = (FrameLayout)findViewById(R.id.videoView);
       layout.addView(mVideoView, layoutParams);
       mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
           @Override
           public void onPrepared(MediaPlayer mp) {
               mVideoView.start();
           }
       });
       mVideoView.setVideoPath("~.mp4");
       mVideoView.start();
   }
}

res/layout/activity_main.xml

<FrameLayout android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="200dp"></FrameLayout>

参考:http://blog.livedoor.jp/sen_ritsu/archives/51350872.html