How to play separate audio and video together using exoplayer? - java

So I have two urls one for audio and one for video. I want to play them together but really couldn't find any documentation about this.

I just found the answer just build it like below in kotlin:
val dataSourceFactory: DataSource.Factory =
DefaultHttpDataSource.Factory()
val videoSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(fromUri(videoInPlayer.videoStreams[0].url))
val audioSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(fromUri(videoInPlayer.audioStreams[0].url))
val mergeSource: MediaSource = MergingMediaSource(videoSource,audioSource)

Related

How to share a recorded video programmatically?

Well guys, I'm new at programming, I've tried some ways, but I was not able to achieve it.
I have these two paths, how can I open the share options to send this file?
I/ExternalStorage: Scanned /storage/emulated/0/Movies/HD2022-04-23-22-37-44.mp4:
I/ExternalStorage: -> uri=content://media/external_primary/video/media/102870
I did it with text, but I was not able to do it with the video, it's something like this?
binding.btShare.setOnClickListener {
ShareCompat.IntentBuilder(this)
.setType("text/plain")
.setChooserTitle(R.string.shareFriends)
.setText(getString(R.string.shareMessage)+" https://play.google.com/store/apps/details?id=" + this.getPackageName())
.startChooser();
}
No more need. thx...
Already did.
I'll put it here, maybe it can help someone.
fun shareVideo(filePath:String) {
val videoFile = File(filePath)
val videoURI = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
FileProvider.getUriForFile(this,BuildConfig.APPLICATION_ID + ".fileprovider", videoFile)
else
Uri.fromFile(videoFile)
ShareCompat.IntentBuilder.from(this)
.setStream(videoURI)
.setType("video/mp4")
.setChooserTitle("Share video...")
.startChooser()
}

How to make exoplayer's ClippingMediaSource more precise?

I try to play short piece of mp3 audio (44100 samples rate) with exoPlayer using it's ClippingMediaSource but it cuts the start of the piece for about 250 milliseconds, it's too much, is it possible to make it more precise?
I start exoplayer like this:
dataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, "com.example.player"));
Uri uri = Uri.fromFile(new File(path));
MediaSource audioSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
ExoPlayer exoPlayer = ExoPlayerFactory.newSimpleInstance(context, new DefaultTrackSelector());
exoPlayer.setPlayWhenReady(true);
ClippingMediaSource clip = new ClippingMediaSource(audioSource, 7_225_000, 8_175_000);
exoPlayer.prepare(clip);
According to the exoplayer documentation, there are the two methods
ClippingMediaSource(MediaSource MediaSource, long startPosition, long endPositionUs)
Creates a new clipping source that wraps the specified source and provides samples between the specified start and end position.
ClippingMediaSource(MediaSource mediaSource, long startPositionUs, long endPositionUs, boolean enableInitialDiscontinuity)
try the second method with false or adjust startPosition and endPosition.
For more check, this clipping documentation of ExoPlayer. Hope this will solve your issue.

Is it possible to do Uri.parse(Arraylist<POJO>)

Here is a piece of code. I am pretty much trying to do Uri.parse(Arraylist) instead of string values
ArrayList<StepsModel> videoURL = new ArrayList<>();
Uri uri = Uri.parse(videoURL);
MediaSource mediaSource = buildMediaSource(uri);

MediaPlayer android stream radio starts not immediately or does not start at all

