Categories

Archives

Play MediaPlayer with Authentication on Android

I was back to the Android lang with the free Nexus One from Google, and had a project in mind that involves MediaPlayer and for which here is how i did it.

 Java |  copy code |? 
01
private void playAudio(String mediaUrl) {
02
    try {
03
        URLConnection cn = new URL(mediaUrl).openConnection();
04
        cn.setRequestProperty("Your Header Name", "Your Header Value");
05
        InputStream is = cn.getInputStream();
06
 
07
        // create file to store audio
08
        File mediaFile = new File(this.getCacheDir(),"mediafile");
09
        FileOutputStream fos = new FileOutputStream(mediaFile);   
10
        byte buf[] = new byte[16 * 1024];
11
 
12
        // write to file until complete
13
        do {
14
                int numread = is.read(buf);   
15
            if (numread <= 0)  
16
                break;
17
            fos.write(buf, 0, numread);
18
        } while (true);
19
        fos.flush();
20
        fos.close();
21
 
22
        MediaPlayer mp = new MediaPlayer();
23
 
24
        // create listener to tidy up after playback complete
25
        MediaPlayer.OnCompletionListener listener = new MediaPlayer.OnCompletionListener() {
26
                public void onCompletion(MediaPlayer mp) {
27
                        // free up media player
28
                        mp.release();
29
                        Log.i("MediaPlayer.OnCompletionListener", "MediaPlayer Released");
30
                }
31
        };
32
        mp.setOnCompletionListener(listener);
33
 
34
        FileInputStream fis = new FileInputStream(mediaFile);
35
        mp.setDataSource(fis.getFD());
36
        mp.prepare();
37
        mp.start();
38
    } catch (Exception e) {
39
        e.printStackTrace();
40
    }
41
}

Explanation
First up this code came from http://stackoverflow.com/questions/1650983/server-side-aac-audio-with-android where it was the answer to a different question.

URLConnection cn = new URL(mediaUrl).openConnection();
cn.setRequestProperty(“Your Header Name”, “Your Header Value”);

This is the core part and probably the only explanation beside the concept of the code that ill do. On here you create a URLConnection on where you can specify the request(headers) Property, depending on how you made the authentication on your server (say like GData) you can pass any headers related values.

Now on the concept, what the code does is like what it had said in stackoverflow, it downloads the music, outputs it to a FileOutputStream under the mediaFile File, where then we get to stream it to the FileInputStream which then we use as our data source on our MediaPlayer.

Hope this helps :)

Android Involvement

This might be the last update, not that i might stop everything but instead i’ll build a prototype then later on probably build a business upon it (find some angel investor). The reason why is that if i do it full time then i could help more and thus i might not bring update with this thing but instead would continue the blogging on android and possible other mobile programming. Anyhow most probably by next week i shall be back blogging about android. Thanks

Android involvement feb update

After a month of planning to make a builder for Android apps, the progress has been super slow due to a lot of reason like work, bricking my wii, and games. So the original plan had changed from python(app engine) to Php5 due to app engine limitation on number of files that could be uploaded and the time to develop for me should be shorter. And since the progress has been so slow, i cant show anything as this moment, i believe before July this year i could release an alpha version, ahha.

Here are the list of features that had been implemented and will be implemented.
Templates from database – Initial phase started using PHP and MySql
Internationalization – Basic internationalization done, still need to improve
Saving Project – Just started
Export to an app – Just started
Plans to use it besides for Android

Besides this, the biggest update is i’ll be doing mobile programming starting April, yes i had said goodbye to my current job at Razorfish to focus more on Mobile and Frontend web, sort of a dream job, i could say. On which company, its huge here in HK and its a young company plus more of a software house (compare to Razorfish which is more on online advertising) so i’m excited to join their staff. So I’m seeing that there will be more post in the 2nd half of the year here regarding programing especially mobile based and ofcourse Android. Besides bricking my wii, the first month of this year is amazing.

Happy Tiger Year, Happy Chinese New Year.

My Android involvement for this site in 2010

Some of you might be wondering why there isn’t any post recently regarding Android or why have i not answer any of the question on the blog, i have been doing something unique for the Android community which i believe no one has ever done or atleast i haven’t seen it. So what is it?

Basically i’m applying (finally) the knowledge I gather from doing/maintaining my current company’s flash framework, our company flash generator that is based on ExtJS that would export JSFL files that could then build a working flash prototype, with fla and AS3 sources (ExtJS to PHP to JSFL to flash fla/AS3). What does this have to do with Android?

