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

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);

Related

How to play separate audio and video together using exoplayer?

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)

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.

How to pass an arrayList between two intent in Android

I am new in Android and I have a question regarding intent. I want to pass an String ArrayList between two activities by using intent. I have tried this:
`
ArrayList<String> playerName = new ArrayList<>();
Intent intent = new Intent(this,Game.class);
intent.putStringArrayListExtra("Player",playerName);
startActivity(intent);`
Then I tried to receive my intent like that:
`
ArrayList<String> playerNameList= new ArrayList<>();
playerNameList = getIntent().getStringArrayListExtra("Player");
int listSize = playerNameList.size();
StringBuilder str = new StringBuilder(" ");
str.append(listSize);
textView.setText(str);
StringBuilder str1 = new StringBuilder(" ");
str1.append(playerNameList.get(2));
textView2.setText(str1);`
I can get the correct listSize; however I am not able to get any element of my arrayList. I always get the "index(0)" element. I would be really appreciated to any advice and help
you can easily to by using Gson
Intent intent = new Intent(this,Game.class);
intent.putExtra("Player",new Gson().toJson(playerName));
startActivity(intent);
at Game.class
ArrayList<String> playerNameList = playerNameList = new ArrayList<>();
String str = getIntent().getStringExtra("Player");
playerNameList = new Gson().fromJson(str,new TypeToken<ArrayList< String >>(){}.getType());
Check your code to add data to ArrayList playerName. The code to putStringArrayListExtra to Intent and getStringArrayListExtra from Intent is correct.

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().

Most robust way to fetch album art in Android

Please note that I have already been through similar questions and their answers here and on other websites. I also have a solution that works on some devices (my G2X running CyanogenMod 7.1, my wife's HD2 running a custom ROM and the emulator running Android 2.1). It doesn't, however work on my Nook running CyanogenMod.
My question is: What is the most robust and general way to fetch album art on all android devices? What are the gotchas for specific devices, versions or music applications (I don't mean third-party players, I mean Google Music versus the old Music client)? My current code is:
// Is this what's making my code fail on other devices?
public final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
// This works, and well on all devices
private int[] getAlbumIds(ContentResolver contentResolver)
{
List<Integer> result = new ArrayList<Integer>();
Cursor cursor = contentResolver.query(MediaStore.Audio.Media.getContentUri("external"), new String[]{MediaStore.Audio.Media.ALBUM_ID}, null, null, null);
if (cursor.moveToFirst())
{
do{
int albumId = cursor.getInt(0);
if (!result.contains(albumId))
result.add(albumId);
} while (cursor.moveToNext());
}
int[] resultArray = new int[result.size()];
for (int i = 0; i < result.size(); i++)
resultArray[i] = result.get(i);
return resultArray;
}
// This is the bit I want to make more robust, make sure that it works on all devices
private Shader getAlbumArt(ContentResolver contentResolver, int albumId, int width, int height)
{
Uri uri = ContentUris.withAppendedId(sArtworkUri, albumId);
InputStream input = null;
try {
input = contentResolver.openInputStream(uri);
if (input == null)
return null;
Bitmap artwork = BitmapFactory.decodeStream(input);
input.close();
if (artwork == null)
return null;
Bitmap scaled = Bitmap.createScaledBitmap(artwork, width, height, true);
if (scaled == null)
return null;
if (scaled != artwork)
artwork.recycle();
artwork = scaled;
return new BitmapShader(artwork, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Thanks in advance,
Ananth
Here i can attach one function that is return album art from media store . Here in function we just have to pass the album_id which we get from Media store .
public Bitmap getAlbumart(Long album_id)
{
Bitmap bm = null;
try
{
final Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ParcelFileDescriptor pfd = context.getContentResolver()
.openFileDescriptor(uri, "r");
if (pfd != null)
{
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
}
} catch (Exception e) {
}
return bm;
}
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ContentResolver res = context.getContentResolver();
InputStream in = res.openInputStream(uri);
Bitmap artwork = BitmapFactory.decodeStream(in);
More complete sample code can be found in Android Music player source here https://github.com/android/platform_packages_apps_music/blob/master/src/com/android/music/MusicUtils.java method getArtworkQuick.
This below code snippet returns the uri for the album art cache present in the MediaStore. may be this will help.
Cursor cursorAudio = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, {MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.DATA}, MediaStore.Audio.Media.DATA+ " LIKE \"" + path+ "\"", null, null);if(cursorAudio != null && cursorAudio.moveToFirst()){
Long albumId = Long.valueOf(cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
cursorAudio.close();
Cursor cursorAlbum = managedQuery(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART}, MediaStore.Audio.Albums._ID+ "=" + albumId, null, null);
if(cursorAlbum != null && cursorAlbum.moveToFirst()){
String uri = cursorAlbum.getString(cursorAlbum.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
cursorAlbum.close();
if(uri != null){
return Uri.parse(uri);
}
}
I'd like to modify Chirag Raval's answer by using Picasso library. It is simple to use and very powerful.
final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, arrayList.get(i).getArt());
Picasso.with(context).load(uri).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher)
.into(ivPic);
For full documentation, refer this.

Categories

Resources