I'm trying to play Vimeo video using ExoPlayer and here is the code:
val dataSourceFactory = DefaultDataSourceFactory(requireContext(),
getUserAgent(requireContext(), requireContext().getString(R.string.app_name)))
val videoSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(videoLink))
player.prepare(videoSource)
This throw the following exception:
com.google.android.exoplayer2.upstream.HttpDataSource$InvalidResponseCodeException: Response code: 403
at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:300)
at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:177)
at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83)
at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:961)
at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:391)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Also I tried to extract the Vimeo url using AndroidVimeoExtractor and it fails with the following exception:
java.io.IOException: Video has restricted playback
Here is the code:
VimeoExtractor.getInstance().fetchVideoWithIdentifier(
"videoIdentifier",
null,
object : OnVimeoExtractionListener {
override fun onSuccess(video: VimeoVideo) {
Logger.i("video: ${video.streams.size}")
}
override fun onFailure(throwable: Throwable) {
Logger.e(throwable)
}
})
And here is a sample of Vimeo video link: http://player.vimeo.com/external/videoIdentifier.sd.mp4?s=value&profile_id=value&oauth2_token_id=value
Note: I'm not the person who uploaded the video. Also, when I put the link in Google Chrome Browser it redirects to another link and plays video normally. So I need something like that to get the final link to pass it to ExoPlayer.
There is 2 ways to handle this issue
First solution
Using Vimeo networking API to get video URI and to use it
create app on vimeo developer
using your app access token and video Id you can get Video Url then pass it to Exo player
Second Solution (it's a work around and fast solution)
using web view to redirect to .mp4 url, then use the redirected url ends with.mp4 to open it with Exo player
webView = WebView(context).apply {
settings.javaScriptEnabled = false
webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
val requestUrl = request?.url.toString()
val uri = requestUrl.toUri()
if (isVideoLink(requestUrl)) {
preparePlayer(uri)
webView?.release()
webView = null
return true
}
return false
}
}
loadUrl(url)
}
fun isVideoLink(url: String): Boolean {
return getMimeType(url)?.contains("video", true) ?: false
}
Are you trying to play all Vimeo Videos, or just Vimeo videos you've uploaded?
I think the issue is the mp4 file you're pulling isn't actually valid or you don't have permissions to stream it given your oauth token (if you don't own it).
You can check out the vimeo-networking library README here.
The basic requirements for native playback are:
User must be logged in.
User must be the owner of the video.
User must be PRO or higher (or the app must have the "can access owner's video files" capability).
Token must have the video_files scope.
User must be the owner of the API app making the request.
If you satisfy all of those requirements, then you can make an API request to Vimeo and they'll give you back a valid MP4 file you can pass as the videoLink.
// Obtain a video you own my making a GET request
Video video = ...;
Play play = video.getPlay();
// This will be a list of "progressive" video files (basically just MP4s of different resolutions that you can pass to ExoPlayer
ArrayList<VideoFile> progressiveFiles = play.getProgressiveVideoFiles();
String linkToMp4File = progressiveFiles.get(0).getLink();
val videoSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(linkToMp4File))
If you weren't the person who uploaded the video, I think there's a way to get a valid video link, but I've never done it personally. Since all users are allowed to embed any video (if the video is public and embeddable), then you could get the embed link from the API here and then extract the MP4 file link from the embed code maybe? Just an idea.
Related
Background
I'm using Google Play Install Referrer Library in my native Android app to track some UTM parameters. To test my implementation, I use Google Play URL Builder to generate test URLs for my app (which is on Internal Testing by the way).
Problem
On the ReferrerDetails object, getting installReferrer returns an incomplete URL i.e. a URL with only the query parameters and nothing else e.g. utm_source=google&utm_medium=cpc&utm_term=myUtmTerm&utm_content=myUtmContent&utm_campaign=myCampaignName&anid=admob. I cannot really call Uri.parse() to convert this into a URI and get the query parameters directly (the query parameters are null if I try to do that since it's not a valid URI). My other option is to use a custom parser built just for this that I'm trying to avoid for my convenience. Is there another way around it?
Here is the code block that I am using to get this UTM data.
//other code
...
referrerClient = InstallReferrerClient.newBuilder(context).build()
referrerClient.startConnection(object : InstallReferrerStateListener {
override fun onInstallReferrerSetupFinished(responseCode: Int) {
when (responseCode) {
InstallReferrerClient.InstallReferrerResponse.OK -> {
val response: ReferrerDetails = referrerClient.installReferrer
val referrerUrl: String = response.installReferrer
referrerClient.endConnection()
}
...
}
}
I am trying to integrate youtube into my Android app. I have the following code to queue the video
fun setUpYoutubePlayer()
{
val youtubeFragment = fragmentManager.findFragmentById(R.id.songYoutubeFragment)
as YouTubePlayerFragment
youtubeFragment.initialize(getString(R.string.youtube_api_key),
object : YouTubePlayer.OnInitializedListener {
override fun onInitializationSuccess(provider: YouTubePlayer.Provider,
youTubePlayer: YouTubePlayer, b: Boolean) {
// do any work here to cue video, play video, etc.
youTubePlayer.cueVideo(intent.getStringExtra(getString(R.string.song_youtube_id)))
}
override fun onInitializationFailure(provider: YouTubePlayer.Provider,
youTubeInitializationResult: YouTubeInitializationResult) {
}
})
}
Now sometimes I won't have the video id. But I will always have the name of the song and the album name. How can I queue the video with the name and the album name of the song.
Here we will teach you how to play a YouTube video in an Android App. So here YouTube will be embedded in Android App itself. This we will do through the Youtube Player API developed by Google. You can read more about it here. A major application of android youtube player is that now you can directly upload video to YouTube and stream it in your App. You can see a demo of this youtube android player API example below:
https://www.androidtutorialonline.com/android-youtube-player-api/
I am developing vimeo video app in native android. But it is not supported in VideoView. May I know any samples or related query for Android. I want final output to be in .mp3/.mp4 format.
I have tried iframe in Android WebView, It works well in Android WebView but I am not able to get seek bar. And OnPause() not able to Pause the video.
Here I am able to get Pause and Play button Only
Example: player.vimeo.com/video/49462103
I want play this video in android native
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/videoView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
Update link in :
Vimeo site Thread-1
Vimeo site Thread-2
I am getting above error
I made a native player for vimeo, base by WebView. Support public and private video.
Try it : https://github.com/ct7ct7ct7/Android-VimeoPlayer
<com.ct7ct7ct7.androidvimeoplayer.view.VimeoPlayerView
android:id="#+id/vimeoPlayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
VimeoPlayerView vimeoPlayer = findViewById(R.id.vimeoPlayer);
getLifecycle().addObserver(vimeoPlayer);
//public video
vimeoPlayer.initialize(59777392);
//If video is open. but limit playing at embedded.
vimeoPlayer.initialize({YourPrivateVideoId}, "SettingsEmbeddedUrl")
//If video is pirvate.
vimeoPlayer.initialize({YourPrivateVideoId},"VideoHashKey", "SettingsEmbeddedUrl")
Vimeo's embed codes should work inside an Android WebView.
Vimeo only offers .mp4 links to PRO users on those users own videos.
Another option is to use the official Deep Link library for the android application. This will let you open any vimeo video in the Android app.
You can use Exoplayer to play vimeo Videos. Exoplayer is more customizable. All you need is to extract the url link from the video config link. You may use retrofit to extract the video url.
BASE_URL = "https://player.vimeo.com/video/"
You will need to use a get method like below:
#GET("{id}/{config}")
Call<JsonObject>getVideoLink(#Path("id") String id, #Path("config") String config);
You will get the id from video link. Example: "https://vimeo.com/123456789/" Here the id is: 123456789 .
JsonObject jsonObject = response.body();
JsonObject req = jsonObject.getAsJsonObject("request");
JsonObject file = req.getAsJsonObject("files");
JsonArray arr = file.getAsJsonArray("progressive");
String url = arr.get(0).getAsJsonObject().get("url").getAsString();
// Build the media item.
MediaItem mediaItem = MediaItem.fromUri(url);
// Set the media item to be played.
player.setMediaItem(mediaItem);
// Prepare the player.
player.prepare();
// Start the playback.
player.play();
Don't forget to initiate Exoplayer first.
I'm using the "TheMovieDB" API to get information about movies, and it is possible to retrieve video information for a certain movie, like this:
videos: {
results: [
{
id: "533ec6a5c3a3685448005327",
iso_639_1: "en",
key: "ac7KhViaVqc",
name: "First Trailer",
site: "YouTube",
size: 720,
type: "Trailer"
}
]
}
As you can see, it is possible to build the youtube video url with the key provided in the JSON.
When building the MediaInfo object, you can pass the video URL, like this:
MediaInfo.Builder(MOVIE_URL)
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setContentType("video/mp4")
.setMetadata(movieMetadata)
.build();
However, if MOVIE_URL is the youtube url, I can't cast the content.
Is it possible to cast youtube videos from my app? If yes, how can I do it?
Thanks!
There are currently no APIs in the cast SDK to accomplish that. People have had different degrees of luck with using the embedded youtube iframe approach, but that is not a perfect solution for various reasons; for example you cannot skip ads, etc.
I can stream some videos from URL (server) successfully,
But the others (my video up to my server) is not.
You can see both in my code (Working/Not working)
I'm on stuck with this problem.
"Sorry, this video is not valid for streaming to this device"
The following code :
String url = null;
// url = "rtsp://v5.cache5.c.youtube.com/CiILENy73wIaGQmC00ZlwwIDOxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"; // Working
// url = "http://daily3gp.com/vids/747.3gp"; // Working
url = "http://www.ooklnet.com/files/381/381489/video.mp4"; // Working
// url = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov"; // Working
// url = "http://dl.dropbox.com/u/80419/santa.mp4"; // Working
// url = "http://trekvn.hostoi.com/encode_normal.mp4"; // Not Working
// url = "http://trekvn.hostoi.com/output_encode_mp4_box.mp4"; // Not Working
// url = "http://www.ourserversite.com:14556/ingCloud/users/400010001/mp4_320p/e8d8535852a98c949640c5d618ac23d320131022174507.mp4"; // Not Working
// url = "http://trekvn.hostoi.com/mpeg_2_ts.ts"; // Not Working
videoviewer.setVideoURI(Uri.parse(url));
videoviewer.requestFocus();
videoviewer.setKeepScreenOn(true);
videoviewer.setOnErrorListener(this);
videoviewer.setOnPreparedListener(this);
#Override
public void onPrepared(MediaPlayer mp) {
videoviewer.start()'
}
All of these video can play good on the site,
have format *.mp4 (mpeg-4 part 10), Video : .H264, Audio : aac.
Please tell me why,
and how to fix it.
p/s : Samsung Galaxy Tab 7" - 2.2.1
Thanks,
This is the issue that many people met, ensure you understand following content to fix the issue.
Through my experience and the answer in this Sorry, this video cannot be played - streaming mp4 to android
Finally, I found the reason and the solution:
- Reason : Almost the *.mp4 file was produced for developing focus on iOS, and have the encoding : H264 Main. While Android side can only stream & play the files have the encoding : H264 Baseline.
- Solution : We need convert from the encoding H264 Main to H264 Baseline (guaranteed enabled Web Optimized (move -moov atom before -mdat) feature)
- Bugs :
Sorry this video is not valid for streaming to this device.
Sorry, this video can not be played.
HandBrake tool is the easiest and cheapest way to complete it.
Thanks,
p/s : But if you use this method, you need upload your video to the server for testing.