andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

[VIDEO-Tut] - Playing Media(mp3) on the emulator

Goto page Previous  1, 2, 3, 4, 5  Next
 
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials
Author Message
fisherman
Freshman


Joined: 13 Feb 2008
Posts: 2

PostPosted: Tue Feb 19, 2008 5:08 pm    Post subject: about play streaming fron the http url Reply with quote

hello, plusminus:
thanks for your help, i can play the mp3 file. Now I want to play from the http URL
below is my code 1:

Java:
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource( "http://127.0.0.1/em_ringtone1.mp3" );
if(mp!=null){
mp.prepareAsync();
mp.start();
}                   
} catch ( IOException e ) {
 Log.e( "ioexception", "",  e);                 
}


code 2
Java:
MediaPlayer mp;
Uri myUrl = Uri.parse("http://127.0.0.1/02.mp3");
mp = MediaPlayer.create(mplayer.this,myUrl);
if(mp!=null){
mp.start();
}

====================

but i still can not play mp3 from the http URL.
could you please give me some suggestion ?
by the way, did anyone try the code from http://code.google.com/android/toolbox/apis/media.html ?
====================
Java:
Playing Media via URL
Uri myUrl = Uri.parse("http://myserver.com/link/to/my.mp3");
Intent intent = new Intent(Intent.VIEW_ACTION, myUrl);
intent.setType("audio/*");
startActivity(intent);

====================
i try it, but it failed in startActivity(intent); ?

thank you very much !
Back to top
View user's profile Send private message
plusminus
Site Admin


Joined: 14 Nov 2007
Posts: 2067
Location: Germany

PostPosted: Tue Feb 19, 2008 6:16 pm    Post subject: Reply with quote

Hello fisherman,

your secodn approch did not work, because 127.0.0.1 is the internal IP of the emulator. (Remember its a seperate linux-machine within your desktop pc).

Regards,
plusminus

_________________

| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Jesmiatka
Senior Developer


Joined: 04 Feb 2008
Posts: 161
Location: Netherlands

PostPosted: Fri Feb 22, 2008 10:55 am    Post subject: Reply with quote

I'm actually having the same problem when I'm using an external URI..
When I use the R.raw.<id> it works fine, but when I use the URI it says it starts playing when I use setDataSource but it actually does not...

My external URI is (works when I open it in a browser):
http://192.168.1.127:8080/androidService-1.0/cnet_podcast110607.mp3

I do have a SDCard emulated, since it asked for it while accessing the browser.

I think none of this is the problem tough, because I get an error with a view if I use the .create(Context, URI) method, it is attached.

Anyone knows this error or how to handle this?

CODE:

Java:
               public void onClick(View arg0) {
                    Log.d("MP", "ONCLICK");

                    myPD = ProgressDialog.show(Mediaplayback.this,
                              "Please wait...", "Loading the media file...", true);

                    new Thread() {
                         public void run() {
                              try {
                                   Log.d("MP", "STARTING");
                                   mp = MediaPlayer.create(Mediaplayback.this, myURL);
                                   mp.start();
                                   Log.d("MP", "STARTED");
                              } catch (Exception e) {
                              }
                              myPD.dismiss();
                         }
                    }.start();

               }



mp_error.JPG
 Description:
 Filesize:  19.22 KB
 Viewed:  1717 Time(s)

mp_error.JPG


Back to top
View user's profile Send private message
Jesmiatka
Senior Developer


Joined: 04 Feb 2008
Posts: 161
Location: Netherlands

PostPosted: Fri Feb 22, 2008 11:10 am    Post subject: Reply with quote

Fixed View error!
Still not hearing sound.. I think the file needs to be downloaded to a local file first?

Needed to run myPD.dismiss() on the ui thread...

new code

Java:
               public void onClick(View arg0) {
                    Log.d("MP", "ONCLICK");

                    myPD = ProgressDialog.show(Mediaplayback.this,
                              "Please wait...", "Loading the media file...", true);

                    new Thread() {
                         public void run() {
                              try {
                                   Log.d("MP", "STARTING");
                                   mp = MediaPlayer.create(Mediaplayback.this, myURL);
                                   mp.start();
                                   Log.d("MP", "STARTED");
                              } catch (Exception e) {
                              }
                              runOnUIThread(new Runnable() {
                                   public void run() {
                                        try {
                                             myPD.dismiss();
                                        } catch (Exception e) {
                                        }
                                   }
                              });
                         }
                    }.start();

               }
