I need straming audio file in my project.
I try this code, it works at android 5.0 but dosen't work at 7.0.
String url = "http://sites.google.com/site/ubiaccessmobile/sample_audio.amr";
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.prepare() throw error
java.io.IOException: Prepare failed.: status=0x1
if i use audio file in resource, this code perfectly work.
but for streaming didn't.
i try prepareAsync(), prepare() in another thread. but same error exist.
of course, i use try-catch.
i think for runtime permisson, but this app need only INTERNET. and it didn't need runtime permisson.
what can i do??
android 7.0 cant use streaming audio???
please tell me problem, or another library.
I solve it....?
I cant know what the hell is it, if i use another audio format - like mp3, m4a - this code perfectly work.
why android 7.0 hate amr file???
You can use try catch for mediaPlayer.prepare() as:
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Related
I have saved the sample mp4 video in android internal storage in a file, But when try to read from file and playback in android videoview by parsing file path as a uri , am getting can't play this video error. I had checked many links but no use, How do i implement this.?
final String mFileName = "sample.mp4";
try{
FileOutputStream mFileOutputStream = openFileOutput(mFileName, Context.MODE_PRIVATE );
mFileOutputStream.write(getResources().getAssets().open("sample_video.mp4").read());
mFileOutputStream.close();
Log.v(TAG, "Success");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(mVideoView);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoPath(getFilesDir().getAbsolutePath()+"/sample.mp4");
mVideoView.start();
Simply you can't. VideoView internally uses a MediaPlayer, which explicitly requires a file to be world-readable (app's dirs aren't). The only way to bypass this limitation, as doc says, is to pass a File descriptor rather than the file path.
Unfortunately VideoView does not have a method like setDataSource(FileDescriptor), so you have two ways:
Access the VideoView's MediaPlayer through reflection and call setDataSource(FileDescriptor) on it. Not sure if this is a feasible solution, because the internal media player is instantiated when you call VideoView.setVideoPath, but you should not call it... in short, evaluate it, but it may be a dead end.
Use a MediaPlayer, set a surface holder on which play the video and pass a FileDescriptor directly to the MediaPlayer (imo this second solution is cleaner).
I am developing an android application for android OS > 4.0 (including and post OS). I have a sample m3u8 file as follows :
#EXTM3U
#EXT-X-TARGETDURATION:56
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:28, no desc
ulr/audio/file.mp3
#EXTINF:28, no desc
ulr/audio/file.mp3
#EXT-X-ENDLIST
and I am trying to play that file, using the following code
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnErrorListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try
{
mMediaPlayer.setDataSource(uri);
} catch (IllegalArgumentException e)
{
e.printStackTrace();
} catch (SecurityException e)
{
e.printStackTrace();
} catch (IllegalStateException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
mMediaPlayer.prepareAsync();
and my onPrepared() method is as follows :
public void onPrepared(MediaPlayer player)
{
player.start();
}
But the code first comes to onPrepared() and then immediately goes to onError(), with the what=1 and the extra=-1010.
I know this question has been asked various times (here, here and here for instance) and I also know about Vitamio, but i want to find out what is wrong with my implementation. Is there something wrong with the m3u8 file that I created? I went through its documentation and everything seems correct.
Would be really glad if someone could throw some light in this matter.
Error code -1010 matches up with MEDIA_ERROR_UNSUPPORTED which would imply that the device does not have the hardware or software codecs it needs to decode the MP3 files in your playlist.
Vitamio would work in this situation because it adds software decoding for the media. This is slower than hardware decoding and uses more battery. It can also increase your app size significantly.
This seems odd, though, since MP3 has been a supported media format for decoding in Android for a very long time.
I'm back with another problem!
I'm trying to create an app that would list a selected Livestreams, from Own3d.TV, Justin.Tv etc...
If my research isn't totally failed, I can use the MediaPlayer object to Stream video, the only question is how do I use it?
So far my code looks like this, but it's giving me an Exception when trying to prepare the MediaPlayer.
public class Media extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SurfaceView sw = new SurfaceView(this);
SurfaceHolder sh = sw.getHolder();
setContentView(sw);
Uri ur = Uri.parse("http://www.twitch.tv/widgets/live_embed_player.swf?channel=hashe");
MediaPlayer mp = new MediaPlayer();
mp.setDisplay(sh);
try {
mp.setDataSource("http://www.twitch.tv/widgets/live_embed_player.swf?channel=hashe");
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
//mp.setDisplay(sw);
}
}
Is it even possible to Stream the video from these sites using the MediaPlayer?
If not, how shoud I approach this problem?
Thanks!
when implementing mediaPlayer, try something like this, it worked fine for me: http://android-er.blogspot.com/2010/11/play-3gp-video-file-using-mediaplayer.html
but I think, u cant use source like u have written in your code. You have to use some URL from where is video streamed directly with progresive streaming, not URL of html site with player embeded. You can recognize: when u write useful URL with streamed video in your internet browser, browser starts to download this streamed video in your computer. And I think, MediaPlayer supports in core .3gp or .mp4 format only..
I hope u understand my bad english
Use ExoPlayer instead of MediaPlayer. See Android official documentation:
ExoPlayer supports features like Dynamic adaptive streaming over HTTP (DASH), SmoothStreaming and Common Encryption, which are not supported by MediaPlayer. It's designed to be easy to customize and extend.
ExoPlayer - Android Developers
i want to play sound when my condition is matched, but i don't how can i play sound or music in andengine?
help me to find my solution.
your answer will be awarded.
You can obviously use the MediaPlayer in AndEngine too, it works fine as it does in Android, because AndEngine is also a part of Android itself.
You can keep your file in res/raw/anyfile.mp3
MediaPlayer mediaPlayer = MediaPlayer
.create(getApplicationContext(), R.raw.anyfile);
try {
mediaPlayer.start();
mediaPlayer.setLooping(true);
} catch (Exception e) {
e.printStackTrace();
}
though i haven't perform a sound task, but hope this tutorial link will help u,,
http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html
I am working on android application in which i have play online radio streaming.
i have gone through the media player classes but i don't think is there any method to online streaming of radio. If any know about this please help me.
Thank You.
Vikram
Vikram,
You should be able to achieve this using the MediaPlayer; however, depending on your format it may be difficult. For example, if you're trying to play an online radio stream that uses .pls, or .m3u, you would have to parse that file, and pull out the true URLs to use.
Beyond that, you should be able to use MediaPlayer's create method with a URL to start streaming playback. Keep in mind that if the streams URL redirects (which it likely does) you may have to resolve the URL. A simple way to do this is use HttpURLConnection to open a connection, then connect(), then getURL(). You'll likely need a string url, so call toExternalForm() on the result from getURL().
Additionally, If things aren't working for you with MediaPlayer via URL, you might have to come up with your own buffering mechanism to get the data from the server. That being the case, you can try this tutorial: http://blog.pocketjourney.com/2008/04/04/tutorial-custom-media-streaming-for-androids-mediaplayer/
From what I've read, you should just be able to do:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(streamingURL);
mediaPlayer.prepare();
mediaPlayer.start();
to get basic functionality I believe, but I haven't tested it myself.
the easiest way to play a radio channel in android is to use the built in MediaPlayer, however when the datasource is from web the prepare() method takes a long time to execute and you should use prepareAsync() instead to avoid blocking the ui:
player = new MediaPlayer();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
player.start();
}
}
});
try {
player.setDataSource(currentChannelUrl);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
return;
}
player.prepareAsync();