This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 1 year ago.
Actually I want to incooperate multiple videos in a single xml file .But while adding I am getting error.
Actually I am confused why the error has occurred.So I have attached my java code below .The part which i wrote in bold Im getting error over there.I want to add mutiple videos in one page.So I was trying that.
public class bvideos extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bvideos);
VideoView videoView = findViewById(R.id.video_view1);
String videoPath = "android.resource://" + getPackageName() + "/" + R.raw.bvideo1;
VideoView **videoView** = findViewById(R.id.video_view1);
String **videoPath** = "android.resource://" + getPackageName() + "/" + R.raw.bvideo1;
Uri uri = Uri.parse(videoPath);
videoView.setVideoURI(uri);
MediaController mediaController = new MediaController(this);
videoView.setMediaController(mediaController);
mediaController.setAnchorView(videoView);
}
}
// make an array or Uri data type and add uri's in it
MediaController mc = new MediaController(this);
mc.setPrevNextListeners(new View.OnClickListener() {
#Override
public void onClick(View v) {
// get video from next index and set to video view
}
}, new View.OnClickListener() {
#Override
public void onClick(View v) {
// get video from previous index and set to video view
}
});
videoView.setMediaController(mc);
Related
I am trying to play a series of videos in an array in Android, however when the below code runs only the last element / video of the array plays.
How can I loop through the array and play the videos one after the other?
I get the feeling that the continuation of the loop happens immediately after the videoView.start() command so only the last one plays.
Here is an approximation of my code...
VideoView videoView = (VideoView) findViewById(R.id.videoView);
String file_location = "path/to/my/files/"; // external storage
String filepaths[] = {"1_A.mp4", "1_B.mp4"}; // array could have many more elements
for(String filepath: filepaths){
String path = file_location + filepath;
videoView.setVideoPath(path);
videoView.start();
}
I have tried adding the setOnCompletionListener and putting the continue inside the onCompletion but the error is "continue outside of loop"
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
continue;
}
});
How can I play each video sequentially with as little / no gap in between?
Don't use loop because you are supposed to set the path of the next video only after previous one finished so instead of loop create a field called currentPlayingIndex make it increment after each video finishes and then set path from that ....
Like this
private int currentPlayingIndex; // Keep this as gloabal variable
VideoView videoView = (VideoView) findViewById(R.id.videoView);
String file_location = "path/to/my/files/"; // external storage
String filepaths[] = {"1_A.mp4", "1_B.mp4"}; // array could have many more elements
String path = file_location + filepaths[currentPlayingIndex];
videoView.setVideoPath(path);
videoView.start();
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
currentPlayingIndex++; //Increment index here
if(currentPlayingIndex < filepaths.length)
{
String newPath = file_location + filepaths[currentPlayingIndex];
videoView.setVideoPath(newPath);
videoView.start();
}else {
//Add logic here when all videos are played
}
}
});
Note: I am not sure how long it will take to switch between videos ...
The first: You need to play *.mp4 in the array of index 0.
Then when the video play complet,use the player play the *.mp4 in the array of index next.
My English expression may not be very good, I hope you can understand
Here is my code on full screen video.
It work just fine in emulator but not in real android phone.
public void videoClick(View view){
Intent mIntent = new Intent(getBaseContext(),VideoFullscreenActivity.class);
videoUri = Uri.parse("android.resource://tk.myessentialoils.ideasapp/raw/"+ contentStringList[count][2]);
mIntent.putExtra("videoUri",videoUri);
startActivity(mIntent);
}
My thinking is that the Uri problem.
Xiaomi android have different uri than other.
Some Huawei phone also not functioning well.
So is there any alternative to get the file instead?
Perhaps a work around that will work on all version of android.
Edit 1
as per Vivek Mishra suggestions,
tried the below
Intent mIntent = new Intent(getBaseContext(),VideoFullscreenActivity.class);
String path = "file:///android_asset/"+ contentStringList[count][2];
videoUri = Uri.parse(path);
mIntent.putExtra("videoUri",videoUri);
startActivity(mIntent);
However i got this error >> Can't play this video
as per How to load videos from assets folder? (to play them with VideoView) asset folder cannot play video
Edit 2
same question as Nullpointerexception, i cant get the Media player to work with my code.
Uri videoUri = getIntent().getParcelableExtra("videoUri");
VideoView videoView=findViewById(R.id.myvideoview);
videoView.setVideoURI(videoUri);
//videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.full));
MediaController mediaController = new MediaController(this);
videoView.setMediaController(mediaController);
videoView.start();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
how do i implement the below code in my code above.
AssetFileDescriptor afd;
try {
afd = getAssets().openFd("v.mp4");
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),
afd.getLength());
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
} catch (Exception e) { e.printStackTrace();}
I have been modifying an example video player code; the example code played the video from the program resource directory. I copied the video to the phone's /storage/emulated/0/DCIM/ directory and add three lines:
String videoName1 = Environment.getExternalStorageDirectory().getPath()+"/DCIM/steprock";
Uri videoUri = Uri.parse(videoName1);
videoView.setVideoURI(videoUri);
to replace:
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.steprock));
This change causes a file not found error. The video is in the Phone storage DCIM directory. I also tried adding the .mp4 to the video name.
In debug mode the uri appears to be the correct value: /storage/emulated/0/DCIM/steprock
Can anyone spot the code error? A partial listing is below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView videoView = (VideoView) findViewById(R.id.video_view);
String videoName1 = Environment.getExternalStorageDirectory().getPath()+"/DCIM/steprock";
Uri videoUri = Uri.parse(videoName1);
videoView.setVideoURI(videoUri);
//videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.steprock));
This is code which play video from path you describe for video in VideoView.
// Video path.
path = Environment.getExternalStorageDirectory().getPath()+"/DCIM/steprock.mp4";
videoView = (VideoView) findViewById(R.id.video_view);
final MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri = Uri.parse(path);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
I found the error, forgot to add the read permission below to the manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<application ...
I want to add a youtube video to my page. I downloaded the video and added it into the res-> raw folder. The video is marked minecraft_trailer.mp4. I am getting an error in the javascript page and I cant figure out what it is. Please help.
package com.treacheryofimages.www;
import android.app.Activity;
import android.os.Bundle;
import android.widget.VideoView;
public class OtherActivity7 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other7);
}
VideoView vv = (VideoView)this.findViewById(R.id.videoView1);
String fileName = "android.resource://" + getPackageName() + "/" +
R.raw.minecraft_trailer;
vv.setVideoURI(Uri.parse(minecraft_trailer));
vv.start();
}
}
The error is coming from the vv.setVideoURI(Uri.parse(minecraft_trailer)); line
I cant tell what it is. Its possibly whats inside the ().
Also, what is supposed to go inside those ()
You are passing the wrong variable minecraft_trailer when calling vv.setVideoURI();
it should be vv.setVideoURI(Uri.parse(fileName ));
check this out :
VideoView vd = (VideoView) findViewById(R.id.VideoView);
Uri uri = Uri.parse("android.resource://"+getPackageName() + "/"+R.raw.video);
mc = new MediaController(this);
vd.setMediaController(mc);
vd.setVideoURI(uri);
vd.start();
I want to play a video from res/raw in android here is my code:
public class CreditsActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.creditslayout);
VideoView v = (VideoView) findViewById(R.id.video);
v.setVideoURI(Uri.parse("android.resource://test.test.test/raw/maincredits"));
v.start();
}
}
But I hear only the sound of video. And the emulator doesn't show the video itself the size of file is 2.8 MB.
Try below code :
public class CreditsActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.creditslayout);
VideoView v = (VideoView) findViewById(R.id.video);
v.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video_file));
v.start();
}
}
Please try this.
Uri uri = Uri.parse("android.resource://test.test.test/"+R.raw.[video_resid]);
v.setVideoURI(uri);
try Uri.parse("android.resource://test.test.test/" + R.raw.yourVideoName)
Try this
// trailer_final is the video name
String fileName = "android.resource://"+ getPackageName()+"/raw/trailer_final";
VideoView mvideo = (VideoView) findViewById(R.id.playVideo);
mvideo.setVideoPath(fileName);
MediaController controller = new MediaController(this);
mvideo.setMediaController(controller);
mvideo.requestFocus();
mvideo.start();
Try this:
VideoView view = (VideoView)findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
view.setVideoURI(Uri.parse(path));
view.start();
Just pass the name of the video residing in the raw folder under res of the project.
The emulator doesn't show the video. I had this problem on a project. We tried it on an actual device and it played perfectly. Try to play it on actual device. It will likely work.