I can't play some audio got from firebase storage - android

I'm trying to get audios from firebase storage and play with MediaPlayer. And actually I can do this but the problem is I can't play some of the audios. I realized that it can't play audios which have size higher than 50 kb. I can play audios which the size less than 50 kb.
Btw this is my first question here. Sorry for mistakes.
Errors:
E/FLACExtractor: unsupported sample rate 44000
E/GenericSource: initFromDataSource, source has no track!
E/GenericSource: Failed to init from data source!
E/MediaPlayerNative: error (1, -2147483648)
E/MediaPlayer: Error (1,-2147483648)
public class KelimeSecimActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener {
MediaPlayer mMediaplayer = new MediaPlayer();
private Button buttonBasla ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kelime_secim);
buttonBasla = findViewById(R.id.buttonBasla);
mMediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
fetchAudioUrlFromFirebase();
buttonBasla.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onPrepared(mMediaplayer);
}
});
}
private void fetchAudioUrlFromFirebase() {
final FirebaseStorage storage = FirebaseStorage.getInstance();
// Create a storage reference from our app
StorageReference storageRef = storage.getReferenceFromUrl("MY_URL");
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
try {
// Download url of file
final String url = uri.toString();
mMediaplayer.setDataSource(url);
// wait for media player to get prepare
mMediaplayer.setOnPreparedListener(null);
mMediaplayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getApplicationContext(),"Failed",Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
As i said, there is no problem if the audio size low than 50 kb. Actually i'm not sure whether it's because of size. But I tried many files and result is like this.

I am able to reproduce the issue. The files you mentioned do work in other players, but don't work in Android's MediaPlayer.
The problem files are all encoded at 44000Hz, using libFLAC 1.1.3.
The files that work are all 44100Hz, using libFLAC 1.1.2.
The size correlation appears to be a coincidence. For instance, En-us-you_re2.flac plays fine. It's 166KB, but it's 44100Hz.
It's clear that 44000Hz is a valid rate, but MediaPlayer simply doesn't support it. You may have better luck with exoplayer with the FLAC extension. Worst case, you might have to find your own decompression library and do the conversion yourself.

Related

How to play Vimeo video using Vimeo networking library?

I am using Vimeo for video hosting. But I don't know how to play video in my Android app. I have got the Vimeo-networking library here 'https://github.com/vimeo/vimeo-networking-java' but unable to play video. I am confused which code exacly used for playing video. Please help me. I am new to Vimeo.
String uri = what url?;
VimeoClient.getInstance().fetchNetworkContent(uri, new ModelCallback<Video>(Video.class) {
#Override
public void success(Video video) {
//what to do now?
}
#Override
public void failure(VimeoError error) {
// voice the error
}
});

streaming a mp3 from a site/server

hi guys me and a few other djs want to be able to upload our mixes to a site that could then be streamed direct in to my app. at the moment i have the files stored on google drive. they will download and then i can play via a media player. but i want the user to be able to just stream the music direct from the site..
1) what site would you suggest for uploading of the music. i have a firebase account would this be an idea or would the data from the files i upload be expensive? im on the pay as you go account type.
****EDIT
So ive found out that firebase could be a n actual viable option for doing this
2) what would be the best way to implement the code so that it streams the music direct rather than downloading it first?
****EDIT
ive found some code while trawling the internet that could make this viable but i seam to be getting a null pointer error on the setDatasource
this is my code
i have this my onchild added
final String url = dataSnapshot.child("url").getValue(String.class);
urlList.add(url);
then in my onitemclick i use
mp3url = urlList.get(i);
fetchAudioUrlFromFirebase();
finally the method for streaming the mp3
private void fetchAudioUrlFromFirebase() {
String mp3 = mp3url;
final FirebaseStorage storage = FirebaseStorage.getInstance();
storageRef = storage.getReferenceFromUrl(mp3);
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
try {
// Download url of file
final String url = uri.toString();
mMediaplayer.setDataSource(url);
// wait for media player to get prepare
mMediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mMediaplayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.i("TAG", e.getMessage());
}
});
}
this is my full logcat with the null pointer error
07-11 18:39:45.326 2565-2565/com.example.harrops.h20droidapp2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.harrops.h20droidapp2, PID: 2565
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setDataSource(java.lang.String)' on a null object reference
at com.example.harrops.h20droidapp2.Mixes$3.onSuccess(Mixes.java:198)
at com.example.harrops.h20droidapp2.Mixes$3.onSuccess(Mixes.java:192)
at com.google.android.gms.tasks.zze$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Following line means: mMediaPlayer is null.
java.lang.NullPointerException: Attempt to invoke virtual method
'void android.media.MediaPlayer.setDataSource(java.lang.String)'
on a null object reference
Please check mMediaplayer is initialized before onSuccess(Uri uri) method is called.
mMediaplayer = new MediaPlayer();
Q1: i have no idea about a site do this, sorry
Q2: you have to learn how to use MediaPlayer class in android and here is an example :
String link = "your link.mp3"; //usually it's ends with .mp3
Mediaplayer player = new MediaPlayer();
player.setDataSource(link);
player.prepare();//prepare to play
player.start();
Its a quick and very simple example.
You can edit this as your need.