Back to top
View user's profile Send private message
plusminus
Site Admin


Joined: 14 Nov 2007
Posts: 2067
Location: Germany

PostPosted: Fri Feb 22, 2008 1:07 pm    Post subject: Reply with quote

Hello Jesmiatka,

yes, downloading to local storage is what I normally done.
I have also seen that with some web-video-viewers.

Regards,
plusminus

_________________

| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
fisherman
Freshman


Joined: 13 Feb 2008
Posts: 2

PostPosted: Fri Feb 22, 2008 3:25 pm    Post subject: media streaming Reply with quote

hi, plusminus
thanks for your reply. i have another question about MediaPlayer, in
http://code.google.com/android/reference/android/media/MediaPlayer.html, it mention that
The MediaPlayer class is used for playing audio and video files and streams. but play mp3 file or 3gp file from url
seems to download the file and then play file, and it doest not play through streaming ?
can i play streaming audio, like "mms://streaming.books.com.tw/audio/6/0020095626_01_01.mp3".
Could you please give me some information about the media streaming that android support ?
Or whether the android m5 sdk currently only support playing from the local file , not streaming ?
thank you very much!
Back to top
View user's profile Send private message
markww
Freshman


Joined: 24 Feb 2008
Posts: 4

PostPosted: Sun Feb 24, 2008 7:42 am    Post subject: Reply with quote

Hi everyone,

The original tutorial does not work for me either - (using the m5 release). It either WILL play an mp3 but only for a fraction of a second, or it won't play the mp3 at all (they are all less than 1mb in size).

The second solution does work, I pushed the mp3s manually into the emulator using DDMS and at least I can reliably play something now with:

MediaPlayer mp = new MediaPlayer();
mp.setDataSource("tmp/test.mp3");
mp.prepareAsync();
mp.start();

My question is, shouldn't I put the mp3 files in my application's own data folder? I cannot find it through DDMS. Also, how will others need to load these mp3 files? Do I need to tell friends to take all these external mp3s and do the same push command?

I wish the original method would work with the mp3 just in your eclipse resource folder, then it would be much easier. Anyone know why it doesn't work? Yet another android bug?

Thanks,
Mark
Back to top
View user's profile Send private message
KG200802
Freshman


Joined: 26 Feb 2008
Posts: 3

PostPosted: Tue Feb 26, 2008 2:46 am    Post subject: Re: media streaming Reply with quote

fisherman wrote:
hi, plusminus
thanks for your reply. i have another question about MediaPlayer, in
http://code.google.com/android/reference/android/media/MediaPlayer.html, it mention that
The MediaPlayer class is used for playing audio and video files and streams. but play mp3 file or 3gp file from url
seems to download the file and then play file, and it doest not play through streaming ?
can i play streaming audio, like "mms://streaming.books.com.tw/audio/6/0020095626_01_01.mp3".
Could you please give me some information about the media streaming that android support ?
Or whether the android m5 sdk currently only support playing from the local file , not streaming ?
thank you very much!



sorry. my English is poor
.tw? 你是台湾的兄弟?
我使用android 可以在线听音乐 但是音乐下载总是不全 如果停到一个位置就无法继续 很郁闷啊
我用的是M5
java code: Activity
Java:
   protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.media);
        tv = (TextView)this.findViewById(R.id.title1);
        tv.setText("0%");
          Button cmd_play = (Button)this.findViewById(R.id.cmd_play);
        cmd_play.setOnClickListener(new OnClickListener(){

               @Override
               public void onClick(View arg0) {
                    MediaPlayer mp = new MediaPlayer();                
                    try {
                         mp.setDataSource("http://qlqwl.com.cn/yule/wohei/yigeren.mp3");
                              mp.prepare();
                         } catch (IOException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                         }
                    //mp.start();
                   
                    mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener(){
                         //@Override
                         public void onBufferingUpdate(MediaPlayer mp, int percent){
                              if(percent==100&&!musicFlag){
                                   mp.start();
                                   musicFlag=true;
                              }
                              tv.setText(percent+"%");
                         }
                    });
                    // i.e. react on the end of the music-file:
                    mp.setOnCompletionListener(new OnCompletionListener(){

                         // @Override
                         public void onCompletion(MediaPlayer arg0) {
                              // File has ended !!! Wink
                          //tv.setText(100+"%");
                         }
                    });
               }
        });
    }

