Android MediaPlayer Uri.parse problems (Api 15) - android

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!

Related

Using VideoView with basic authenticated URLs

I'd like to know how to properly display video from an authenticated URL to VideoView. My code is functioning before Lollipop (5.0). Now, after compiling to APIs 21 and 22, the error occurs. I have tried compiling back to API 19 but the appcompat library I'm using break so many codes. Here's my sample code:
Uri videoUri = Uri.parse("https://user:12345#sample.com/dir1/dir2/dir3/myVideo.mp4");
vidAtt.setVideoURI(videoUri);
I already tried
vidAtt.setVideoURI(videoUri, headers);
but the minimum API is 21 while mine is API 16. I tried the generated URI and paste into the browser and it works. It just doesn't work in the device. I also tried passing the URI as an intent so that it can open the video link externally, whether using the stock video player or a third part one. It didn't work too.
Any help would be appreciated. Thanks in advance!
You have to use the setVideoURI-Method which is only available with Reflection:
Method setVideoURIMethod = videoview.getClass().getMethod("setVideoURI", Uri.class, Map.class);
setVideoURIMethod.invoke(videoview, Uri.parse(url), /*HashMap<String, String>*/ basicAuthentication;
Replace basicAuthentication with your Basic Authentication Header.
Can you try this
try {
VideoView.setVideoPath("https://user:12345#sample.com/dir1/dir2/dir3/myVideo.mp4");
} catch (Exception e) {
e.printStackTrace();
}
VideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
//
VideoView.start();
}
});

set ringtone app android

I want to build an android app. Basically, I have a Web site where I have a lot of music into categories and from there you can listen to it or download. I want to use my app to have a mobile view of my site, and I know how to do it with android studio, there are just some things I need to change. Anyway, I want the app background/style to be different, and I want everytime I add a new category to my site, to be added to my app to. At first, I was wanting to make a button for every category, but I realised it won t work.
Anyway, in the app, the first thing that you ll see are the categories, then if you click on one of them, you will se a list of ringtone, and if you click on a ringtone, you will have 4 options : set as ringtone, set as notification, set as alarm. I know what s the code for this things, what I do not understand is where to place it, because I want my app to use the music from my site, not to have a music as an asset and then set it (that s the way I know how to do it). I know is redundant, but I am a teenager:)). I know how to build an app that can set a ringtone, I do not know how to do it for hundreds of ringtones that I do not have as an asset.
Some ideas please ? Maybe a video oor something to read
For playing a ringtone for preview purposes you have two options:
You can either stream it via the MediaPlayer class.
which is done like this:
String url = "http://your-path";
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(url);
mPlayer.prepare();
mPlayer.start();
but keep in mind that prepare() method might take a while and it blocks UI thread. you need to use prepareAsync() and set a listener for it when prepared.
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp) {
mPlayer.start();
}
});
but if you'd also like to download it before playing it you can use this library AQuery. If you are new to android it can really help you to easily make http calls and downloads asynchronously. It is initiated and used as follows:
AQuery aq = new AQuery(context);
File ringtoneFileToDownload = new File("http://path/to/your/online/ringtone");
aq.download(url, ringtoneFileToDownload, new AjaxCallback<File>(){
#Override
public void callback(String url, File file, AjaxStatus status) {
//method is called when the download is finished
//and the file parameter is the file downloaded
//which you can play as above with the MediaPlayer class
}
});
you can use the above method to download it to the device and keep them local.
As the last thing don't forget to add the required permissions in the manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Android playing stream m3u using mediaPlayer

I'm building an application that play streaming m3u file from web.
I'm using mediaPlayer class and it works.
Here's the code :
String test_path = "http://cast.idvps.com:8000/djwirya.m3u";
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(test_path);
mediaPlayer.prepareAsync();
} catch (IOException e) {Log.e("Error", "No Stream");}
mediaPlayer.start();
It was working perfectly. But, after a whie I compiled it again, there's no sound.
pls help.... THX
You need to call mediaPlayer.setOnPreparedListener(this) before the prepareAsync(). This assumes that your activity or whatever has implemented the OnPreparedListener interface. Then you need a callback called onPrepared() in which you can call mediaPlayer.start().
The other thing you need to do is make sure you call mediaPlayer.release() somewhere when your app is ending. Inside of onPause() is probably a good idea.
This is a solution.
Sorry I'm french but i'm think that should be ok with google translation.
Link for a solution

How to play online radio in android

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();

Android Assets-Folder: it takes always the first file by alphabetical order

I'm new in Java/Android programming, so please have patience with me.
I try to play a mp3 which is locate und the assets folder. I know there is another way with the /res/raw/ folder, but use the assets-folder because later I'll try to access the file by String.
This code works to play a mp3-file:
try
{
MediaPlayer mp = new MediaPlayer();
FileDescriptor sfd = getAssets().openFd("song.mp3").getFileDescriptor();
mp.setDataSource(sfd);
mp.prepare();
mp.start();
}
catch(Exception e) {}
Now the problem: In the same assets-folder is another mp3 file stored. Though I specify the name of the mp3 to use it take the one which comes first in alphabet. E.g. the other file is named "music.mp3" it plays this one. Renaming it to "worldmusic.mp3" it will play "song.mp3". Rerename "worldmusic.mp3" back to "music.mp3" it will take this mp3 again. Another test: Renaming "song.mp3" to something other so the application can find whats specify by the code above will result that no song is played. So this means the songname have to exist, although it take arbitrary the song first in alphabet.
I'm testing with the AVD emulator of eclipse. But I think the behaviour would be the same on a real device.
Have someone an idea about this problem?
I don't believe using FileDescriptor is the right way to do this. Try using .create() and a Uri instead:
MediaPlayer mp = MediaPlayer.create(getBaseContext(), songUri);
mp.start();
Not sure why, but the URI syntax doesn't seem to work for the assets. Try getting the AssetFileDescriptor instead, as answered in the related question:
Play audio file from the assets directory

Categories

Resources