I'd like to know how to properly display video from an authenticated URL to VideoView. My code is functioning before Lollipop (5.0). Now, after compiling to APIs 21 and 22, the error occurs. I have tried compiling back to API 19 but the appcompat library I'm using break so many codes. Here's my sample code:
Uri videoUri = Uri.parse("https://user:12345#sample.com/dir1/dir2/dir3/myVideo.mp4");
vidAtt.setVideoURI(videoUri);
I already tried
vidAtt.setVideoURI(videoUri, headers);
but the minimum API is 21 while mine is API 16. I tried the generated URI and paste into the browser and it works. It just doesn't work in the device. I also tried passing the URI as an intent so that it can open the video link externally, whether using the stock video player or a third part one. It didn't work too.
Any help would be appreciated. Thanks in advance!
You have to use the setVideoURI-Method which is only available with Reflection:
Method setVideoURIMethod = videoview.getClass().getMethod("setVideoURI", Uri.class, Map.class);
setVideoURIMethod.invoke(videoview, Uri.parse(url), /*HashMap<String, String>*/ basicAuthentication;
Replace basicAuthentication with your Basic Authentication Header.
Can you try this
try {
VideoView.setVideoPath("https://user:12345#sample.com/dir1/dir2/dir3/myVideo.mp4");
} catch (Exception e) {
e.printStackTrace();
}
VideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
//
VideoView.start();
}
});
Related
I am trying to run media through android mediaplayer with some API link (thus doesn't have .mp3 in the end of url) as follows:
android code :
headers.put("token", token_value);
myPlayer.setDataSource(
getApplicationContext(),
Uri.parse(url),//url : http://basedomain.com/api/getmp3
headers
);
backend laravel code :
$song_path = "abc.mp3";
$path = storage_path().DIRECTORY_SEPARATOR."somefolder".DIRECTORY_SEPARATOR.$song_path;
$response = new BinaryFileResponse($path);
return $response;
I am getting the binary response from laravel code(tested through postman), however android code is not playing the music from laravel response.
android issue list :
1) W/MediaPlayer: Couldn't open http://basedomain.com/api/getmp3: java.io.FileNotFoundException: No content provider: http://basedomain.com/api/getmp3
2) E/MediaPlayer: error (1, -2147483648)
I've just tried the following code in Android studio targeting Android SDK 28 and it works fine. Make sure your API is actually returning the mp3 file.
Android MediaPlayer docs
try {
MediaPlayer player = new MediaPlayer();
player.setDataSource("https://yourdomain/yourmusic.mp3");
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
Try testing your code using a url to a known public mp3 file to verify that the MediaPlayer code is correct. Once that is working, you can narrow the issue down to either the token not being set correctly or your server not returning the mp3 file correctly. Maybe the response doesn't indicate it is an mp3 file correctly and thus the media player doesn't know how to handle it.
I finally found the reason behind this issue. My backend laravel api call was a POST request. Changing the same to Get request worked for me.
Also changing request from POST to GET was not an issue as my request parameters were sent through the header. (This was done because android mediaplayer's setDataSource method only accepts headers and not body parameters).
In short android mediaplayer is expecting us to use GET request only.
I am working on an app which stream youtube videos.
Here is my code snippet :
MediaController mediaController = new MediaController(PlayVideo.this);
mediaController.setAnchorView(videoView_Video);
mediaController.setMediaPlayer(videoView_Video);
Uri videoUri = Uri.parse(url);
videoView_Video.setMediaController(mediaController);
videoView_Video.setVideoURI(videoUri);
videoView_Video.requestFocus();
videoView_Video.start();
mediaController.show();
My URI:
videoUri = rtsp://v2.cache8.c.youtube.com/CiILENy73wIaGQm-LZitWsUGKxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
Now I Am getting an error as below:
1. http youtube = false, scheme = rtsp
2. Couldn't open file on client side, trying server side
3. Error (1,-2147483648)
4. Error: 1,-2147483648
And a dialog with a message :
Please suggest any answer to overcome this.
Use the proper API instead of manually parsing and finding videos.
https://developers.google.com/youtube/android/player/
you just import the proper YouTubeVideoPlayer and things work fine.
ps.: I don't suggest using this in a ViewPager, I only had problems with that.
Google has a YouTube Android Player API that enables you to incorporate video playback functionality into your Android applications. The API itself is very easy to use and works well. For example, here is how to create a new activity to play a video using the API.
Intent intent = YouTubeStandalonePlayer.createVideoIntent(this, "<<YOUTUBE_API_KEY>>", "<<Youtube Video ID>>", 0, true, false);
startActivity(intent);
See this for more details.
I'm using a book called Android Wireless Application Development 2nd edition 2009 (L.Darcey & S.Conder, published by Addison Wesley) as my literature for a course I'm currently doing in Android app development. In the third chapter of this book you use the MediaPlayer class to initiate a media player object to stream mp3 files to your app using to this method below.
public void playMusicFromWeb() {
try {
Uri file = Uri.parse("http://downloads.bbc.co.uk/podcasts/6music/adamandjoe/adamandjoe_20111231-1430a.mp3");
mp = MediaPlayer.create(this, file);
mp.start();
}
catch (Exception e) {
Log.e(DEBUG_TAG, "Player failed", e);
}
}
Happy days, this all works just fine in API 8 (which is what the book uses). However trying to use this method in a higher API, especially 15 which is (on writing) the latest API available I get a NullPointerException thrown. Debugging makes me none the wiser as the file variable seems to hold the string value fine, but it won't translate into a functional mp3 stream. I have tried various different approaches using prepare(); prepareAsync(); setDataSource(); reset(); etc., however nothing seems to work. You'd think that my teachers would be able to help me out, but they apparently don't know what to do either.
I now assume that this way of streaming must be obsolete in higher API's than 8. I would be very grateful if someone could shine a light on how to stream an mp3 file in API 15.
I think you have to do more than that. Here is an example for doing what you want:
The Header's content-type of the URL could be bad.
You could try this syntax:
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(URL_OF_FILE);
mp.prepare();
mp.start();
See if the NullPointerException remains.
You could also view a more recent tutorial on this particular subject.
It was as simple as adding this line:
<uses-permission android:name="android.permission.INTERNET" />
to the manifest file!
I'm back with another problem!
I'm trying to create an app that would list a selected Livestreams, from Own3d.TV, Justin.Tv etc...
If my research isn't totally failed, I can use the MediaPlayer object to Stream video, the only question is how do I use it?
So far my code looks like this, but it's giving me an Exception when trying to prepare the MediaPlayer.
public class Media extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SurfaceView sw = new SurfaceView(this);
SurfaceHolder sh = sw.getHolder();
setContentView(sw);
Uri ur = Uri.parse("http://www.twitch.tv/widgets/live_embed_player.swf?channel=hashe");
MediaPlayer mp = new MediaPlayer();
mp.setDisplay(sh);
try {
mp.setDataSource("http://www.twitch.tv/widgets/live_embed_player.swf?channel=hashe");
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
//mp.setDisplay(sw);
}
}
Is it even possible to Stream the video from these sites using the MediaPlayer?
If not, how shoud I approach this problem?
Thanks!
when implementing mediaPlayer, try something like this, it worked fine for me: http://android-er.blogspot.com/2010/11/play-3gp-video-file-using-mediaplayer.html
but I think, u cant use source like u have written in your code. You have to use some URL from where is video streamed directly with progresive streaming, not URL of html site with player embeded. You can recognize: when u write useful URL with streamed video in your internet browser, browser starts to download this streamed video in your computer. And I think, MediaPlayer supports in core .3gp or .mp4 format only..
I hope u understand my bad english
Use ExoPlayer instead of MediaPlayer. See Android official documentation:
ExoPlayer supports features like Dynamic adaptive streaming over HTTP (DASH), SmoothStreaming and Common Encryption, which are not supported by MediaPlayer. It's designed to be easy to customize and extend.
ExoPlayer - Android Developers
I am working on android application in which i have play online radio streaming.
i have gone through the media player classes but i don't think is there any method to online streaming of radio. If any know about this please help me.
Thank You.
Vikram
Vikram,
You should be able to achieve this using the MediaPlayer; however, depending on your format it may be difficult. For example, if you're trying to play an online radio stream that uses .pls, or .m3u, you would have to parse that file, and pull out the true URLs to use.
Beyond that, you should be able to use MediaPlayer's create method with a URL to start streaming playback. Keep in mind that if the streams URL redirects (which it likely does) you may have to resolve the URL. A simple way to do this is use HttpURLConnection to open a connection, then connect(), then getURL(). You'll likely need a string url, so call toExternalForm() on the result from getURL().
Additionally, If things aren't working for you with MediaPlayer via URL, you might have to come up with your own buffering mechanism to get the data from the server. That being the case, you can try this tutorial: http://blog.pocketjourney.com/2008/04/04/tutorial-custom-media-streaming-for-androids-mediaplayer/
From what I've read, you should just be able to do:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(streamingURL);
mediaPlayer.prepare();
mediaPlayer.start();
to get basic functionality I believe, but I haven't tested it myself.
the easiest way to play a radio channel in android is to use the built in MediaPlayer, however when the datasource is from web the prepare() method takes a long time to execute and you should use prepareAsync() instead to avoid blocking the ui:
player = new MediaPlayer();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
player.start();
}
}
});
try {
player.setDataSource(currentChannelUrl);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
return;
}
player.prepareAsync();