I use this code:
mediaPlayer.setDataSource("http://some online radio");
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.prepareAsync();
and onPrepared method is:
if (mediaPlayer != null) {
mediaPlayer.start();
}
In general, the problem is: then i run this code, playback does not start right away, but about 10 seconds later. + some streams do not start at all, on the emulator it works a little better, than on the device, but still. It depends on concrete radio, some are better, other very bad.
I assume that the matter is in the preparation and buffering. It can be possible to make an InputStream from this stream and write to some temporary file / buffer, and read/play this file in the MediaPlayer, but how to implement it is, not yet clear .. Help please
If you just do mp.prepare, and then mp.start - the result is the same
On a PC in Chrome, all the radio streams that I tried to use immediately start playing
Sorry for my english, thank you.
If somebody get here for same reason (just in case), solution is ExoPlayer
Something like this:
LoadControl loadControl = new DefaultLoadControl();
bandwidthMeter = new DefaultBandwidthMeter();
extractorsFactory = new DefaultExtractorsFactory();
trackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(trackSelectionFactory);
defaultBandwidthMeter = new DefaultBandwidthMeter();
dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "mediaPlayerSample"), defaultBandwidthMeter);
mediaSource = new ExtractorMediaSource(Uri.parse(radioURL), dataSourceFactory, extractorsFactory, null, null);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
player.prepare(mediaSource);
player.setPlayWhenReady(true);
But u better check actual version of examples here: https://github.com/google/ExoPlayer
It seems like audio bitrate might be the key here: https://stackoverflow.com/a/12850965/3454741. There is no easy way to get around this, it seems.
If the streaming does not start at all it might be due to some error, which you could catch using MediaPlayer.setOnErrorListener().

How to add a share intent with robovm (libgdx)?

So for android devices there is a default share intent function that you call that will list all the apps that is downloaded on the device with that allows sharing of content. How would I do this using robovm, here is a Screen shot of what I am trying to achieve. Also on a side note, what I want to ultimately do is take a screenshot of their device and post it to whatever social media site the user chooses. Any help would be greatly appreciated :D
For iOS users:
RoboVM is deprecated in favor of RoboPods. So, RoboVM is no more, what now?
https://github.com/robovm/robovm-robopods/tree/master
For sharing text using roboVM, you can set the UIAvtivityViewController class
NSString textShare = new NSString("This is the text to share");
NSArray texttoshare = new NSArray(textShare);
UIActivityViewController share = new UIActivityViewController(texttoshare,null);
presentViewController(share, true, null);
For sharing text, image and app, you can do the following code,
NSURL imgUrl = new NSURL(EXTERNAL_IMG_URL);
NSData data = NSData.read(imgUrl);
UIImage image = new UIImage(data);
NSString appStoreUrl = new NSString(APP_STORE_URL);
NSString googleUrl = new NSString(GOOGLE_PLAY_URL);
NSString text = new NSString(TEXT_TO_SHARE);
NSArray<NSObject> texttoshare = new NSArray<NSObject>(text, image, appStoreUrl, googleUrl);
UIActivityViewController share = new UIActivityViewController(
texttoshare, null);
if (UIDevice.getCurrentDevice().getUserInterfaceIdiom() == UIUserInterfaceIdiom.Phone) {
//iPhone
iosApplication.getUIViewController().presentViewController(
share, true, null);
} else {
//iPad
UIPopoverController popover = new UIPopoverController(share);
UIView view = iosApplication.getUIViewController().getView();
CGRect rect = new CGRect(
view.getFrame().getWidth() / 2,
view.getFrame().getHeight() / 4,
0,
0);
popover.presentFromRectInView( rect, view, UIPopoverArrowDirection.Any, true);
}
The following code sends an animated .gif through Mail and Messenger, however, Twitter only shares a static image.
public void shareGif(String path)
{
NSURL url = new NSURL(new File(path));
NSArray<NSObject> imageArray = new NSArray<NSObject>(image);
UIActivityViewController share = new UIActivityViewController(imageArray,null);
((IOSApplication)Gdx.app).getUIViewController().presentViewController(share, true, null);
}
For generic share in iOS, with text, link and image the code is given below:
#Override
public void share() {
NSArray<NSObject> items = new NSArray<>(
new NSString("Hey! Check out this mad search engine I use"),
new NSURL("https://www.google.com"),
new UIImage(Gdx.files.external("image.png").file())
);
UIActivityViewController uiActivityViewController = new UIActivityViewController(items, null);
((IOSApplication) Gdx.app).getUIViewController().presentViewController(uiActivityViewController, true, null);
}
For Android users: Sharing Content with intents
It says RoboVM is for iOS only, So how would we use it for android using LibGDX?
roboVM with RoboPods is used for iOS while google play services is used for android. Different devices, different code. For android, after integrating the android sdk to your IDE, just link the google play services lib to the android project.

Categories

Resources