在其他活动

时间:2015-11-07 10:52:21

标签: android android-fragments android-intent android-activity android-mediaplayer

这是我检索存储中所有歌曲列表的地方:

enter code here公共类MainActivity扩展了AppCompatActivity {

public static final long pos=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView audioView = (ListView) findViewById(R.id.songView);

    ArrayList<String> audioList = new ArrayList<>();

    String[] proj = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DURATION};// Can include more data for more details and check it.

    final Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);

    if(audioCursor != null){
        if(audioCursor.moveToFirst()){
            do{
                int audioIndex = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);

                audioList.add(audioCursor.getString(audioIndex));
            }while(audioCursor.moveToNext());
        }
    }
    audioCursor.close();

    ArrayAdapter<String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,android.R.id.text1, audioList);
    audioView.setAdapter(adapter);

    audioView.setOnClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            Intent now_play=new Intent(MainActivity.this, NowPlaying.class);
            now_play.putExtra(pos, id);
            startActivity(now_play);
        }
    }

}

如何让意图通过哪首歌被选中,然后如何让这段代码播放该文件?

public class NowPlaying extends AppCompatActivity implements View.OnClickListener{

private ImageButton bplay;
private ImageButton bpause;
private ImageButton bfr;
private ImageButton bff;
private ImageButton breplay;
MediaPlayer mp;
SeekBar sb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(layout.now_playing);
    initializeVariables();

    bplay.setOnClickListener(this);
    bpause.setOnClickListener(this);
    bff.setOnClickListener(this);
    bfr.setOnClickListener(this);

    sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int progress=0;

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            this.progress=progress;
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    breplay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mp.setLooping(true);
        }
    }

}
}


@Override
public void onClick(View v) {
    switch(v) {
        case id.play:
            mp.start();
            break;
        case id.pause:
            mp.pause();
            break;
        case id.ff:
            mp.reset();
}

}     private void initializeVariables(){

    bplay=(ImageButton)findViewById(id.play);
    bff=(ImageButton)findViewById(id.ff);
    bfr= (ImageButton) findViewById(id.fr);
    bpause=(ImageButton)findViewById(id.pause);
    breplay=(ImageButton)findViewById(id.replay);
    mp=new MediaPlayer();
    sb=(SeekBar)findViewById(id.seekBar);
}

}

2 个答案:

答案 0 :(得分:1)

  

在不同的活动中播放所选歌曲

获取所选歌曲的URI,然后转到下一个想要播放的活动。

这样做:

1。使用Audio.Media.DATA获取歌曲文件的路径与获取DISPLAY_NAME的方式相同:

ArrayList<String> audioFilePathList = new ArrayList<>();
String path =audioCursor.getString(audioCursor.getColumnIndex(Audio.Media.DATA));
audioFilePathList.add(path);

2。onItemClick方法中使用positionaudioFilePathList获取所选项目文件路径:

String strSelectedPath=audioFilePathList.get(position);
now_play.putExtra(pos, id);
now_play.putExtra("file_path", strSelectedPath);
startActivity(now_play);

3。NowPlaying类中从Intent获取文件路径并将其传递给MediaPlayer以进行播放:

String file_path = getIntent().getStringExtra("file_path");

如何使用创建

在原始文件夹中播放音频文件

这样做:

MediaPlayer mPlayer = MediaPlayer.create(NowPlaying.this, R.raw.soundfilename);
mPlayer.start();

答案 1 :(得分:0)

您的位置错误可以通过以下代码简单地解决:

now_play.putExtra("pos",id)