media.xml

XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<Button android:id="@+id/cmd_play"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Play the music !!!"
    />

    <TextView android:id="@+id/title1"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center_vertical" android:textAlign="center"
        android:text="@string/dtnews_name"/>
 
</LinearLayout>
Back to top
View user's profile Send private message
markww
Freshman


Joined: 24 Feb 2008
Posts: 4

PostPosted: Wed Feb 27, 2008 6:40 am    Post subject: Reply with quote

Hi,

Thanks for your post. For whatever reason, my onBuffering() handler never reaches 100%. This is the code I'm using now:

Java:

public class SoundPlayer
{
     protected static boolean m_bSoundOn = true;

     /** Turn sound on/off. */
     public void setSoundOn(boolean bOn) {
          m_bSoundOn = bOn;
     }

     protected static ArrayList<String> m_PlayQueue = new ArrayList<String>();
     protected static boolean m_bPlayingSound = false;
     
     /** Play an mp3 file. */
     public static void playSoundBlocking(Context context,
                                         String strFileName)
     {
          if (!m_bSoundOn) {
               return;
          }
          
          if (m_bPlayingSound) {
               return;
          }
          m_bPlayingSound = true;
          
          try {
               MediaPlayer mp = new MediaPlayer();
               mp.setDataSource("data/data/files/" + strFileName);
               if (mp != null){

                    mp.setOnPreparedListener(new
                         MediaPlayer.OnPreparedListener(){
                              public void onPrepared(MediaPlayer mp) {
                                   mp.start();
                              }
                         });
                    mp.setOnCompletionListener(new
                         MediaPlayer.OnCompletionListener() {
                              public void onCompletion(MediaPlayer mp) {
                                   m_bPlayingSound = false;
                              }
                         });
                    mp.setOnErrorListener(new
                         MediaPlayer.OnErrorListener() {
                              public void onError(MediaPlayer mp, int what, int extra) {
                                   m_bPlayingSound = false;
                              }
                         });

                    mp.prepareAsync();
               }                    
          }
          catch (IOException e ) {
               context.showAlert("Error", 0, "Error playing mp3: [" + e.getMessage() + "].", "OK", true);
          }
     }
}


My OnCompletionListener() never seems to get called either. I'm not quite sure what's going on here. At least with the above code, the mp3s are played - its just that the app will randomly crash after playing a few mp3 clips.

Thanks for any pointers,
Mark
Back to top
View user's profile Send private message
KG200802
Freshman


Joined: 26 Feb 2008
Posts: 3

PostPosted: Wed Feb 27, 2008 7:12 am    Post subject: Reply with quote

markww wrote:
Hi,

Thanks for your post. For whatever reason, my onBuffering() handler never reaches 100%. This is the code I'm using now:



