Android Mp3 live streaming from url - java

Hello everyone,
I'm trying to play mp3 audio from a url (radio) using media player class. It is running but it took too much time to start. I know this question is already asked on this site. But I need help. If their is no solution for that kindly tell me alternative method . Reference code or tutorial would be a great help.here is my java code.
public void streaming(String url)
{
MediaPlayer mediaPlayer = new MediaPlayer();
try {
Log.i("stream", "connecting data source");
mediaPlayer.setDataSource(url);
Log.i("stream", "preparing");
mediaPlayer.prepare();
Log.i("stream", "prepared");
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
catch(Exception e){
e.printStackTrace();
}
mediaPlayer.start();
Log.i("stream", "Started");
}
}

Related

Android webRTC video call inside a Background Service

Forgive me if this question was already asked, I couldn't find an answer for my case.
So, I have an Android app with Voice & Video call feature. I used webRTC for this.
I was able to make both Voice and Video call working perfectly inside an Activity, but now I want to keep the call running while the user exit the CallActivity and go back to the ChatActivity (to send a file/link/photo for example).
I managed to make the Voice call run perfectly inside a Background Service, but video call won't work as expected.
The remote video won't be displayed even though the audio from the video track is playing.
here is my Background Service code :
#Override
public void onAddStream(MediaStream mediaStream) {
if (mediaStream.videoTracks.size() > Constants.ONE || mediaStream.audioTracks.size() > Constants.ONE) {
return;
}
//check for video track, means this is a video call
if (!isAudioCall && mediaStream.videoTracks.size() > Constants.ZERO) {
remoteVideoTrack = mediaStream.videoTracks.get(Constants.ZERO);
CallActivityNew.remoteVideoTrack = remoteVideoTrack;
try {
localAudioTrack.setEnabled(true);
//Now ask the UI to display the video track
sendOrderToActivity(Constants.START_REMOTE_VIDEO, null);
} catch (Exception ignored) {}
} else if (mediaStream.audioTracks.size() > Constants.ZERO) {
//Means this is a Voice call, only audio tracks available
remoteAudioTrack = mediaStream.audioTracks.get(Constants.ZERO);
try {
localAudioTrack.setEnabled(true);
remoteAudioTrack.setEnabled(true);
} catch (Exception ignored) {}
}
}
and below my CallActivity code :
case Constants.START_REMOTE_VIDEO: {
if (remoteVideoView == null) {
remoteVideoView = findViewById(R.id.remote_gl_surface_view);
}
remoteVideoView.init(eglBaseContext, null);
remoteVideoView.setEnableHardwareScaler(true);
remoteVideoView.setMirror(true);
remoteVideoView.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
remoteVideoView.setZOrderMediaOverlay(true);
//Apply video track to the Surface View in order to display it
remoteVideoTrack.addSink(remoteVideoView);
//now enable local video track
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//now enable local video track
remoteVideoTrack.setEnabled(true);
}
}, Constants.TIME_THREE_HUNDRED_MILLIS);
setSpeakerphoneOn(false);
break;
}
I am sending orders from Service to Activity, the "case Constants.START_REMOTE_VIDEO" work after receiving the order from Service.
I don't see where the problem, why am I only hearing sound but the remote video won't start display !!
Thank you in advance for helping.
After testing for long hours, I found that my code works just fine, I just forget to change the view visibility from "GONE" to "VISIBLE".
Yeah that was the solution, i swear xD

How to get streaming bitrate with MediaPlayer?

I'm developing a stream video app in Android with MediaPlayer. The problem is that I need to show the current bitrate, but I haven't found any valid suggestions on how to do get it?
Here is how I'm setting the video url to play:
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(VIDEO_PATH);
mediaPlayer.prepare();
mediaPlayer.init();
} catch (IOException e) {
e.printStackTrace();
}
I don't know if the only way to get that working is using ExoPlayer (which I've read it may be possible)
Any suggestions?
Thanks!
Apparently you cannot do this with MediaPlayer but you can use MediaMetadataRetriever, which is available since API level 10, i.e., quite a while ago.
int getBitRate(String url) {
final MediaMetadataRetriever mmr = new MediaMetadataRetriever();
try {
mmr.setDataSource(url, Collections.EMPTY_MAP);
return Integer.parseInt(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE));
} catch (NumberFormatException e) {
return 0;
} finally {
mmr.release();
}
}
The disadvantage this can have is that you will make an extra HTTP request for getting the metadata (only an RTT if you are streaming from an URI; if you are reading from an file descriptor it could be more serious). Hopefully no big deal.

