I am making a media player where I am showing videos from the remote server(e.g. youtube). I have shown a list of videos but I am unable to get the length of videos.
(I have searched on this site but I got answers regarding how to get time duration of the video which is locally stored.
I am giving the link to one of such question.
How to get length in milliseconds of video from URL without video view in Android?)
MediaPlayer mp= new MediaPlayer();
mp.setDataSource(context,uri);
int durartion = mp.getDuration();
Related
This question already has answers here:
Inject audio into voice stream in android
(2 answers)
Closed 3 years ago.
I am looking for a way to play an audio message to recipient of a phone call as soon as he/she receives the call. I want to push my audio when someone receives the call and then I start conversation. Example:- When some organization calls they start with a greetings audio message first then start the actual conversation.
I have tried searching a lot but all the questions related to this on Stackoverflow are bit old and they mention that this is not possible. Is it still the case?
Could someone please guide how one could achieve this in android?
You can play and control the audio files in android with the help of the MediaPlayer class.
MediaPlayer: This class is the primary API for playing sound and video.
AudioManager: This class manages audio sources and audio output
Simply you can use MediaPlayer and play the audio file. Check out this nice example for playing Audio. How to play audio in android..
public void audioPlayer(String path, String fileName){
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(path + File.separator + fileName);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
I'm making an Android audio visualizer app for streaming Spotify music. I'm using Spotify's android-streaming-sdk which creates a local service (no need to have the Spotify app installed) and it plays music from within the app. I'm having a difficult time getting Androids Visualizer library to pick up any audio from Spotify (it works fine if I use a local .mp3 file).
//Start playing spotify track
player.playUri(MainActivity.operationCallback,
VisualizerModel.getInstance().getTrackURI(), 0, 0);
.... other code ....
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
int audioSessionId = mediaPlayer.getAudioSessionId();
visualizer = new Visualizer(audioSessionId);
visualizer.setCaptureSize(audioSampleSize);
visualizer.setDataCaptureListener(this, Visualizer.getMaxCaptureRate(), true, true);
visualizer.setEnabled(true);
If I run this, everything compiles and runs okay, but the visualizer does not pick up any audio. This makes me thing that the audio sessionId is not the same one as Spotify is playing on. However, if I switch the audioSessionId to 0 (which should listen to all the audio sessions mixed from what I understand) it crashes with this error:
E/AudioEffect: set(): AudioFlinger could not create effect, status: -1
E/visualizers-JNI: Visualizer initCheck failed -3
E/Visualizer-JAVA: Error code -3 when initializing Visualizer.
According to their documentation that error means: Operation failed due to bad object initialization.
I'm targetting API 24 (Android 7.0).
Please let me know if you have suggestions or an alternative to using Android's Visualizer library. I feel like my main problem is that I'm not sure how to find the audioSessionId that Spotify is playing on, and because Spotify's android-streaming-sdk is in beta (and not mentioned on their website) there is virtually no documentation on it from what I can see on Github.
Thank you for taking the time to read my issue!
I was facing the same error. Adding "android.permission.RECORD_AUDIO" to manifest and requesting for runtime permissions fixed the issue.
to protect privacy of certain audio data (e.g voice mail) the use of the visualizer requires the permission android.permission.RECORD_AUDIO
Also
Creating a Visualizer on the output mix (audio session 0) requires permission Manifest.permission.MODIFY_AUDIO_SETTINGS
Android docs for Visualizer class
We are developing an app that plays podcasts, we are using services and the media player to play the files that are in the server, in some Marshmallow devices we are encountering this error (Motorola g4):
W/MediaHTTPConnection: readAt 22575768 / 32768 => java.net.ProtocolException: unexpected end of stream
But it only happens when the app is killed and the service starts to run, when the app is in foreground it works well, we have noticed that the buffering is working well, but when the app is killed it cannot buffer more, it recahes a limit and then it throws that error... In other devices the buffering is working well.
Here is the chunk of the code that prepares the media player.
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "audio/mp3");
headers.put("Accept-Ranges", "bytes");
headers.put("Status", "206");
mp = new MediaPlayer();
Uri uri = Uri.parse(songPath);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(getApplicationContext(), uri, headers);
mp.prepareAsync();
The headers were added because a post we saw, but it has the same result as if it doesn't have it.
Any advice?
Playing audio in background is a bit more complex than that. If you're just using a MediaPlayer, then that component's container (i.e. Activity) or the open http connection are probably being destroyed when Android decides to release resources. This can happen as soon as you switch to another app, or when the device (some other app) is using too many resources, or when the operating system decides it's a good time to do it.
However, MediaPlayer is just one part of the background audio playing.
I'd advice you to take a look at the oficial guide on how to build an Audio app.
https://developer.android.com/guide/topics/media-apps/audio-app/building-an-audio-app.html
I also found a well-explained step-by-step tutorial that can also help
https://code.tutsplus.com/tutorials/background-audio-in-android-with-mediasessioncompat--cms-27030
I'm wondering how can we get pause,play and upto how much user seen the video data from the youtube player api ?Is this possible to get those details ?
For getting current time : getCurrentTimeMillis()
For moving the video to the time indicated: seekToMillis(int milliSeconds)
To plays the selected video : play()
To pause the video: pause()
And to get info maybe the best option would be use JSON
I am trying to play the mp3 file from web using media player class , on some devices it is working fine but for few devices it is giving following error .
QCMediaPlayer mediaplayer NOT present
Unable to create media player
error (-38, 0)
Attempt to call getDuration without a valid mediaplayer
any help on this will be of great help.
In general error -38 means you are trying to call a function in wrong state for the player.
In your case as error says, you call getDuration() when player is not prepared or started. Set setOnPreparedListener() then start the player mp.start(), then you can call getDuration().
Check this Tutorial to stream audio from web