Android MediaPlayer prepare failed: status = 0x1 and Mediaplayer.creat() returns null with .AAC audio file

I am accessing the audio files from
/data/user/0/app_packagename/files/filename
I don't have WRITE_EXTERNAL READ_EXTERNAL permissions.
Now I can access files which are saved in the /data/user/0/app_packagename/files/ without WRITE_EXTERNAL READ_EXTERNAL permissions.
And can able to play all the audio files in my application.
In my application I am playing all files in queue. Few of my users are not able to play second audio file once 1st audio file completed.
Below is my code
void playNext(File varientFile) {
if (mSessionMediaPlayer != null) {
if (mSessionMediaPlayer.isPlaying()) {
mSessionMediaPlayer.stop();
}
mSessionMediaPlayer.release();
mSessionMediaPlayer = null;
}
mSessionMediaPlayer = new MediaPlayer();
if (mSessionMediaPlayer != null) {
Uri uri = Uri.parse(variantFile.getAbsolutePath());
mSessionMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mSessionMediaPlayer.setDataSource(this, uri);
mSessionMediaPlayer.setLooping(false);
mSessionMediaPlayer.prepare();
mSessionMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
playNext(mFiles.get(mNextIndex);
}
});
mSessionMediaPlayer.start();
runScheduler();
}
}
Instead of
mSessionMediaPlayer = new MediaPlayer();
I had tried with
mSessionMediaPlayer = MediaPlayer.create(getApplicationContext(),
uri);
Even MediaPlayer.create() is returning null. mSessionMediaPlayer.prepare() is throwing an exception
Cause : Prepare failed.: status=0x1
Whats wrong with my code? Why it is getting null or prepare failed? I had tested the files as well they are working, audio file format is in .aac.
The same user who is not able to play second audio file. He is able to play first file with the same code. But if same method is calling from onCompleted() it is not working. Why so?
Can anyone please help me to fix this issue?
Instead of creating a new object every time, try with the same Media Player object something like this.
MediaPlayer mSessionMediaPlayer;
mSessionMediaPlayer = MediaPlayer.create(getApplicationContext(),uri);
mSessionMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mSessionMediaPlayer) {
moveToNext(mSessionMediaPlayer,nextUri);
}
});
mSessionMediaPlayer.prepare();
mSessionMediaPlayer.start();
private void moveToNext(MediaPlayer mSessionMediaPlayer,Uri uri){
mSessionMediaPlayer.create(getApplicationContext(), uri);
mSessionMediaPlayer.prepare();
mSessionMediaPlayer.start();
}

Load Video from Firebase Storage to videoView