Play Video From URL By Using Vuforia for Android Native?

I want to play a video from URL by using the Vuforia Android Native API.
I have changed the code in the VideoPlayback sample, as shown below:
VideoPlayback.java
Video from Url
mMovieName[STONES] = "..Youtube Video link..";
mMovieName[CHIPS] = "..Youtube Video link..";
mMovieName[celebVM_LOGO] = "..Youtube Video link..";
I removed some code from VideoPlayerHelper.java as shown below:
for Video from Url, in Load method
AssetFileDescriptor afd = mParentActivity.getAssets().openFd(filename);
mMediaPlayer.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
afd.close();
Added:
mMediaPlayer.setDataSource(filename);
And removing the code
try {
AssetFileDescriptor afd = mParentActivity.getAssets().openFd(filename);
afd.close();
} catch (Exception e) {
Log.d(LOGTAG, "File does not exist");
mCurrentState = MEDIA_STATE.ERROR;
mMediaPlayerLock.unlock();
mSurfaceTextureLock.unlock();
return false;
}
It's not working, can anybody suggest why? Please refer to this link for more information:
https://developer.vuforia.com/forum/android/how-work-video-url-videoplayback
I believe it will only work with links to the actual videos, for example this one.
mMediaPlayer.setDataSource("../video.mp4"); //or any video type

Check if Android Media Player can play a file

In Android, is there a way to check if a given file is a legitimate file for the media player? This is my current method of testing if a file is playable or not in a media player.
public boolean isPlayable(File file){
try {
Uri uri = Uri.fromFile(file);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(this, uri);
player.reset();
return true;
} catch (Exception e){
Toast.makeText(this, "Invalid File: " + file.getName(), Toast.LENGTH_SHORT).show();
return false;
}
}
I'm using the exception to test, basically if it throws an exception then something is up and we shouldn't play the file. To me, this doesn't seem very clean even though it seems to do the job just fine. Is there a better way I should be looking at this problem? I just want to know if a file is going to throw an exception before I try to plug it into a media player.

playing video using jmf

I am trying to play a video file using JMF but it gives me No Media Player found exception.
Here is my code, can anyone tell me what I am doing wrong here?
public class MediaPanel extends JPanel {
public MediaPanel(URL mediaURL) {
setLayout(new BorderLayout());
try {
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);
Player mediaPlayer = Manager.createRealizedPlayer(mediaURL);
Component video = mediaPlayer.getVisualComponent();
Component controls = mediaPlayer.getControlPanelComponent();
if (video != null)
add(video, BorderLayout.CENTER);
if (controls != null)
add(controls, BorderLayout.SOUTH);
mediaPlayer.start();
} catch (NoPlayerException noPlayerException) {
System.err.println("No media player found");
} // end catch
catch (CannotRealizeException cannotRealizeException) {
System.err.println("Could not realize media player");
} // end catch
catch (IOException iOException) {
System.err.println("Error reading from the source");
}
}
}
public class MediaTest {
public static void main(String args[]) {
// create a file chooser
JFileChooser fileChooser = new JFileChooser();
// show open file dialog
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) // user chose a file
{
URL mediaURL = null;
Player mediaPlayer = null;
try {
// get the file as URL
mediaURL = fileChooser.getSelectedFile().toURL();
} catch (MalformedURLException malformedURLException) {
System.err.println("Could not create URL for the file");
}
if (mediaURL != null) {
JFrame mediaTest = new JFrame("Media Tester");
mediaTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MediaPanel mediaPanel = new MediaPanel(mediaURL);
mediaTest.add(mediaPanel);
mediaTest.setSize(300, 300);
mediaTest.setVisible(true);
}
}
}
}
The exception that I am getting is No media player found
What kind of video are you trying to play? JMF is a pretty old library and won't be able to play most of modern video formats, only a few old ones (i am not even sure which ones).
Actually, if I am right, to play something specific you will have to write/add your own video-encoders into JMF or at least download and use existing ones, which are usually outdated.
If you really want to have something like tunable video player that could play any modern video there are two options (in my opinion):
Use vlcj library to embed VLC video player into your Java-application
USe JavaFX media player
I am offering only those two because I have dig through tons of libraries some time ago and there were nothing else even close to these two. Plus most of other libraries are outdated as well as JMF itself and these two are getting frequent updates and are supported with lots of users so those two are the best choice.
In case you don't mind embedding Java FX player into your application - that might be your choice.
On the other hand - vlcj is stable and easily integrated into Swing applications (its not like its hard with Java FX, but vlcj might be better for some cases).
Anyway, it is your call what to choose.

Categories