Load mp3 from url with progressBar Android Java


Xml Code: activty_main.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   tools:context=".MainActivity">  
   
   <Button  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="ply button"  
     android:id="@+id/btnPlay"  
     />  
   
   <ProgressBar  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:id="@+id/progressBar"  
     android:layout_centerInParent="true"  
     android:visibility="gone"  
     />  
   
 </RelativeLayout>  

Java Code: MainActivity.java

 public class MainActivity extends AppCompatActivity {  
   MediaPlayer mediaPlayer;  
   ProgressBar progressBar;  
   Button btnPlay;  
   String musicUrl = "Your Mp3 Url";  
     
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
   
     btnPlay = findViewById(R.id.btnPlay);  
     progressBar = findViewById(R.id.progressBar);  
   
     mediaPlayer = new MediaPlayer();  
     mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
   
     btnPlay.setOnClickListener(v -> {  
       new LoadMusicFromUrl().execute(musicUrl);  
     });  
       
   } // onCreate method end here =============  
     
   private class LoadMusicFromUrl extends AsyncTask<String,Integer,Boolean>{  
   
     @Override  
     protected Boolean doInBackground(String... strings) {  
       try {  
         mediaPlayer.setDataSource(strings[0]);  
         mediaPlayer.prepare();;  
         return true;  
       } catch (Exception e){  
         e.printStackTrace();  
         return false;  
       }  
     }  
   
     @Override  
     protected void onPreExecute() {  
       super.onPreExecute();  
       progressBar.setVisibility(View.VISIBLE);  
     }  
   
     @Override  
     protected void onPostExecute(Boolean success) {  
       super.onPostExecute(success);  
       progressBar.setVisibility(View.GONE);  
       if (success){  
         mediaPlayer.start();  
       } else {  
         Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();  
       }  
     }  
   } // LoadMusicFromUrl end here =============  
     
   @Override  
   protected void onDestroy() {  
     if (mediaPlayer != null){  
       mediaPlayer.stop();  
     }  
     super.onDestroy();  
   }  
 } // public class end here ====================  

Android Internet Permissions: AndroidManifest.xml

   <uses-permission android:name="android.permission.INTERNET"/>  
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>  

Video:


Next Post Previous Post
No Comment
Add Comment
comment url