setOnBufferingUpdateListener will be called when playing online music(http://...........XXXX.mp3), if playing local music, setOnBuffering UpdateListener will not be called and I've only listened 500K online music and it stoped.
Back to top
View user's profile Send private message
KG200802
Freshman


Joined: 26 Feb 2008
Posts: 3

PostPosted: Wed Feb 27, 2008 7:25 am    Post subject: Reply with quote

markww wrote:


My OnCompletionListener() never seems to get called either. I'm not quite sure what's going on here. At least with the above code, the mp3s are played - its just that the app will randomly crash after playing a few mp3 clips.



combine OnCompletionListener() with OnBufferingUpdateListener() when playing online music
Back to top
View user's profile Send private message
jessica
Freshman


Joined: 27 Dec 2007
Posts: 4

PostPosted: Wed Feb 27, 2008 9:39 am    Post subject: Reply with quote

Hello everyone,
I have a problem of palying video use m5. I put the video file under the res/raw folder, now i can here the sound of the video but i can't see the interface. When i push the video file into the file system of emulator, then it paly normarlly. I play the video file in the same situation very well use rc3. And whether the VideoView can play the video file which under res/raw folder.
Anyone can help me.
Back to top
View user's profile Send private message
markww
Freshman


Joined: 24 Feb 2008
Posts: 4

PostPosted: Wed Feb 27, 2008 5:57 pm    Post subject: Reply with quote

Ok, I understand now that buffering is only for online media.

I'm trying to just play a local file. This is what my class to play mp3s looks like:

Java:

public class SoundPlayer
{
     protected static boolean m_bSoundOn = true;

     /** Turn sound on/off. */
     public void setSoundOn(boolean bOn) {
          m_bSoundOn = bOn;
     }

     protected static boolean m_bPlayingSound = false;
     
     /** Play an mp3 file. */
     public static void playSoundBlocking(Context context,
                                         String strFileName)
     {
          if (!m_bSoundOn) {
               return;
          }
          
          if (m_bPlayingSound) {
               return;
          }
          m_bPlayingSound = true;
          
          try {
               MediaPlayer media = new MediaPlayer();
               media.setDataSource("data/data/myapp/files/" + strFileName);
               if (media != null){

                    media.setOnPreparedListener(new
                         MediaPlayer.OnPreparedListener(){
                              public void onPrepared(MediaPlayer mp) {
                                   mp.start();
                              }
                         });
                    media.setOnCompletionListener(new
                         MediaPlayer.OnCompletionListener() {
                              public void onCompletion(MediaPlayer mp) {
                                   //mp.release();
                                   m_bPlayingSound = false;
                              }
                         });
                    media.setOnErrorListener(new
                         MediaPlayer.OnErrorListener() {
                              public void onError(MediaPlayer mp, int what, int extra) {
                                   //mp.release();
                                   m_bPlayingSound = false;
                              }
                         });

                    media.prepareAsync();
               }                    
          }
          catch (IOException e ) {
               context.showAlert("Error", 0, "Error playing mp3: [" + e.getMessage() + "].", "OK", true);
          }
     }
}


Again, it will play the files, but randomly the app will just crash after a few files are played (not consistent).

Is anyone actually playing mp3s consistently without error?

Thanks,
Mark
Back to top
View user's profile Send private message
JaydeRyu
Freshman


Joined: 16 Mar 2008
Posts: 4

PostPosted: Sun Mar 16, 2008 10:15 am    Post subject: Reply with quote

I've been reading through the thread that MediaPlayer.create(context, id) doesn't seem to work and that using MediaPlayer.setDataSource(string) works. My question is, can the setDataSource() read data from the .apx? If it does work what would the String to get to the /res/raw/my.mp3?

I have tried both methods and have been unable to succeed in playing a small .wav file.
Back to top
View user's profile Send private message
windkiosk
Freshman


Joined: 07 Mar 2008
Posts: 5

PostPosted: Tue Mar 18, 2008 8:19 am    Post subject: Re: media streaming Reply with quote

fisherman wrote:
hi, plusminus
thanks for your reply. i have another question about MediaPlayer, in
http://code.google.com/android/reference/android/media/MediaPlayer.html, it mention that
The MediaPlayer class is used for playing audio and video files and streams. but play mp3 file or 3gp file from url
seems to download the file and then play file, and it doest not play through streaming ?
can i play streaming audio, like "mms://streaming.books.com.tw/audio/6/0020095626_01_01.mp3".
Could you please give me some information about the media streaming that android support ?
Or whether the android m5 sdk currently only support playing from the local file , not streaming ?
thank you very much!


I have the same question. It should be some way to play audio stream as J2ME or RIM platform.
Thanks for sb to reply.
Back to top
View user's profile Send private message
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials All times are GMT + 1 Hour
Goto page Previous  1, 2, 3, 4, 5  Next
Page 4 of 5

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


© 2007, Android Development Community
All rights reserved.
Powered by phpBB.