I am trying to run media through android mediaplayer with some API link (thus doesn't have .mp3 in the end of url) as follows:
android code :
headers.put("token", token_value);
myPlayer.setDataSource(
getApplicationContext(),
Uri.parse(url),//url : http://basedomain.com/api/getmp3
headers
);
backend laravel code :
$song_path = "abc.mp3";
$path = storage_path().DIRECTORY_SEPARATOR."somefolder".DIRECTORY_SEPARATOR.$song_path;
$response = new BinaryFileResponse($path);
return $response;
I am getting the binary response from laravel code(tested through postman), however android code is not playing the music from laravel response.
android issue list :
1) W/MediaPlayer: Couldn't open http://basedomain.com/api/getmp3: java.io.FileNotFoundException: No content provider: http://basedomain.com/api/getmp3
2) E/MediaPlayer: error (1, -2147483648)
I've just tried the following code in Android studio targeting Android SDK 28 and it works fine. Make sure your API is actually returning the mp3 file.
Android MediaPlayer docs
try {
MediaPlayer player = new MediaPlayer();
player.setDataSource("https://yourdomain/yourmusic.mp3");
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
Try testing your code using a url to a known public mp3 file to verify that the MediaPlayer code is correct. Once that is working, you can narrow the issue down to either the token not being set correctly or your server not returning the mp3 file correctly. Maybe the response doesn't indicate it is an mp3 file correctly and thus the media player doesn't know how to handle it.
I finally found the reason behind this issue. My backend laravel api call was a POST request. Changing the same to Get request worked for me.
Also changing request from POST to GET was not an issue as my request parameters were sent through the header. (This was done because android mediaplayer's setDataSource method only accepts headers and not body parameters).
In short android mediaplayer is expecting us to use GET request only.
Related
I am trying to load a video file at runtime and play that video using the Unitys Video player component.The video is located in a folder on the mobile. i receive the URL to the file location(this is tested correct). While looking for the file Unity even finds the file at location but is not able to play it. I receive the following error :
AndroidVideoMedia: Error opening extractor: -10002
There is nothing on the internet regarding this issue as well. One page said something about changing gradle version.
Please note that this works fine on the editor. I have also noticed if putting a file in the persistent data path of the app, then loading and playing is fine. But if the file is kept in any other custom folder, then even after finding the video Unity is not able to play it.
Following is a snippet of the code :
void Start()
{
StartCoroutine("LoadVideoRoutine");
}
IEnumerator LoadVideoRoutine()
{
if(video==null)
yield return null;
string root = _arManager.VideoURLToPlay;
print("This is video url : " + root);
if(!File.Exists(root))
{
print("no video");
yield return null;
}
else if(File.Exists(root))
{
print("Found vid");
video.url = root;
video.Prepare();
// video.Play();
}
while(!video.isPrepared)
{
print("preparing");
yield return null;
}
print("playing");
video.Play();
// video.url = root;
// video.Play();
}
Following is a sample URL where the video is present :
"/storage/emulated/0/MyApp/RaceRecordings/3D2A4AF9-A6EB-4D48-92D2-1B2A6ADC968A.mp4"
In the logs, I got the "Found vid" log indicating that unity finds the file. But immediately when I am setting the URL I got the error.
I am not sure what this is and would appreciate some help in this regard.
Maybe try remove the .mp4. Maybe it doesn't belong to the URL but I don't know much about it.
use thisMagic Exo player library.
can play online and offline video and handle initializing and deint the payer on its own
I have some encrypted (AES-128) .m3u8 stream in my Android app and I want to play it in ExoPlayer. I have for this two variables:
val secretKey = "a4cd9995a1aa91e1"
val initVector = "0000000000000000"
As I read in docs I need to add URI and IV parameters into source file. After adding I have the next one:
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:6
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-DISCONTINUITY
#EXT-X-KEY:METHOD=AES-128,URI="data:text/plain;charset=utf-8,a4cd9995a1aa91e1",IV=0x30303030303030303030303030303030
#EXTINF:6.0,
media_b525000_0.ts
#EXTINF:6.0,
media_b525000_1.ts
#EXTINF:6.0,
media_b525000_2.ts
*other .ts segments...*
where I added two lines: #EXT-X-DISCONTINUITY and #EXT-X-KEY. But the player doesnt play the stream and I have the next exception:
com.google.android.exoplayer2.upstream.HttpDataSource$HttpDataSourceException: Malformed URL
What did I do wrong? And how can I to decrypt stream when I have secretKey and initVector?
Exoplayer assumes anything following URI is an actual URL in m3u8 file and when it tries to load the encryption key using the below url which is invalid, above exception is thrown by OkHttpDataSource class.
URI="data:text/plain;charset=utf-8,a4cd9995a1aa91e1"
This problem can be solved in two ways.
1. Place the encryption key in a server and use a URL to fetch it.
2. Implement custom data source classes and implement, intercept the calls and modify the request/response objects as per your need.
I have faced similar issue but in my case i have a custom scheme instead of http. Default OkHttpDataSource does not handle custom url schemes hence i had to write my own data source and intercept.
How to Catch all Call Back Notification for getting .m3u8 or mp4 link from dailymotion embed link when play in android default media player using web view android?
We have another solution to get link using html method like this one.
using Demo #1: How to Embed HLS / M3u8 Streaming: Demo of M3u8 / HLS embedding with HTML5 rollove
But i want to get m3u8 link when webview notification through to default mediaplayer of android for playing a video in default android mediaplayer Please help me i am stuck approx 2 weeks Thanks.
I get a code in IOS(Objective C) and in C language level for getting notification of player that code is following
C-Language Level:
void PlayerWillExitFullscreen (CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef userInfo)
{
//do something...
}
CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(),
NULL,
PlayerWillExitFullscreen,
CFSTR("UIMoviePlayerControllerWillExitFullscreenNotification"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
Objective-C Level:
- (void)playerWillExitFullscreen:(NSNotification *)notification {
//do something... }
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerWillExitFullscreen:)
name:#"UIMoviePlayerControllerWillExitFullscreenNotification"
object:nil];
can any one provide me alternative code in android please help me yet i am stuck
I have checked the similar questions but they did not seem to solve the purpose hence asking the question along with the code logic.
I am able to get a REST url from S3 of format : https://s3.amazonaws.com/{bucket}/file.MOV?Signature=xyz&Expires=abc&AWSAccessKeyId=lmn
Now I need the Media player to play(stream) this video.
My Code :
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(urlS3REST);
mp.prepare();
mp.start();
}
catch{...}
The Error thrown here is : Prepare failed. : status=0x105
Any suggestions?
const PVMFStatus PVMFErrRTSP458ParameterIsReadOnly = (-105);
You might not have permission to write to the file,Files can only be read.
I'm using a book called Android Wireless Application Development 2nd edition 2009 (L.Darcey & S.Conder, published by Addison Wesley) as my literature for a course I'm currently doing in Android app development. In the third chapter of this book you use the MediaPlayer class to initiate a media player object to stream mp3 files to your app using to this method below.
public void playMusicFromWeb() {
try {
Uri file = Uri.parse("http://downloads.bbc.co.uk/podcasts/6music/adamandjoe/adamandjoe_20111231-1430a.mp3");
mp = MediaPlayer.create(this, file);
mp.start();
}
catch (Exception e) {
Log.e(DEBUG_TAG, "Player failed", e);
}
}
Happy days, this all works just fine in API 8 (which is what the book uses). However trying to use this method in a higher API, especially 15 which is (on writing) the latest API available I get a NullPointerException thrown. Debugging makes me none the wiser as the file variable seems to hold the string value fine, but it won't translate into a functional mp3 stream. I have tried various different approaches using prepare(); prepareAsync(); setDataSource(); reset(); etc., however nothing seems to work. You'd think that my teachers would be able to help me out, but they apparently don't know what to do either.
I now assume that this way of streaming must be obsolete in higher API's than 8. I would be very grateful if someone could shine a light on how to stream an mp3 file in API 15.
I think you have to do more than that. Here is an example for doing what you want:
The Header's content-type of the URL could be bad.
You could try this syntax:
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(URL_OF_FILE);
mp.prepare();
mp.start();
See if the NullPointerException remains.
You could also view a more recent tutorial on this particular subject.
It was as simple as adding this line:
<uses-permission android:name="android.permission.INTERNET" />
to the manifest file!