How to play online stream in Android - android

I have online radio (shout cast ) in web. I want to develop android app for listen this stream. so I want to know how to play online stream in android. using URL. In Android Im not going to Stream the audio. I want listen the web stream from android app.
How to do that..?
thanks

Something like
private void init() throws IOException {
try {
mediaPlayer = new MediaPlayer();
String streamPath = "";//enter path
mediaPlayer.setDataSource(streamPath);
mediaPlayer.prepare();
} catch (MalformedURLException ex) {
throw new RuntimeException("Wrong url for mediaplayer! " + ex);
} catch (IllegalStateException ex) {
} catch (IllegalArgumentException ex) {
throw new RuntimeException("Wrong url for mediaplayer! " + ex);
}
}
private void play() {
mediaPlayer.start();
}

Please refer the link it may help you a while, http://developer.android.com/guide/topics/media/index.html

Related

MediaRecorder issue on Android Lollipop

I'm testing libstreaming on new Android Lollipop, and this code that worked on previous release, seems to launch exception.
try {
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setVideoEncoder(mVideoEncoder);
mMediaRecorder.setPreviewDisplay(mSurfaceView.getHolder().getSurface());
mMediaRecorder.setVideoSize(mRequestedQuality.resX,mRequestedQuality.resY);
mMediaRecorder.setVideoFrameRate(mRequestedQuality.framerate);
// The bandwidth actually consumed is often above what was requested
mMediaRecorder.setVideoEncodingBitRate((int)(mRequestedQuality.bitrate*0.8));
// We write the ouput of the camera in a local socket instead of a file !
// This one little trick makes streaming feasible quiet simply: data from the camera
// can then be manipulated at the other end of the socket
mMediaRecorder.setOutputFile(mSender.getFileDescriptor());
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (Exception e) {
throw new ConfNotSupportedException(e.getMessage());
}
Launched exception is:
MediaRecorder: start failed -38
11-18 09:50:21.028: W/System.err(15783): net.majorkernelpanic.streaming.exceptions.ConfNotSupportedException
11-18 09:50:21.028: W/System.err(15783): at net.majorkernelpanic.streaming.video.VideoStream.encodeWithMediaRecorder(VideoStream.java:442)
11-18 09:50:21.028: W/System.err(15783): at net.majorkernelpanic.streaming.MediaStream.start(MediaStream.java:250)
I've tried to comment:
mMediaRecorder.setOutputFile(mSender.getFileDescriptor());
no exception launched, but when I start streaming a dialog tell me that need an outputfile.
Help appreciated.
I filed a bug report on AOSP.
https://code.google.com/p/android/issues/detail?id=80715
"The current SELinux policies don't allow for mediaserver to handle app generated abstract unix domain sockets.
Instead, I'd recommend you create a pipe-pair ( http://developer.android.com/reference/android/os/ParcelFileDescriptor.html#createPipe() ) which is allowed by the Android 5.0 policy.
"
I don't know why they did this or how we were supposed to know.
I'm using a very old/modified (can't tell) version of libstreaming where mediastream is still extended from mediarecorder, but looking at the current version, in MediaStream you'll probably want to change createSockets to something including the following:
ParcelFileDescriptor[] parcelFileDescriptors =ParcelFileDescriptor.createPipe();
parcelRead = new ParcelFileDescriptor(parcelFileDescriptors[0]);
parcelWrite = new ParcelFileDescriptor(parcelFileDescriptors[1]);
then in your video/audio stream
setOutputFile(parcelWrite.getFileDescriptor());
and in that same file
change
// The packetizer encapsulates the bit stream in an RTP stream and send it over the network
mPacketizer.setInputStream(mReceiver.getInputStream());
mPacketizer.start();
to
InputStream is = null;
try{ is = new ParcelFileDescriptor.AutoCloseInputStream(parcelRead);
}
catch (Exception e){}
mPacketizer.setInputStream(is);
As andreasperelli pointed out in the comment, make sure to close the ParcelFileDescriptors in closeSockets(), or depending on your implementation and version, before closeSockets() and before you call MediaRecorder.stop().
at Android 6.0 I resolve this problem with the code
new Thread(new Runnable() {
#Override public void run() {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (true) {
byte[] buffer = new byte[0];
try {
buffer = new byte[inputStream.available()];
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
try {
mSender.getOutputStream().write(buffer);
mSender.getOutputStream().flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
I use a file as buffer and write bytes at another thread.the MediaRecorder output to the file.

Android rtsp url not loading

I'm trying to play an rtsp stream using MediaPlayer in android and the application seems to always become stuck on MediaPlayer.prepare();
The url is valid as I tested it using VLC on my desktop.
Any ideas why the application is not preparing the stream.
class InitializeService extends Thread {
#Override
public void run() {
try {
player.prepare();
Log.d("Play", "Player prepared");
} catch (IOException e) {
e.printStackTrace();
fallback();
} catch (IllegalStateException e) {
e.printStackTrace();
fallback();
}
}
}
The log statement is never reached.
Update 1:
Sorry I forgot to mention that the stream will always be in 3gp format. Here is a url rtsp://r2---sn-p5qlsu76.c.youtube.com/CiILENy73wIaGQnTXOVs7Kwo8xMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
Your stream might not be of a format supported by Android.
Check http://developer.android.com/guide/appendix/media-formats.html to see if Android supports it.
Turns out it was android l that wasn't able to play the streams.

Chromecast Android SDK: RemoteMediaPlayer always ends in IllegalStateException when trying to control

I'm trying to add media controls to my app but I can't get the RemoteMediaPlayer to send commands.
The video starts playing but then I can't control it.
This is the code I use:
RemoteMediaPlayer mRemoteMediaPlayer = new RemoteMediaPlayer();
try {
Cast.CastApi.setMessageReceivedCallbacks(apiClient, mRemoteMediaPlayer.getNamespace(), mRemoteMediaPlayer);
MediaMetadata mediaMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE);
JSONObject jsonExtra = new JSONObject();
mediaMetadata.putString(MediaMetadata.KEY_TITLE, "My video");
if (mediaType != null) {
jsonExtra.put("type", mediaType);
}
if ("audio".equals(mediaType)) {
mimeType = "audio/mp3";
}
com.google.android.gms.cast.MediaInfo.Builder builder = new MediaInfo.Builder(getUrl()).setContentType(mimeType).setMetadata(mediaMetadata).setCustomData(jsonExtra);
builder.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED);
MediaInfo mediaInfo = builder.build();
mRemoteMediaPlayer.load(apiClient, mediaInfo, true, inititalTime).setResultCallback(new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
#Override
public void onResult(MediaChannelResult result) {
if (result.getStatus().isSuccess()) {
log(context, "Media loaded successfully");
}
}
});
Thread.sleep(5000);
mRemoteMediaPlayer.pause(apiClient);
} catch (IllegalStateException e) {
log(context, "Problem occurred with media during loading " + e);
} catch (IOException e) {
log(context, "Problem occurred with media during loading " + e);
} catch (JSONException e) {
log(context, "Problem occurred with media during loading " + e);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You may notice that at the end I wait 5 seconds to make sure that the video is playing and then try to pause it.
This always results in an IllegalStateException with the message:
No current media session
Am I missing something?
I also notice that the ResultCallback is never called after the video starts playing. Maybe this is also related to the same issue I'm experiencing.
Thanks in advance.
It is not clear when you are running this piece of code. Do not set a sleep like you did; you have to register callbacks on mRemoteMediaPlayer to be notified when status or metadata changes on the remote player. When the status changed listener is called, get the updated status by calling mRemoteMediaPlayer.getMediaStatus().getPlayerState() and based on the status (whether it is playing, buffering, idle, paused), make the appropriate decision. In asynchronous systems, never use "sleep()", always hook into the callbacks.

android .mov format video playing from url

I am playing video from url then .mp4 in playing but .mov format is giving IOException
java.io.IOException: Prepare failed.: status=0x1
My code for playing video is,
private void playVideo() {
if (extras.getString("video_path").equals("VIDEO_URI")) {
showToast("Please, set the video URI in HelloAndroidActivity.java in onClick(View v) method");
} else {
new Thread(new Runnable() {
public void run() {
try {
player.setDataSource(extras.getString("video_path"));
player.setDisplay(holder);
player.prepare();
} catch (IllegalArgumentException e) {
showToast("Error while playing video");
Log.i(TAG, "========== IllegalArgumentException ===========");
e.printStackTrace();
} catch (IllegalStateException e) {
showToast("Error while playing video");
Log.i(TAG, "========== IllegalStateException ===========");
e.printStackTrace();
} catch (IOException e) {
showToast("Error while playing video. Please, check your network connection.");
Log.i(TAG, "========== IOException ===========");
e.printStackTrace();
}
}
}).start();
}
}
Any pointers?
Please visit the website vitamio.org or download VitamioDemo from github github.com/yixia/VitamioDemo
if you want to use Vitamio as your video player in an app, you need to download the Vitamio SDK from their website. Play around with the demo project and you'll understand how it works.
I have implemented the same and it works absolutely fine with me.
.mov files are not supported in android.
Check the Supported Media Formats on developers site.

Using MediaPlayer to play an AMR file created by MediaRecorder fails with muri null

I am writing an app that records voice from the microphone in AMR format using MediaRecorder, and then plays the data back using MediaPlayer.
That's the goal anyway.
I am fairly confident my MediaRecorder side is working, I'm producing the data file in the right place at the right data rate. Here's how I start and stop my MediaRecorder
public void OnStartRecord(View v )
{
System.out.println( "StartRecord");
try {
audioFile = File.createTempFile("amrtmp", ".amr", getApplicationContext().getFilesDir());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println( "Recording to " + audioFile.getAbsolutePath());
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setAudioEncodingBitRate(4750);
mRecorder.setAudioSamplingRate(8000);
mRecorder.setOutputFile(audioFile.getAbsolutePath());
try {
mRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mRecorder.start();
}
public void OnStopRecord(View v )
{
System.out.println( "StopRecord");
mRecorder.stop();
mRecorder.release();
}
This works like a charm. Typical output is something like
StartRecord
Recording to /data/data/com.test.playback/files/amrtmp-235967797.amr
And when I start, then stop recording I can see that the file has been created and it has a certain amount of data in it that properly corresponds to the settings.
Side note: I detect an odd buzzing at my speaker while this runs. Any idea what that is?
When I try to play the file back however I have no end of trouble. I have tried the following:
public void OnPlay(View v )
{
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
FileInputStream FIS = null;
try {
FIS = new FileInputStream(audioFile.getAbsolutePath());
mPlayer.setDataSource(FIS.getFD());
mPlayer.prepare();
}
catch( Exception e )
{
e.printStackTrace();
}
mPlayer.start();
}
This results in nothing being played at all with the following output from MediaPlayer:
start() mURI is null
I have also tried the same code, but setting mPlayer's data source differently:
mPlayer.setDataSource(audioFile.getAbsolutePath());
This fails when prepare is called witha java.io.IOException status 0x1.
I have to imagine there is something else I need to do with MediaPlayer to set it up properly. Any suggestions?

Categories

Resources