音乐播放器同时播放2首歌曲

时间:2014-06-08 04:10:01

标签: android listview android-activity

我是初学者,我的Android应用程序遇到了一些麻烦。我的应用程序有2个活动。第一个是PlayerActivity.java,它会列出一个listView中sdcard中的所有歌曲,当我点击一个项目时,PlaysongActivity就会出现。但是当我按下后退按钮时,我会显示歌曲的列表视图再次,但当我再次点击一个项目然后它打开一个新的活动,而旧的仍在播放。现在我有2首歌在同一时间播放。我怎么能解决这个麻烦?这是我的代码。

public class PlayerActivity extends Activity {

    String pathToFile = new String("");
    private List song = new ArrayList();
    ListView listSong;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listSong = (ListView) findViewById(R.id.list);

        listAllSong();
    }

    private void listAllSong() {

        String end = ".mp3";
        File myMp3Dir = new File("/sdcard/");
        final File[] filelist = myMp3Dir.listFiles();
        for (File f : filelist) {
            if (!f.isDirectory()) {
                if (f.getName().toLowerCase().contains(end)) {
                    song.add(f.getName());
                }
            }

        }
        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, song);
        listSong.setAdapter(adapter);
        listSong.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String path = parent.getItemAtPosition(position).toString();
                Intent myIntent = new Intent(getApplicationContext(),
                        PlaysongActivity.class);
                myIntent.putExtra("pathOfSong", path);

                startActivity(myIntent);
                finish();
            }
        });

    }


    public boolean onCreateOptionsMenu(Menu menu) {


        getMenuInflater().inflate(R.menu.player, menu);
        return true;
    }


    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }


        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.main, container, false);
            return rootView;
        }
    }

}

/和PlaysongActivity /

public class PlaysongActivity extends Activity {

    MediaPlayer mp1;
    SeekBar seekBar;
    Boolean isPaused=false;
    Handler mHandler=new Handler();
    Boolean isMovingSeekBar=false;
    ImageButton button;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playsong);
        TextView tvPlaysong=(TextView) findViewById(R.id.tv);
        mp1=new MediaPlayer();
        seekBar=(SeekBar) findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {


            public void onStopTrackingTouch(SeekBar seekBar) {
                isMovingSeekBar = false;

            }


            public void onStartTrackingTouch(SeekBar seekBar) {
                isMovingSeekBar = true;

            }


            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                if (isMovingSeekBar) {
                    mp1.seekTo(progress);
                }

            }
        });
        button=(ImageButton) findViewById(R.id.btnPlay);
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if (mp1.isPlaying()) {
                    mp1.pause();
                    button.setImageResource(android.R.drawable.ic_media_play);
                } else if (isPaused) {
                    mp1.start();
                    button.setImageResource(android.R.drawable.ic_media_pause);
                }

            }
        });
        Intent i=getIntent();
        String path=i.getStringExtra("pathOfSong");
        tvPlaysong.setText(path);
        if(mp1.isPlaying()) mp1.stop();
        playSong(path);
        }


    private void playSong(String path) {
        seekBar.setProgress(0);

        if(mp1!=null) mp1.stop();
        try {
            mp1.setDataSource("/sdcard/" + path);
            mp1.prepare();
            mp1.start();
            seekBar.setMax(mp1.getDuration());
            updateSeekBar();

            isPaused = true;
        } catch (IllegalArgumentException e) {

            e.printStackTrace();
        } catch (SecurityException e) {

            e.printStackTrace();
        } catch (IllegalStateException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
        button.setImageResource(android.R.drawable.ic_media_pause);
    }
    private void updateSeekBar() {

        mHandler.postDelayed(getTimeTask, 1000);

    }
    private Runnable getTimeTask=new Runnable() {


        public void run() {
        long duration=mp1.getDuration();
            long currentPosition=mp1.getCurrentPosition();
            seekBar.setProgress((int) currentPosition);
            mHandler.postDelayed(this, 1000);

        }
    };




    public boolean onCreateOptionsMenu(Menu menu) {


        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.playsong, menu);
        return true;
    }


    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }


        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_playsong,
                    container, false);
            return rootView;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

PlaysongActivity中,覆盖onStop()并致电MediaPlayer上的release()

退出该活动时,这将停止播放(并释放资源)。