Android音乐播放器 - 一次一个PlayerActivity

时间:2017-03-16 15:55:29

标签: android android-intent android-activity android-music-player

我正在编写一个音乐播放器,我有两个活动:

  1. 第一个(MainActivity)包含歌曲的ListView,我设置了一个OnItemClickListener(在其onCreate()方法内),它打开一个名为PlayerActivity的新活动,传递已被点击的项目的位置: / p>

    public class MainActivity extends AppCompatActivity {
        Intent intent;
        ArrayList<String> artistsTitles;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            intent=new Intent(getApplicationContext(), PlayerActivity.class);
            artistsTitles=getArtistTitle();
            ListView mainList=(ListView) findViewById(R.id.mainList);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, artistsTitles);
            mainList.setAdapter(adapter);
            mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    intent.putExtra("currentSong", position);
                    startActivity(intent);
                }
            });
        }
    
        private ArrayList<String> getArtistTitle() {
            ContentResolver musicResolve = getContentResolver();
            String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
            String[] projection = { MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE};
            Uri uri=android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            Cursor cursor = musicResolve.query(uri, projection, selection, null, null);
    
            ArrayList<String> arrayList=new ArrayList<String>();
            while(cursor.moveToNext())
                arrayList.add(cursor.getString(0)+" - "+cursor.getString(1));
    
            return arrayList;
        }
    }
    
  2. 第二个(PlayerActivity)有一个歌曲字符串路径的ArrayList,并且使用MediaPlayer播放具有项目位置的歌曲并使用它访问ArrayList:

    public class PlayerActivity extends AppCompatActivity {
        ArrayList<String> songs;
        MediaPlayer mediaPlayer;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_player);
            songs=new ArrayList<String>();
            songs=getSongs();
            String songPath=songs.get(intent.getIntExtra("songID", 0));
            mediaPlayer=MediaPlayer.create(getApplicationContext(), Uri.parse(songPath));
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(getApplicationContext(), Uri.parse(songPath));
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        private ArrayList<String> getSongs() {
            ContentResolver musicResolve = getContentResolver();
    
            String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
            String[] projection = {MediaStore.Audio.Media.DATA};
            Uri uri=android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            Cursor cursor = musicResolve.query(uri, projection, selection, null, null);
    
            ArrayList<String> s = new ArrayList<String>();
            while(cursor.moveToNext())
                s.add(cursor.getString(0));
    
            return s;
        }
    }
    
  3. 它完美无缺,但当我回到第一个活动(MainActivity)然后我点击另一个项目(或相同)时,它会创建另一个PlayerActivity,因此该应用程序同时播放2首歌曲

    如何实现它以便它只创建一个PlayerActivity?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

解决!我试图通过Intent传递一个在MainActivity中创建的MediaPlayer实例,但似乎无法完成。所以,这是我的解决方案:

  1. 创建一个扩展Application并在那里实例化MediaPlayer的类,以便我有一个全局的MediaPlayer对象:

    import android.app.Application;
    import android.media.MediaPlayer;
    
    public class MyApp extends Application {
        MediaPlayer mediaPlayer=null;
    
        public MediaPlayer getMediaPlayer() {
            return mediaPlayer;
        }
    
        public void setMediaPlayer(MediaPlayer mediaPlayer) {
            this.mediaPlayer=mediaPlayer;
        }
    }
    
  2. 在AndroidManifest.xml中将MyApp设置为我的应用程序的名称:

    <application
    android:name="NicoPlayer"
    ...>
        ...
    </application>
    
  3. 在PlayerActivity中创建一个MediaPlayer实例,并将全局MediaPlayer实例分配给它调用getMediaPlayer(),所有这些都在onCreate()方法中。完成此操作并播放mediaPlayer后,将其分配给调用setMediaPlayer()的全局MediaPlayer实例:

    mediaPlayer=((NicoPlayer)this.getApplication()).getMediaPlayer();
    if(mediaPlayer==null)
        mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(songPath));
    ((NicoPlayer)this.getApplication()).setMediaPlayer(mediaPlayer);
    try {
        mediaPlayer.reset();
        mediaPlayer.setDataSource(getApplicationContext(), Uri.parse(songPath));
        mediaPlayer.prepare();
        mediaPlayer.start();
    } catch (IOException e) {
        e.printStackTrace();
    }