I want to play a video from Firebase storage with a videoview I'm trying like this:
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
//Toast.makeText(Chat.this, uri.toString(), Toast.LENGTH_SHORT).show();
MediaController mc = new MediaController(Chat.this);
videoView.setMediaController(mc);
videoView.setVideoURI(uri);
videoView.requestFocus();
//videoView.start();
}
});
But it wont play. The video is empty.
First of all, be certain that the video in Storage is actually in a format that Android devices can play. VideoView cannot play everything - only the types of videos that Android supports.
What you're doing now by passing the uri directly into the VideoView is interpreted as an attempt to stream the video from the given uri. Firebase Storage doesn't support video streaming, so that's won't work. Streaming from a uri requires that the server on the other end be able to stream the given resource.
If want to play a video from Storage, you'll have to download entirety first, saved to a local file, then point the VideoView at the local file for playback.
I think the time is late for the answer, but this answer to those who are looking for it later:
Via RecyclerView View dial Uri from the data Snapshot in adapter
#Override
protected void onStart() {
super.onStart();
final FirebaseRecyclerAdap...///
) {
#Override
protected void popul...////
/////
viewHolder.setVideo(Uri.parse(String.valueOf(videoUri)),model.getVideo());
// Here the position is sent to videoview adapter
final String post_key = getRef( position ).getKey().toString();
final String post_id = String.valueOf(post_key);
viewHolder.setUid(model.getUid());
// Here the position is sent to videoview adapter
viewHolder.setVideo(post_id);
viewHolder.setUserprofile(getApplication(),model.getUserprofile());
}
yourAdpater ....
}
public static class ViewHolder extends RecyclerView.ViewHolder {
View mView;
Videoview post_video ;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
post_video = (VideoView ) mView.findViewById(R.id.videoView);
}
public void setVideo(final String post_id) {
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//
Uri uri=Uri.parse(dataSnapshot.child(post_id).child("video").getValue(true).toString());
//You can set up the video display as you like
post_video.setVideoURI(uri);
post_video.requestFocus();
post_video.start();
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
The databases should look like this:
look here.
good luck.
may be possible this by storing videos URLs in Database, then present them in Recyclerview which consists of Webview/Youtube Integration in Single row
I had the same problem and the following solution worked for me:
Add this string to AndroidManifest.xml file
<uses-permission android:name="android.permission.INTERNET" />

Play Mp4 video from server android

I want to play video of mp4 format and size 4-5Mb from server in streaming mode.I am using sdk version 2.3,on emulator it
gives only sound but not any picture.
I also tested it on devices Samsung(android sdk ver 2.1) and LG optimus(android sdk ver 2.2)
and only get "cannot play video:sorry this video is not valid for streaming to this device" message.
I have searched on this but not getting any solution, if anybody have any solution please help me.Thanks in advance.
Here is my code:
public class ShowVideo extends Activity
{
private static ProgressDialog progressDialog;
public String video_url;
private MediaController mediaController;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.videoalbum);
progressDialog = ProgressDialog.show(ShowVideo.this, "", "Buffering video...", true);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
video_url = "http://www.letumobi.com/videouploads/cd0a4170-1fb2-4fba-b17c-b5d70b2cd2e7.mp4";
try {
final VideoView videoView =(VideoView)findViewById(R.id.video_viewId);
mediaController = new MediaController(ShowVideo.this);
mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
Uri video = Uri.parse(video_url);
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
videoView.start();
}
});
}catch(Exception e){
progressDialog.dismiss();
System.out.println("Video Play Error :"+e.getMessage());
}
}
You may try these urls (ends with .3gp):
http://daily3gp.com/vids/747.3gp
http://daily3gp/www/vids/juggling_while_on_unicycle.3gp
instead of .mp4 urls:
video_url = "http://www.letumobi.com/videouploads/cd0a4170-1fb2-4fba-b17c-b5d70b2cd2e7.mp4";
On emulator it is difficult to get video played, because it needs really fast computer. Try this link for emulator. Maybe you can get some discrete video view.
http://commonsware.com/misc/test2.3gp
Also in your real devices this link should work if your implementation is proper.
I assume your video using following link
"http://www.letumobi.com/videouploads/cd0a4170-1fb2-4fba-b17c-b5d70b2cd2e7.mp4
is not proper for safe streaming.
You should hint that video or play other hinted videos. I couldn't find any other solution for playing unhinted videos yet.
This link may help.
getting PVMFErrContentInvalidForProgressivePlayback error while playing mp4 files on samsung devices

Categories

Resources