-3

I working on a music app in which user is getting mp3 file from WebService Api. When I am selecting any song in adapter class, I am getting its selectionPositions value and sending it in next activity(PlaySong) using Intent. In next activity which is actually having a layout of music player starts playing that song.

Here is my Adapter class.

 public class Albumnlistadapter  extends BaseAdapter {
    Context cxt;
    ArrayList<Albumnlistdata>ARR;
    private MediaPlayer mediaPlayer;
    public TextView songName, duration;
    private double timeElapsed = 0, finalTime = 0;
    private int forwardTime = 2000, backwardTime = 2000;
    private Handler durationHandler = new Handler();
    private SeekBar seekbar;
    public  String st,satic;
    
    public String url;
    private int selectedPosition;
    private static final String TAG_MESSAGES = "messages";
    
    public SharedPreferences sharedpreferences;
    
    String filenameee;
       
    public Albumnlistadapter(Context cxt,ArrayList<Albumnlistdata>ARR) {
        
        this.cxt=cxt;
        this.ARR=ARR;
        
    }
    
    @Override
    public int getCount() {
        
        return ARR.size();
    }

    @Override
    public Object getItem(int position) {
        
        return position;
    }

    @Override
    public long getItemId(int position) {
        
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        
        View v=convertView;
        if(v==null)
        {
            v=View.inflate(cxt, R.layout.songslistitem, null);
        }
        TextView tvaudioName=(TextView)v.findViewById(R.id.TEXT1);
        LinearLayout laySong = (LinearLayout)v.findViewById(R.id.lvsong);
        tvaudioName.setText(ARR.get(position).strgeneralid);
    
    
        sharedpreferences = cxt.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        satic ="http://sabakuch.org/public/uploads/tracks/";
         
        laySong.setOnClickListener(new OnClickListener() {
            
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            filenameee = ARR.get(position).strsongfileid;
            
            
            
            Toast.makeText(cxt, filenameee, Toast.LENGTH_SHORT).show();
            
            String PlaySongPath =satic+filenameee;
        
             Intent i =new Intent(cxt,Playsong.class);
             i.putExtra("path", PlaySongPath);
             i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             cxt.startActivity(i);
    
        }
        
    }); 
     
        return v;
    }
}

here is my next activity in which I am playing the song.

   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   
    //set the layout of the Activity
    setContentView(R.layout.songsplay);
    PATH =getIntent().getExtras().getString("path");
    NAME =getIntent().getExtras().getString("name");
    
    mediaPlayer = MediaPlayer.create(Playsong.this, Uri.parse(PATH));
    finalTime = mediaPlayer.getDuration();
    //initialize views
    initializeViews();
}

public void initializeViews(){
    
      b1 = (ImageView) findViewById(R.id.backButton);
 
      b3=(ImageView)findViewById(R.id.playButton);
      b4=(ImageView)findViewById(R.id.nextButton);
   
      if(Index==0){
          
         b3.setVisibility(View.VISIBLE);
         //b2.setVisibility(View.GONE);
         
        }else if(Index==1){
            
            b3.setVisibility(View.GONE);
            //b2.setVisibility(View.VISIBLE);
            
        }
    
    //mediaPlayer = MediaPlayer.create(this, Uri.parse(PATH));
    
//  finalTime = mediaPlayer.getDuration();
      
      b3.setOnClickListener(new View.OnClickListener() {
         @SuppressLint("NewApi") @Override
         public void onClick(View v) {
             
                Index=1;
                changeimageButton();
              
                mediaPlayer.start();
                finalTime = mediaPlayer.getDuration();
           
             
         }
          });
     
      
      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            int temp = (int)startTime;
            
            if((temp+forwardTime)<=finalTime){
               startTime = startTime + forwardTime;
               mediaPlayer.seekTo((int) startTime);
              
               Toast.makeText(getApplicationContext(),"You have Jumped forward 5 seconds",Toast.LENGTH_SHORT).show();
            }
            else{
               Toast.makeText(getApplicationContext(),"Cannot jump forward 5 seconds",Toast.LENGTH_SHORT).show();
            }
         }
      });
      
      b4.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            int temp = (int)startTime;
            
            if((temp-backwardTime)>0){
               startTime = startTime - backwardTime;
               mediaPlayer.seekTo((int) startTime);
               Toast.makeText(getApplicationContext(),"You have Jumped backward 5 seconds",Toast.LENGTH_SHORT).show();
            }
            else{
               Toast.makeText(getApplicationContext(),"Cannot jump backward 5 seconds",Toast.LENGTH_SHORT).show();
            }
         }
      });
   }
   
   protected void changeimageButton() {
       
       if(Index==0){
         b3.setVisibility(View.VISIBLE);
         
        }else if(Index==1){
            
            b3.setVisibility(View.GONE);
        }
}

