How to Play Web Address URL to Media Player in and

2019-12-16 21:09发布

问题:

My URL contains number medial related URLs which we have to play in media player. When i am passing my any media url with extension .mp3 or .mp4. it plays but whn i am passing any url that holds list of URLs then it is not playing in medi payer. Please tell me how can i resolve this issue ?

回答1:

this will help you:

private void PlayOnlineUrl() {  
            String url = "http://www.gaana.mp3"; // your URL here  
            MediaPlayer mediaPlayer = new MediaPlayer();  
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
            try {  
                 mediaPlayer.setDataSource(url);  
            } catch (IllegalArgumentException e) {  
                 e.printStackTrace();  
            } catch (SecurityException e) {  
                 e.printStackTrace();  
            } catch (IllegalStateException e) {  
                 e.printStackTrace();  
            } catch (IOException e) {  
                 e.printStackTrace();  
            }  
            mediaPlayer.prepareAsync();  
            // You can show progress dialog here untill it prepared to play  
            mediaPlayer.setOnPreparedListener(new OnPreparedListener() {  
                 @Override  
                 public void onPrepared(MediaPlayer mp) {  
                      // Now dismis progress dialog, Media palyer will start playing  
                      mp.start();  
                 }  
            });  
            mediaPlayer.setOnErrorListener(new OnErrorListener() {  
                 @Override  
                 public boolean onError(MediaPlayer mp, int what, int extra) {  
                      // dissmiss progress bar here. It will come here when  
                      // MediaPlayer  
                      // is not able to play file. You can show error message to user  
                      return false;  
                 }  
            });  
       }