SHARE
TWEET

Untitled

a guest Oct 12th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {
  2.  
  3.     private static final String TAG = "videotestmediaplayer";
  4.  
  5.     private static final String FILE_NAME = "cat.mp4"; // ваш файл
  6.  
  7.     private MediaPlayer mMediaPlayer;
  8.     private TextureView mTextureView;
  9.  
  10.     public String folderPath,folderPath2;
  11.     public File f;
  12.     public Uri uri;
  13.     public Context context;
  14.     public Button btn;
  15.     public Button btn2;
  16.  
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);
  21.  
  22.         //check for permission
  23.         if(ContextCompat.checkSelfPermission(this,
  24.                 Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED){
  25.             //ask for permission
  26.             requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
  27.  
  28.         }
  29.  
  30.         mTextureView = (TextureView) findViewById(R.id.textureView);
  31.         mTextureView.setSurfaceTextureListener(this);
  32.  
  33.         folderPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
  34.         folderPath2 = folderPath;
  35.         folderPath = folderPath + "/cloud0.mp4";
  36.         folderPath2 = folderPath2 + "/cloud2.mp4";
  37.  
  38.         f = new File(folderPath);
  39.         if (!f.exists()) {
  40.             Log.d(TAG, "not found");}
  41.  
  42.         //uri = Uri.parse(folderPath);
  43.  
  44.         btn = (Button) findViewById(R.id.button);
  45.         btn.setOnClickListener(new View.OnClickListener() {
  46.             @Override
  47.             public void onClick(View view) {
  48.                 if (mMediaPlayer.isPlaying()) {
  49.                     mMediaPlayer.stop();
  50.                 }
  51.  
  52.                 try {
  53.                     Log.d(TAG, folderPath2);
  54.                     uri = Uri.parse(folderPath2);
  55.                     mMediaPlayer.setDataSource(context, uri);
  56.                 } catch (IOException e) {
  57.                     e.printStackTrace();
  58.                 }
  59.                 mMediaPlayer.prepareAsync();
  60.  
  61.             }
  62.         });
  63.  
  64.     }
  65.  
  66.     @Override
  67.     protected void onDestroy() {
  68.         super.onDestroy();
  69.         if (mMediaPlayer != null) {
  70.             mMediaPlayer.stop();
  71.             mMediaPlayer.release();
  72.             mMediaPlayer = null;
  73.             Log.d(TAG, "onDestroy");
  74.         }
  75.     }
  76.     @Override
  77.     public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
  78.         Surface surface = new Surface(mTextureView.getSurfaceTexture());
  79.  
  80.         try {
  81.             mMediaPlayer = new MediaPlayer();
  82.             uri = Uri.parse(folderPath);
  83.             mMediaPlayer.setDataSource(this ,uri);
  84.             mMediaPlayer.setSurface(surface);
  85.             context = this;
  86.             mMediaPlayer.prepareAsync();
  87.             Log.d(TAG, this.toString());
  88.  
  89.             mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
  90.                 @Override
  91.                 public void onPrepared(MediaPlayer mediaPlayer) {
  92.                     mediaPlayer.start();
  93.                     Toast.makeText(MainActivity.this, "Started", Toast.LENGTH_SHORT).show();
  94.                 }
  95.             });
  96.             mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  97.                 @Override
  98.                 public void onCompletion(MediaPlayer mediaPlayer) {
  99.                     Log.d(TAG, "finished");
  100.                     if (mMediaPlayer.isPlaying()) {
  101.                         mMediaPlayer.stop();
  102.                     }
  103.                     try {
  104.                         uri = Uri.parse(folderPath2);
  105.                         mMediaPlayer.setDataSource(context ,uri);
  106.                         mMediaPlayer.prepareAsync();
  107.                     } catch (IOException e) {
  108.                         e.printStackTrace();
  109.                     }
  110.  
  111.                 }
  112.             });
  113.  
  114.         } catch (IOException e) {
  115.             Log.d(TAG, e.getMessage());
  116.         }
  117.     }
  118.    
  119. mMediaPlayer.Reset();                    // <===
  120. mMediaPlayer.setDataSource(this ,uri);
RAW Paste Data
Top