private Runnable UpdateSongTime = new Runnable() {
      public void run() {
         startTime = mediaPlayer.getCurrentPosition();
      
         myHandler.postDelayed(this, 100);
      }
   };
    
   
   @Override
    public void onBackPressed ()
    {
      if (mediaPlayer != null)
          mediaPlayer.stop();
      super.onBackPressed();
    }
   
   @Override
   public void onPause ()
   {
     if (mediaPlayer != null)
     {
         mediaPlayer.pause();
         mediaPlayer.stop();
     }
     super.onPause();
   }

}

I am not having issue till now, But when I want to change the song. I guess if could be able to change the selectedPosition in previous adapter class, I will be able to get the next and previous song from the api. How can I do that?? please help me. Thanks in advance.

Community
  • 1
  • 1
Devraj
  • 1,479
  • 1
  • 22
  • 42
  • I just need know that why you down voted this question. just want to know, don't have any objection. But it is a humble request to you that please help me to sort it out.. :) – Devraj Jan 27 '16 at 12:41
  • [this](http://stackoverflow.com/questions/2749893/how-to-use-broadcast-receiver-in-different-applications-in-android) approach will help you. :) – Aks4125 Jan 27 '16 at 12:48
  • make public static arraylist and use that in next screen with its position and there, you can change or refresh its value. – Sagar Maiyad Jan 27 '16 at 12:48
  • @Akashbhatt-Aks4125- thank you Akash, Let me check your link. I will get back to you then.. :) – Devraj Jan 27 '16 at 12:56
  • @Devraj It's just a concept. for more detail search for "BroadcastReceiver" in Android. Easiest way to send/receive data from xyz-to-xyz. :) – Aks4125 Jan 27 '16 at 13:00
  • @Akashbhatt-Aks4125- yes I understand Sir. I need to ask one thing that can we use StartActivityForResult() here??? – Devraj Jan 27 '16 at 13:03

2 Answers2

0

I have resolved this issue, the logic behind that is I clicked an item in Songlist in Activity1 and get the selectedposition. I parse that selected Songlist value in Activity2. After that I got the same Songlist in Activity2 too. Using for-Loop, I just compared Songlist with that selectedposition. By this, I got the current value of selected item, set that index in musicPlayer. when I select next and previous song, I increase and decrease the current index and got the item of current index.

Devraj
  • 1,479
  • 1
  • 22
  • 42
-1

pass the whole ArrayListARR in the intent with the selected position. Do anything you want now. You can use reflection to pass the arraylist in the form of String or make your Albumnlistdata parcelable.

Suhaib Roomy
  • 2,501
  • 1
  • 16
  • 22
  • It could be comment. – Sagar Maiyad Jan 27 '16 at 12:48
  • thanks for the quick reply brother. I thought about this too but I got stuck when I had to compare selected position of previous list and new parsed list. can you please help me in this?? So we can try. – Devraj Jan 27 '16 at 12:55
  • didnt get what you mean. pass the list as well as the current position. please elaborate your issue, why do you have to compare the selected position of previous list the new parsed list – Suhaib Roomy Jan 27 '16 at 13:47