Reputation: 55
I know this has been asked many times. I've tried out many of the solutions mentioned previously like increasing API level, changing android device, changing video, changing vide format. The same error each time. I am using Nexus 6p, running Oreo 8.1 and the minimum API is like from Marshmellow. xml code:
<VideoView
android:background="@color/colorPrimaryDark"
android:layout_height="600dp"
android:layout_width="fill_parent"
android:id="@+id/VideoView" />
<Button
android:text="@string/play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onButtonClick"
android:background="@color/colorPrimary"
android:id="@+id/button"/>
java code:
public class IntroVideoActivity extends AppCompatActivity {
VideoView VideoView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro_video);
VideoView = (VideoView) findViewById(R.id.VideoView);
button = (Button) findViewById(R.id.button);
}
public void onButtonClick (View v) {
String videopath = "android.resource://com.example.nisat.favor"+R.raw.anime;
Uri uri = Uri.parse(videopath);
VideoView.setVideoURI(uri);
VideoView.start();
}
}
I've tried this code aswell:
VideoView videoView = (VideoView)findViewById(R.id.VideoView);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoPath("vectorvideo");
videoView.start();
Upvotes: 0
Views: 4036
Reputation: 1061
use this code
mention permission in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
and this line of code inside the MainActivity.class
String videopath = "android.resource://com.example.cloudanalogy.play_video/"+R.raw.a;
Uri vidUri = Uri.parse(videopath);
videoView.setVideoURI(vidUri);
videoView.start();
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
Upvotes: 0
Reputation: 27
Video must be in a supported format (3gp, wmv, mp4 ) and named with lower case, numerics, underscores and dots in its filename likewise:video_file.mp4.
Instead of
String videopath = "android.resource://com.example.nisat.favor"+R.raw.anime;
try
String videopath = "android.resource://" + getPackageName() + "/" + R.raw.anime;
Upvotes: 1
Reputation: 91
Check when is your method invoked?
onButtonClicked(View v)
Try this code & first try without button click if it worked then call it on button click. I am using this in my activity and it is working fine.
String basePath = "android.resource://" + getPackageName() +"/"+"/raw/";
Intent intent = getIntent();
String videoName = intent.getStringExtra(VIDEO_NAME);
final boolean videoRepeat = intent.getBooleanExtra(VIDEO_REPEAT, false);
Uri videoUri = Uri.parse(basePath+videoName);
VideoView videoPlayer = (VideoView) findViewById(R.id.videoPlayer);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoPlayer);
mc.setMediaPlayer(videoPlayer);
mc.setVisibility(View.GONE);
videoPlayer.setMediaController(mc);
videoPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
if(!videoRepeat){
finish();
}
}
});
videoPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if(videoRepeat) {
mp.setLooping(true);
}else{
mp.setLooping(false);
}
}
});
videoPlayer.setVideoURI(videoUri);
videoPlayer.start();
Upvotes: 0