If you work with me, you will know how lazy I am that i want everything to be automate or atleast i’ll do it once and automate the next time or the development time should not be that long. But automation does came with a price, usually the price is learning a lot of technologies, like the example above i have to learn ExtJS, PHP5, JSFL and AS3. And for this project i have to learn ExtJS(more advance stuff), HTML5(will remove IE from the equation), Python using App Engine and Android (need to step up the learning). The purpose of this project is to cut down the time for you to build an app, and possibly cutting down the time for anyone to build an android app.

When can i see it?
The whole concept came to me at Dec 31 while waiting for the new year to come, and since i have a full day work therefore the whole project is still at its baby stage. Here is a screencap of the current development


(yes the phone and its layout is done using css, one of the main reason why I excluded IE).

Update Jan 11, 2009
The ExtJS part is going strong :)

Update Jan 11, 2009

Why am i doing this?
This year, my resolution is to start a new life, a new life that would mean i have to go out to the world and give back what i have gain from it. And to do that I have to be in an English speaking country, tried to apply to US, Canada, Australia and a lot more but no one came back to me, so i have to build something that would showcase my talent and at the same time give back to the community. My resume is at http://docs.google.com/Doc?docid=0AeO-OZiPmCOqZGM2anQ4bV8xMzZmNXc1MjZkNA&hl=en LOL.

The end production’s sources would be opened to the world, the builder under GPL, the python under GPLv3 or apache, then android sources under Apache sources. I would try to give update every month if possible or more frequently.

I would like to get feedback from you guys, any suggestions or any comments?

Let’s fire up the android development!!! :)

We welcome 2010, Happy new year!!!

Happy new year to everyone reading this blog and thanks a lot to all who contributed to my blog and to my apps. :)

This year has been a great year, especially having the chance to be interviewed by Google (and i failed so bad, ahha), being promoted as the Senior web developer at Razorfish, having my birthday in an airplane and a lot more so both technical and non-technical, its just a great year.

Previous year, i made some prediction, http://almondmendoza.com/2009/01/01/happy-new-year-from-almondmendozacom/. Now recap which we prediction did came true.

1) Android did really got big this year, but fake phone from china didnt help it became big.
2) Google globally increase by 1-2 percent, in japan 10+ percent, down in china due to the government favoring baidu? (from http://gs.statcounter.com), google cracked GPS market in US, browser market, smartphone market.
3) Apple release snow leopard in August 28, 2009
4) MS did release win7 (i taught they wont) but i got the lesser version part
5) This one is mysterious, there are source that said it cracked 2 percent (http://digg.com/linux_unix/Linux_Market_Share_Passes_2) and other said its 1 percent.
6) Adobe announce flash 11 but still no linux support but fla will be xml based (therefore it might be easier to create a clone on linux)
7) Not really in financial trouble companies but Google did acquire good companies this year with Admob, reCaptcha and On2.
8) FB connect is hot this year.
9) Don’t know about this one but this year conficker virus made it to the media.
10) I hate MS for not killing IE.

Now for my prediction in 2010
1) Hardware will be huge next year with usb3, 3d tv with internet, motion controller for ps3 and xbox, mac netbook?, more android phones, bluray with 3d (ps3), e-readers.

2) Adobe will be the source of the viruses and not MS, right now MS is doing a better job on security but Adobe isn’t, so in 2010 Adobe will be the target (same prediction as virus vendors)

3) Android would appear in more hardware like TV, PMP, Media box and a lot more and these might roll out to consumers

4) Googlewave would not take off if its not integrated into other services by Google coz by itself its way too complicated

5) Chrome will eat some of IE/Firefox market share especially in linux where firefox sucks big time. With the Canonical and Google Chrome OS deal, Ubuntu will having the ballot box to choice which browser you want by default, Firefox or Chrome. Or Chrome will be included by default.

6) Microsoft Bing will have a better market share but MS Office, IE, Xbox and Windows will suffer

7) Mac currently has 90%+ of 1000USD+ computers, Mac will take over the 800USD+ as well.

8) Vivendi would buy another big Game Publisher (Ubisoft/Take-two/THQ) but might not merge it with Activision Blizzard like Sierra is right now.

9) Frontend web technologies would be huge with web3d, O3d, JS2, HTML5 and it will be called web 3.0, ahahh

10) Win7 will be big (but Mac would still be the winner next year) and IE6 will finally die!!! Die IE6 and IE7!!!

These are my predictions, whats yours?

Merry Christmas (Web 1.0 style)

Merry Christmas and Happy New Year to everyone.

Someone send me an ecard that looks like its build in html3 and the encoding are wrong, hhaha

But i want to watch it new interface

Updated http://butiwanttowatchit.com/ with new interface using blueprint css framework & jquery for the frontend, cakePHP with zend framework on the backend. Head over
http://butiwanttowatchit.com/