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();}
Related
I'm done my Android app, just need to add some looping background music. Here is my method for playing the song
public void playAudio(){
path = "android.resource://" + getPackageName() + "/" + R.raw.music1;
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path);
mp.prepare();
mp.setLooping(true);
mp.setVolume(100, 100);
mp.start();
} catch (Exception e) {
e.printStackTrace();
Log.d("NOTE WORKEING","NOT WORKING");
}
}
It's not working....It's going to catch everytime, and I don't know why. Please help me. Music1 is an mp3 file.
Thank you
Make sure your music1 file is in a Android playable Android format.
Then just use this code:
MediaPlayer mediaPlayer = MediaPlayer.create(YourActivity.this, R.raw.music1);
try {
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(100, 100);
mediaPlayer.start();
}
catch (Exception e) {
e.printStackTrace();
Log.d("NOT WORKEING","NOT WORKING");
}
You don't even need to call prepare() method.
I tested it with an mp3 file and it works perfectly.
If #Squonk's answer does not work, then the problem has to do with trying to start the music file before the MediaPlayer has prepared it. Try changing the MediaPlayer's start code to the following:
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
start();
}
});
Let me know if this helps, I have a good amount of experience with the MediaPlayer class and can help you troubleshoot.
Using mp.setDataSource(path) requires a valid filesystem path or URL.
In your case, path is a string representation of a Uri in which case you need to use a different approach.
Try setDataSource(Context context, Uri uri). You'll obviously need to provide a valid Context and parse your path variable into a Uri. Example...
mp.setDataSource(getApplicationContext(), Uri.parse(path));
Also change the path to...
path = "android.resource://" + getPackageName() + "/raw/music1";
So I'm trying to play a basic avi video in android, it seems to run fine on Windows Media Player, VLC, etc. so it doesn't look like it's requiring any complicated codecs. I have a video view in my app and that's it, and I have my video in my resources directory under:
res/raw/my_video.avi
This is the code I'm using to load my video:
setContentView(R.layout.activity_main);
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.my_video);
videoView.setVideoURI(video);
videoView.start();
And it does not work. I get a popup saying "Can't play this video" along with a logcat message:
03-10 01:42:12.102: E/(185): Failed to open file 'android.resource://com.securespaces.android.bootstrap/2130968576'. (No such file or directory)
03-10 01:42:12.102: E/MediaPlayer(9737): error (1, -2147483648)
03-10 01:42:12.142: E/MediaPlayer(9737): Error (1,-2147483648)
I'm running this on a Nexus 5 running 4.4.2 stock by the way. I'm following instructions that I found in other stack over flow questions here: How to play videos in android from assets folder or raw folder? with some minor tweaks so that I am using a VideoView that I grab from a layout file.
I'm really stumped as to why this isn't working. I've browsed through a few questions on this subject, but this feels like something that should be a duplicate. I'm running this on a Nexus 5 running 4.4.2 stock by the way.
To clarify the question, I'm wondering what I am doing wrong, or is there an alternative for just playing a simple AVI video?
Write your package name statically and check whether video plays or not.
Try out as below:
VideoView videoView = (VideoView) findViewById(R.id.videoView1);
Uri video = Uri.parse("android.resource://com.securespaces.android.bootstrap/" + R.raw.my_video);
videoView.setVideoURI(video);
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
Implement this :
public static void getVideoFromRaw(String rawPath) {
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(mContext);
mediacontroller.setAnchorView(mVideoview);
// Get the URL from String VideoURL
Uri mVideo = Uri.parse(rawPath);
mVideoview.setMediaController(mediacontroller);
mVideoview.setVideoURI(mVideo);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
mVideoview.requestFocus();
mVideoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoview.start();
}
});
mVideoview.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
}
});
}
Thanks
I need to play a live stream on devices with 2.x and greater versions. This states that it's impossible to play live streams on devices with Android 2.x.
What're my options here ? Especially I'm interested in streaming audio - what format should i pick and in conjunction with which protocol ?
P.S. I've tried Vitamio - don't want to make customers download third party libraries.
UPD
How come I can play this stream "http://188.138.112.71:9018/" ?
try this example for RTSP streaming (the url should support RTSP) for video change the code to support just audio
public class MultimediaActivity extends Activity {
private static final String RTSP = "rtsp://url here";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.multimedia);
//***VideoView to video element inside Multimedia.xml file
VideoView videoView = (VideoView) findViewById(R.id.video);
Log.v("Video", "***Video to Play:: " + RTSP);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
Uri video = Uri.parse(RTSP);
videoView.setMediaController(mc);
videoView.setVideoURI(video);
videoView.start();
}
}
EDIT:
Live Audio streaming using MediaPlayer in Android
Live Audio streaming in android, from 1.6 sdk onwards is become so easy. In setDataSource() API directly pass the url and audio will play without any issues.
The complete code snippet is,
public class AudioStream extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = "http://www.songblasts.com/songs/hindi/t/three-idiots/01-Aal_Izz_Well-(SongsBlasts.Com).mp3";
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(url);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();
} catch (Exception e) {
Log.i("Exception", "Exception in streaming mediaplayer e = " + e);
}
}
}
You can use RTSP protocol which is supported by Android native media player.
player = new MediaPlayer();
player.reset();
player.setDataSource(intent.getStringExtra("Path"));
player.prepare();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
Where path would be your rtsp audio streaming url.
Is there any way I can play a video that has been stored in the raw file, without the use of the uri. If not how do i properly set the uri. Let's say i have file named movie and I want to play it with a videoviewer. How would i do this? Also would i have to write the data to the sd card. or can i just play it from the raw.
Why cant I just do
uri uri.parse("android.resources://"+this.getApplicationContext().getPackageName()+movie);
This is what I have it as now if you could can you point out the errors to me so i know and i wont have to have this problem any more
VideoView videoview = (VideoView) findViewById(R.id.videoView1);
videoview.setKeepScreenOn(true);
videoview.setVideoPath("android.raw://com.example.movievp8");
MediaController controller = new MediaController(videoview.getContext());
videoview.setMediaController(controller);
videoview.start();
videoview.requestFocus();
Here is something that works for me:
videoResource = R.raw.some_video;
String uri = String.format("android.resource://%s/%d", context.getPackageName(), videoResource);
animationCanvas = (VideoView) ui.findViewById(R.id.animation_canvas);
animationCanvas.setVisibility(View.VISIBLE);
animationCanvas.setVideoURI(Uri.parse(uri));
animationCanvas.setOnCompletionListener(this);
animationCanvas.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
animationCanvas.start();
}
});
I am beginner in android development and try to play video from link. But it's giving error "sorry,we can't play this video". I tried so many links but for all links its show same error.
My code is the following
public class VideoDemo extends Activity {
private static final String path ="http://demo.digi-corp.com/S2LWebservice/Resources/SampleVideo.mp4";
private VideoView video;
private MediaController ctlr;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.videoview);
video = (VideoView) findViewById(R.id.video);
video.setVideoPath(path);
ctlr = new MediaController(this);
ctlr.setMediaPlayer(video);
video.setMediaController(ctlr);
video.requestFocus();
}
}
Logcat shows following error message:
04-12 15:04:54.245: ERROR/PlayerDriver(554): HandleErrorEvent: PVMFErrTimeout
It has something to do with your link and content. Try the following two links:
String path="http://www.ted.com/talks/download/video/8584/talk/761";
String path1="http://commonsware.com/misc/test2.3gp";
Uri uri=Uri.parse(path1);
VideoView video=(VideoView)findViewById(R.id.VideoView01);
video.setVideoURI(uri);
video.start();
Start with "path1", it is a small light weight video stream and then try the "path", it is a higher resolution than "path1", a perfect high resolution for the mobile phone.
Try this:
String LINK = "type_here_the_link";
setContentView(R.layout.mediaplayer);
VideoView videoView = (VideoView) findViewById(R.id.video);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
Uri video = Uri.parse(LINK);
videoView.setMediaController(mc);
videoView.setVideoURI(video);
videoView.start();
pDialog = new ProgressDialog(this);
// Set progressbar message
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// Show progressbar
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(mVideoView);
Uri videoUri = Uri.parse(videoUrl);
mVideoView.setMediaController(mediacontroller);
mVideoView.setVideoURI(videoUri);
} catch (Exception e) {
e.printStackTrace();
}
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
mVideoView.start();
}
});
mVideoView.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
finish();
}
});
You can do it using FullscreenVideoView class. Its a small library project. It's video progress dialog is build in. it's gradle is :
compile 'com.github.rtoshiro.fullscreenvideoview:fullscreenvideoview:1.1.0'
your VideoView xml is like this
<com.github.rtoshiro.view.video.FullscreenVideoLayout
android:id="#+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
In your activity , initialize it using this way:
FullscreenVideoLayout videoLayout;
videoLayout = (FullscreenVideoLayout) findViewById(R.id.videoview);
videoLayout.setActivity(this);
Uri videoUri = Uri.parse("YOUR_VIDEO_URL");
try {
videoLayout.setVideoURI(videoUri);
} catch (IOException e) {
e.printStackTrace();
}
That's it. Happy coding :)
If want to know more then visit here
Edit:
gradle path has been updated. compile it now
compile 'com.github.rtoshiro.fullscreenvideoview:fullscreenvideoview:1.1.2'
Try Exoplayer2
https://github.com/google/ExoPlayer
It is highly customisable
private void initializePlayer() {
player = ExoPlayerFactory.newSimpleInstance(
new DefaultRenderersFactory(this),
new DefaultTrackSelector(), new DefaultLoadControl());
playerView.setPlayer(player);
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow, playbackPosition);
Uri uri = Uri.parse(getString(R.string.media_url_mp3));
MediaSource mediaSource = buildMediaSource(uri);
player.prepare(mediaSource, true, false);
}
private MediaSource buildMediaSource(Uri uri) {
return new ExtractorMediaSource.Factory(
new DefaultHttpDataSourceFactory("exoplayer-codelab")).
createMediaSource(uri);
}
#Override
public void onStart() {
super.onStart();
if (Util.SDK_INT > 23) {
initializePlayer();
}
}
Check this url for more details
https://codelabs.developers.google.com/codelabs/exoplayer-intro/#2
please check this link :
http://developer.android.com/guide/appendix/media-formats.html
videoview can't support some codec .
i suggested you to use mediaplayer , when get "sorry , can't play video"
I also got stuck with this issue.
I got correct response from server, but couldn`t play video. After long time I found a solution here.
Maybe, in future this link will be invalid.
So, here is my correct code
Uri video = Uri.parse("Your link should be in this place ");
mVideoView.setVideoURI(video);
mVideoView.setZOrderOnTop(true); //Very important line, add it to Your code
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
// here write another part of code, which provides starting the video
}}
Check this UniversalVideoView library its simple and straight forward with controller as well.
Here is the code to play the video
Add this dependancyy in build.gradle
implementation 'com.linsea:universalvideoview:1.1.0#aar'
Java Code
UniversalVideoView mVideoView = findViewById(R.id.videoView);
Uri uri=Uri.parse("https://firebasestorage.googleapis.com/v0/b/contactform-d9534.appspot.com/o/Vexento%20-%20Masked%20Heroes.mp4?alt=media&token=74c2e448-5b1b-47b7-b761-66409bcfbf56");
mVideoView.setVideoURI(uri);
UniversalMediaController mMediaController = findViewById(R.id.media_controller);
mVideoView.setMediaController(mMediaController);
mVideoView.start();
Xml Code
<FrameLayout
android:id="#+id/video_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#android:color/black">
<com.universalvideoview.UniversalVideoView
android:id="#+id/videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
app:uvv_autoRotation="true"
app:uvv_fitXY="false" />
<com.universalvideoview.UniversalMediaController
android:id="#+id/media_controller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:uvv_scalable="true" />
</FrameLayout>
Check whether your phone supports the video format or not.Even I had the problem when playing a 3gp file but it played a mp4 file perfectly.