简体   繁体   中英

Android Raw Folder Media Player

I have two activity Play.java and Home.java,Home.java contain onclick function for Listview and get the position of listview I Need to Pass that position to Play.java.when i click listview "unfortunately app closed"

Home.java

public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
{     
    int songindex = position;
    Intent intent = new Intent(this, Play.class);
    startActivity(intent);

    p1.listen(songindex);
}

Play.java

public void change(View v)
    {
        Intent intent = new Intent(this, Home.class);
        startActivity(intent);
    }

    public void listen(int songindex)
    {
    MediaPlayer mPlayer2;
     if(songindex==0)
     {
        mp=MediaPlayer.create(this, R.raw.gayatri);
        mp.start();



     }
     else if(songindex==1)
     {
         mPlayer2= MediaPlayer.create(this, R.raw.brahma);
         mPlayer2.start(); 
     }
    }

when i click song from listview its not working app closed

You have to pass position using intent.

Home.java

public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
{     
    int songindex = position;
    Intent intent = new Intent(this, Play.class);
    intnt.putExtra("position",position);
    startActivity(intent);

  //  p1.listen(songindex);
}
Play.java

public void change(View v)
    {
        Intent intent = new Intent(this, Home.class);
        startActivity(intent);
    }

    public void listen(int songindex)
    {
     Bundle data = getIntent().getExtras();
         int position = data.getInt("position");

    MediaPlayer mPlayer2;
     if(songindex==0)
     {
        mp=MediaPlayer.create(this, R.raw.gayatri);
        mp.start();



     }
     else if(songindex==1)
     {
         mPlayer2= MediaPlayer.create(this, R.raw.brahma);
         mPlayer2.start(); 
     }
    }

Put listen function in main file

Home.java

public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
{     
    listen(position);
}


    public void listen(int songindex)
    {



    MediaPlayer mPlayer2;
     if(songindex==0)
     {
        mp=MediaPlayer.create(this, R.raw.gayatri);
        mp.start();



     }
     else if(songindex==1)
     {
         mPlayer2= MediaPlayer.create(this, R.raw.brahma);
         mPlayer2.start(); 
     }
    }

LogCat should bring light to you problem, but we still don't have it.

Then why do you need mp and mp2? To play two songs at once?

I would just have one mp, declared in the class, not in the method, because in you case the refrence to mp2 player is lost after exiting from the listen method, and there is no way to control it (in fact, it may even stop playing after exiting from the method).

To make in short, this is what I suggest:

MediaPlayer mp; 

static final int table[] songIndexIds= { R.raw.song1, R.raw.song2};


public void listen(int songIndex)
     if (mp != null) {
        mp.stop();
        mp.release();
        mp = null;
     }

     if (songIndex >= 0) {
         mp=MediaPlayer.create(this, songIndexIds[songIndex]);
         mp.start();
     }

}

// "Destructor"
@Override 
public void finalize() {
     if (mp != null) {
        mp.stop();
        mp.release();
     }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM