0

I adding VideoView in fragment but only I get black background nothing more I tried with two codes but both doesn't work can you help me?

PS I don't need play, stop button and anything other just to show the video Here is the code that I add will be good if I can mute the audio of the video and replay

Code 1

package com.Hristijan.Aleksandar.GymAssistant.Exercises;

import android.media.session.MediaController;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.VideoView;   
import java.net.URL;    

/**
 * A simple {@link Fragment} subclass.
 */
public class BenchFragment extends Fragment {
    private VideoView MyVideoView;

    public BenchFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_bench, container, false);
        MyVideoView = (VideoView)rootView.findViewById(R.id.video_view);
        Uri uri= Uri.parse("android.resource://"+getActivity().getPackageName()+"/"+R.raw.bench);
        MyVideoView.setVideoURI(uri);
        MyVideoView.start();
        return inflater.inflate(R.layout.fragment_bench, container, false);
    }    
}

Code2

package com.hristijan.aleksandar.gymworkout.myapplication;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.VideoView;

import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private VideoView MyVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyVideoView = findViewById(R.id.videoViewId);
        Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.bench);
        MyVideoView.setVideoURI(uri);
        MyVideoView.start();

    }
}

Layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#000000"
    tools:context="com.Hristijan.Aleksandar.GymAssistant.Exercises.BenchFragment">

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>
Komal12
  • 3,340
  • 4
  • 16
  • 25

3 Answers3

0

Try this to play,replay and identify the error

private void startVideo() {
    String path = "android.resource://" + getPackageName() + "/" + R.raw.crop;
//  Log.e(TAG,Uri.parse(path) + " ");
    video_view.setVideoURI(Uri.parse(path));
    video_view.start();
    video_view.setOnErrorListener(new MediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int i, int i1) {
            Log.e(TAG,String.format("Error: What: %d, Extra: %d",i,i1));
            return false;
        }
    });
    video_view.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.start();
        }
    });
}

If the video doesn't play it should log error in LogCat. You can identify what kind of error using this doc

About Muting the Audio

You can refer to this Question basically you just need to get the media player object when the content is ready then you can mute the audio by setting m.setVolume(0f, 0f);

Paul Chu
  • 1,249
  • 3
  • 19
  • 27
Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89
0

Try putting videoView.start() inside videoView.setOnPreparedListener() as:

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
    @Override
        public void onPrepared(MediaPlayer mediaPlayer){
        videoView.start();
    }
});
Paul Chu
  • 1,249
  • 3
  • 19
  • 27
dazed'n'confused
  • 231
  • 3
  • 13
0

I am sure that Code 1 and Code 2 can work

First, make sure your video format is Supported Media Formats

Then, make sure your video file bench stored in ...app\src\main\res\raw (And your file name should be bench not bench.xxx)

On the other hand, in Code 2 something is wrong:

MyVideoView = findViewById(R.id.videoViewId);

should be

MyVideoView = findViewById(R.id.video_view);

in order to find a VideoView by correct Id

Paul Chu
  • 1,249
  • 3
  • 19
  • 27
  • Good;) try dig deep on Android, I saw you got many questions about your Exercise app. – Paul Chu Feb 27 '18 at 07:48
  • Yeah thanks again can you tell me how can i put Ads in the background? because they make my ui to lag too much –  Feb 27 '18 at 07:50