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

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

about play streaming fron the http url

Postby fisherman » Tue Feb 19, 2008 5:08 pm

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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. MediaPlayer mp = new MediaPlayer();
  2.  
  3. try {
  4.  
  5. mp.setDataSource( "http://127.0.0.1/em_ringtone1.mp3" );
  6.  
  7. if(mp!=null){
  8.  
  9. mp.prepareAsync();
  10.  
  11. mp.start();
  12.  
  13. }                              
  14.  
  15. } catch ( IOException e ) {
  16.  
  17.  Log.e( "ioexception", "",  e);                    
  18.  
  19. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


code 2
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. MediaPlayer mp;
  2.  
  3. Uri myUrl = Uri.parse("http://127.0.0.1/02.mp3");
  4.  
  5. mp = MediaPlayer.create(mplayer.this,myUrl);
  6.  
  7. if(mp!=null){
  8.  
  9. mp.start();
  10.  
  11. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

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

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 ?
====================
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Playing Media via URL
  2.  
  3. Uri myUrl = Uri.parse("http://myserver.com/link/to/my.mp3");
  4.  
  5. Intent intent = new Intent(Intent.VIEW_ACTION, myUrl);
  6.  
  7. intent.setType("audio/*");
  8.  
  9. startActivity(intent);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

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

thank you very much !
fisherman
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 13, 2008 7:57 pm

Top

Postby plusminus » Tue Feb 19, 2008 6:16 pm

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
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Jesmiatka » Fri Feb 22, 2008 10:55 am

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/androidServic ... 110607.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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                         public void onClick(View arg0) {
  2.  
  3.                                 Log.d("MP", "ONCLICK");
  4.  
  5.  
  6.  
  7.                                 myPD = ProgressDialog.show(Mediaplayback.this,
  8.  
  9.                                                 "Please wait...", "Loading the media file...", true);
  10.  
  11.  
  12.  
  13.                                 new Thread() {
  14.  
  15.                                         public void run() {
  16.  
  17.                                                 try {
  18.  
  19.                                                         Log.d("MP", "STARTING");
  20.  
  21.                                                         mp = MediaPlayer.create(Mediaplayback.this, myURL);
  22.  
  23.                                                         mp.start();
  24.  
  25.                                                         Log.d("MP", "STARTED");
  26.  
  27.                                                 } catch (Exception e) {
  28.  
  29.                                                 }
  30.  
  31.                                                 myPD.dismiss();
  32.  
  33.                                         }
  34.  
  35.                                 }.start();
  36.  
  37.  
  38.  
  39.                         }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
Attachments
mp_error.JPG
mp_error.JPG (19.22 KiB) Viewed 15444 times
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby Jesmiatka » Fri Feb 22, 2008 11:10 am

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

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                         public void onClick(View arg0) {
  2.  
  3.                                 Log.d("MP", "ONCLICK");
  4.  
  5.  
  6.  
  7.                                 myPD = ProgressDialog.show(Mediaplayback.this,
  8.  
  9.                                                 "Please wait...", "Loading the media file...", true);
  10.  
  11.  
  12.  
  13.                                 new Thread() {
  14.  
  15.                                         public void run() {
  16.  
  17.                                                 try {
  18.  
  19.                                                         Log.d("MP", "STARTING");
  20.  
  21.                                                         mp = MediaPlayer.create(Mediaplayback.this, myURL);
  22.  
  23.                                                         mp.start();
  24.  
  25.                                                         Log.d("MP", "STARTED");
  26.  
  27.                                                 } catch (Exception e) {
  28.  
  29.                                                 }
  30.  
  31.                                                 runOnUIThread(new Runnable() {
  32.  
  33.                                                         public void run() {
  34.  
  35.                                                                 try {
  36.  
  37.                                                                         myPD.dismiss();
  38.  
  39.                                                                 } catch (Exception e) {
  40.  
  41.                                                                 }
  42.  
  43.                                                         }
  44.  
  45.                                                 });
  46.  
  47.                                         }
  48.  
  49.                                 }.start();
  50.  
  51.  
  52.  
  53.                         }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby plusminus » Fri Feb 22, 2008 1:07 pm

Hello Jesmiatka,

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

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

media streaming

Postby fisherman » Fri Feb 22, 2008 3:25 pm

hi, plusminus
thanks for your reply. i have another question about MediaPlayer, in
http://code.google.com/android/referenc ... layer.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!
fisherman
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 13, 2008 7:57 pm

Top

Postby markww » Sun Feb 24, 2008 7:42 am

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
markww
Freshman
Freshman
 
Posts: 9
Joined: Sun Feb 24, 2008 7:36 am

Re: media streaming

Postby KG200802 » Tue Feb 26, 2008 2:46 am

fisherman wrote:hi, plusminus
thanks for your reply. i have another question about MediaPlayer, in
http://code.google.com/android/referenc ... layer.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
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     protected void onCreate(Bundle icicle) {
  2.         super.onCreate(icicle);
  3.         setContentView(R.layout.media);
  4.         tv = (TextView)this.findViewById(R.id.title1);
  5.         tv.setText("0%");
  6.                 Button cmd_play = (Button)this.findViewById(R.id.cmd_play);
  7.         cmd_play.setOnClickListener(new OnClickListener(){
  8.  
  9.                @Override
  10.                public void onClick(View arg0) {
  11.                     MediaPlayer mp = new MediaPlayer();                
  12.                     try {
  13.                         mp.setDataSource("http://qlqwl.com.cn/yule/wohei/yigeren.mp3");
  14.                                                 mp.prepare();
  15.                                         } catch (IOException e) {
  16.                                                 // TODO Auto-generated catch block
  17.                                                 e.printStackTrace();
  18.                                         }
  19.                     //mp.start();
  20.                    
  21.                     mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener(){
  22.                         //@Override
  23.                         public void onBufferingUpdate(MediaPlayer mp, int percent){
  24.                                 if(percent==100&&!musicFlag){
  25.                                         mp.start();
  26.                                         musicFlag=true;
  27.                                 }
  28.                                 tv.setText(percent+"%");
  29.                         }
  30.                     });
  31.                     // i.e. react on the end of the music-file:
  32.                     mp.setOnCompletionListener(new OnCompletionListener(){
  33.  
  34.                          // @Override
  35.                          public void onCompletion(MediaPlayer arg0) {
  36.                               // File has ended !!! Wink
  37.                                  //tv.setText(100+"%");
  38.                          }
  39.                     });
  40.                }
  41.         });
  42.     }
  43.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4

media.xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7. <Button android:id="@+id/cmd_play"
  8.    android:layout_width="fill_parent"
  9.    android:layout_height="wrap_content"
  10.    android:text="Play the music !!!"
  11.    />
  12.     <TextView android:id="@+id/title1"
  13.    android:layout_width="fill_parent" android:layout_height="fill_parent"
  14.    android:gravity="center_vertical" android:textAlign="center"
  15.        android:text="@string/dtnews_name"/>  
  16. </LinearLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
KG200802
Freshman
Freshman
 
Posts: 3
Joined: Tue Feb 26, 2008 2:34 am

Postby markww » Wed Feb 27, 2008 6:40 am

Hi,

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

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class SoundPlayer
  3.  
  4. {
  5.  
  6.         protected static boolean m_bSoundOn = true;
  7.  
  8.  
  9.  
  10.         /** Turn sound on/off. */
  11.  
  12.         public void setSoundOn(boolean bOn) {
  13.  
  14.                 m_bSoundOn = bOn;
  15.  
  16.         }
  17.  
  18.  
  19.  
  20.         protected static ArrayList<String> m_PlayQueue = new ArrayList<String>();
  21.  
  22.         protected static boolean m_bPlayingSound = false;
  23.  
  24.        
  25.  
  26.         /** Play an mp3 file. */
  27.  
  28.         public static void playSoundBlocking(Context context,
  29.  
  30.                                          String strFileName)
  31.  
  32.         {
  33.  
  34.                 if (!m_bSoundOn) {
  35.  
  36.                         return;
  37.  
  38.                 }
  39.  
  40.                
  41.  
  42.                 if (m_bPlayingSound) {
  43.  
  44.                         return;
  45.  
  46.                 }
  47.  
  48.                 m_bPlayingSound = true;
  49.  
  50.                
  51.  
  52.                 try {
  53.  
  54.                         MediaPlayer mp = new MediaPlayer();
  55.  
  56.                         mp.setDataSource("data/data/files/" + strFileName);
  57.  
  58.                         if (mp != null){
  59.  
  60.  
  61.  
  62.                                 mp.setOnPreparedListener(new
  63.  
  64.                                         MediaPlayer.OnPreparedListener(){
  65.  
  66.                                                 public void onPrepared(MediaPlayer mp) {
  67.  
  68.                                                         mp.start();
  69.  
  70.                                                 }
  71.  
  72.                                         });
  73.  
  74.                                 mp.setOnCompletionListener(new
  75.  
  76.                                         MediaPlayer.OnCompletionListener() {
  77.  
  78.                                                 public void onCompletion(MediaPlayer mp) {
  79.  
  80.                                                         m_bPlayingSound = false;
  81.  
  82.                                                 }
  83.  
  84.                                         });
  85.  
  86.                                 mp.setOnErrorListener(new
  87.  
  88.                                         MediaPlayer.OnErrorListener() {
  89.  
  90.                                                 public void onError(MediaPlayer mp, int what, int extra) {
  91.  
  92.                                                         m_bPlayingSound = false;
  93.  
  94.                                                 }
  95.  
  96.                                         });
  97.  
  98.  
  99.  
  100.                                 mp.prepareAsync();
  101.  
  102.                         }                    
  103.  
  104.                 }
  105.  
  106.                 catch (IOException e ) {
  107.  
  108.                         context.showAlert("Error", 0, "Error playing mp3: [" + e.getMessage() + "].", "OK", true);
  109.  
  110.                 }
  111.  
  112.         }
  113.  
  114. }
  115.  
  116.  
Parsed in 0.047 seconds, using GeSHi 1.0.8.4


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
markww
Freshman
Freshman
 
Posts: 9
Joined: Sun Feb 24, 2008 7:36 am

Postby KG200802 » Wed Feb 27, 2008 7:12 am

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.
KG200802
Freshman
Freshman
 
Posts: 3
Joined: Tue Feb 26, 2008 2:34 am

Postby KG200802 » Wed Feb 27, 2008 7:25 am

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
KG200802
Freshman
Freshman
 
Posts: 3
Joined: Tue Feb 26, 2008 2:34 am

Postby jessica » Wed Feb 27, 2008 9:39 am

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.
jessica
Freshman
Freshman
 
Posts: 4
Joined: Thu Dec 27, 2007 2:45 am

Postby markww » Wed Feb 27, 2008 5:57 pm

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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class SoundPlayer
  3.  
  4. {
  5.  
  6.         protected static boolean m_bSoundOn = true;
  7.  
  8.  
  9.  
  10.         /** Turn sound on/off. */
  11.  
  12.         public void setSoundOn(boolean bOn) {
  13.  
  14.                 m_bSoundOn = bOn;
  15.  
  16.         }
  17.  
  18.  
  19.  
  20.         protected static boolean m_bPlayingSound = false;
  21.  
  22.        
  23.  
  24.         /** Play an mp3 file. */
  25.  
  26.         public static void playSoundBlocking(Context context,
  27.  
  28.                                          String strFileName)
  29.  
  30.         {
  31.  
  32.                 if (!m_bSoundOn) {
  33.  
  34.                         return;
  35.  
  36.                 }
  37.  
  38.                
  39.  
  40.                 if (m_bPlayingSound) {
  41.  
  42.                         return;
  43.  
  44.                 }
  45.  
  46.                 m_bPlayingSound = true;
  47.  
  48.                
  49.  
  50.                 try {
  51.  
  52.                         MediaPlayer media = new MediaPlayer();
  53.  
  54.                         media.setDataSource("data/data/myapp/files/" + strFileName);
  55.  
  56.                         if (media != null){
  57.  
  58.  
  59.  
  60.                                 media.setOnPreparedListener(new
  61.  
  62.                                         MediaPlayer.OnPreparedListener(){
  63.  
  64.                                                 public void onPrepared(MediaPlayer mp) {
  65.  
  66.                                                         mp.start();
  67.  
  68.                                                 }
  69.  
  70.                                         });
  71.  
  72.                                 media.setOnCompletionListener(new
  73.  
  74.                                         MediaPlayer.OnCompletionListener() {
  75.  
  76.                                                 public void onCompletion(MediaPlayer mp) {
  77.  
  78.                                                         //mp.release();
  79.  
  80.                                                         m_bPlayingSound = false;
  81.  
  82.                                                 }
  83.  
  84.                                         });
  85.  
  86.                                 media.setOnErrorListener(new
  87.  
  88.                                         MediaPlayer.OnErrorListener() {
  89.  
  90.                                                 public void onError(MediaPlayer mp, int what, int extra) {
  91.  
  92.                                                         //mp.release();
  93.  
  94.                                                         m_bPlayingSound = false;
  95.  
  96.                                                 }
  97.  
  98.                                         });
  99.  
  100.  
  101.  
  102.                                 media.prepareAsync();
  103.  
  104.                         }                    
  105.  
  106.                 }
  107.  
  108.                 catch (IOException e ) {
  109.  
  110.                         context.showAlert("Error", 0, "Error playing mp3: [" + e.getMessage() + "].", "OK", true);
  111.  
  112.                 }
  113.  
  114.         }
  115.  
  116. }
  117.  
  118.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


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
markww
Freshman
Freshman
 
Posts: 9
Joined: Sun Feb 24, 2008 7:36 am

Postby JaydeRyu » Sun Mar 16, 2008 10:15 am

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.
JaydeRyu
Freshman
Freshman
 
Posts: 4
Joined: Sun Mar 16, 2008 10:10 am

Re: media streaming

Postby windkiosk » Tue Mar 18, 2008 8:19 am

fisherman wrote:hi, plusminus
thanks for your reply. i have another question about MediaPlayer, in
http://code.google.com/android/referenc ... layer.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.
windkiosk
Freshman
Freshman
 
Posts: 5
Joined: Fri Mar 07, 2008 2:03 am

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests