从列表视图中单击歌曲后,新活动正在打开但歌曲未播放
Posted
技术标签:
【中文标题】从列表视图中单击歌曲后,新活动正在打开但歌曲未播放【英文标题】:After clicking on song from the listview,new activity is opening but song is not playing 【发布时间】:2019-11-09 08:57:38 【问题描述】:我正在创建一个音乐播放器应用程序。我正在从设备获取歌曲列表,但是当我点击歌曲时,新活动正在打开,但歌曲没有播放,它只是打开活动而已
我多次检查检查代码但没有找到任何解决方案 如果我使用不同的方法来获取设备歌曲,那么它可以工作,但该方法无法找到所有歌曲,而只能找到几首歌曲。
播放列表活动
public class PlayListActivity extends AppCompatActivity
private static final int MY_PERMISSIONS_REQUEST_READ = 7;
private static final int REQUEST_PERMISSION = 5;
private static final int MY_PERMISSIONS_REQUEST_READ_MEDIA = 99;
public static ArrayList<TrackObjects> songsList;
private ListView songView;
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
songView = (ListView) findViewById(R.id.songlist);
songsList = new ArrayList<TrackObjects>();
songView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int
position, long l)
Intent i1 = new Intent(PlayListActivity.this, MainActivity.class);
i1.putExtra("pos", position);
i1.putExtra("songs", songsList);
startActivity(i1);
);
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
Collections.sort(songsList, new Comparator<TrackObjects>()
public int compare(TrackObjects a, TrackObjects b)
return a.getTitle().compareTo(b.getTitle());
);
TracksAdapter songAdt = new TracksAdapter(PlayListActivity.this, songsList);
songView.setAdapter(songAdt);
if (permissionCheck != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]Manifest.permission.WRITE_EXTERNAL_STORAGE, MY_PERMISSIONS_REQUEST_READ_MEDIA);
else
getSongList();
public void getSongList()
//retrieve song info
ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if (musicCursor != null && musicCursor.moveToFirst())
//get columns
int titleColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST);
//add songs to list
do
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
songsList.add(new TrackObjects(thisId, thisTitle, thisArtist));
while (musicCursor.moveToNext());
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
switch (requestCode)
case MY_PERMISSIONS_REQUEST_READ_MEDIA:
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED))
getSongList();
break;
default:
break;
MainActivity(歌曲播放活动):
public class MainActivity extends AppCompatActivity
static MediaPlayer mp;
int position;
SeekBar sb;
ArrayList mySongs;
Thread updateSeekBar;
ImageButton pause, forward, reverse, next, previous;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pause = (ImageButton) findViewById(R.id.pause);
forward = (ImageButton) findViewById(R.id.forward);
previous = (ImageButton) findViewById(R.id.previous);
next = (ImageButton) findViewById(R.id.next);
reverse = (ImageButton) findViewById(R.id.reverse);
sb = (SeekBar) findViewById(R.id.seekBar);
updateSeekBar = new Thread()
@Override
public void run()
int totalDuration = mp.getDuration();
int currentPosition = 0;
while (currentPosition < totalDuration)
try
sleep(500);
currentPosition = mp.getCurrentPosition();
sb.setProgress(currentPosition);
catch (InterruptedException e)
e.printStackTrace();
;
if (mp != null)
mp.stop();
mp.release();
Intent i = getIntent();
Bundle b = i.getExtras();
assert b != null;
mySongs = (ArrayList) b.getParcelableArrayList("songs");
position = b.getInt("pos",0);
Uri u = Uri.parse(mySongs.get(position).toString());
if (mp!=null)
mp= (MediaPlayer) MediaPlayer.create(MainActivity.this, u);
mp.start();
sb.setMax(mp.getDuration());
updateSeekBar.start();
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
@Override
public void onProgressChanged(SeekBar seekBar, int i,
boolean b)
@Override
public void onStartTrackingTouch(SeekBar seekBar)
@Override
public void onStopTrackingTouch(SeekBar seekBar)
mp.seekTo(seekBar.getProgress());
);
pause.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
sb.setMax(mp.getDuration());
if (mp.isPlaying())
pause.setBackgroundResource(R.mipmap.play_button);
mp.pause();
else
pause.setBackgroundResource(R.mipmap.pause);
mp.start();
);
forward.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
sb.setMax(mp.getDuration());
mp.seekTo(mp.getCurrentPosition() + 5000);
);
reverse.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
sb.setMax(mp.getDuration());
mp.seekTo(mp.getCurrentPosition() - 5000);
);
next.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
mp.stop();
mp.release();
position = ((position + 1) % mySongs.size());
Uri u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
);
previous.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
mp.stop();
mp.release();
position = ((position - 1) < 0) ? (mySongs.size() - 1) : (position - 1);
Uri u = Uri.parse(mySongs.get(
position).toString());//%mysongs so that it do not go to invalid position
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
);
TracksAdapter(歌曲列表适配器):
public class TracksAdapter extends BaseAdapter
private ArrayList<TrackObjects> tracks;
private LayoutInflater trackInf;
public TracksAdapter(Context c, ArrayList<TrackObjects> theTracks)
tracks = theTracks;
trackInf = LayoutInflater.from(c);
@Override
public int getCount()
return tracks.size();
@Override
public View getView(int position, View convertView, ViewGroup parent)
//map to song layout
@SuppressLint("ViewHolder") LinearLayout songLay = (LinearLayout) trackInf.inflate
(R.layout.songtracks, parent, false);
//get title and artist views
TextView trackView = (TextView) songLay.findViewById(R.id.track_title);
TextView artistView = (TextView) songLay.findViewById(R.id.track_artist);
//get song using position
TrackObjects currSong = tracks.get(position);
//get title and artist strings
trackView.setText(currSong.getTitle());
artistView.setText(currSong.getArtist());
//set position as tag
songLay.setTag(position);
return songLay;
@Override
public Object getItem(int position)
return null;
@Override
public long getItemId(int position)
return 0;
TrackObjects(对象类):
public class TrackObjects implements Serializable
private long id;
private String title;
private String artist;
public TrackObjects(long id, String title, String artist)
this.id = id;
this.title = title;
this.artist = artist;
protected TrackObjects(Parcel in)
id = in.readLong();
title = in.readString();
artist = in.readString();
public long getId()
return id;
public String getTitle()
return title;
public String getArtist()
return artist;
【问题讨论】:
检查您的歌曲列表是否在播放活动中。您已将 trackObject 声明为可序列化但检索为可包裹。此外,您只是将获取的 trackobject 传递给您的媒体播放器。相反,您应该传递您的歌曲网址 是的,我在播放列表活动中获取歌曲列表。如果我没有将 trackobject 声明为可序列化,那么它会显示错误,你能告诉我如何通过传递歌曲 url 从列表中获取选定的歌曲.. 检查下面发布的答案。 它在主要活动中没有得到那个 SongUrl,它的显示无法解析 getSongUrl,你能告诉我们应该在“songsList.add(new TrackObjects(thisId, thisTitle, thisArtist))中添加什么;”在播放列表活动中 【参考方案1】:问题是您没有将 url 传递给 MainActivity 中的媒体播放器。您的代码类似于:
mySongs = (ArrayList) b.getParcelableArrayList("songs");
position = b.getInt("pos",0);
Uri u = Uri.parse(mySongs.get(position).toString());
这里,
mySongs.get(position).toString();
它将从列表中返回TrackObjects
,使用toString()
将返回对象引用数据。所以最终你不会将任何 url 传递给媒体播放器。
解决方案:
在TrackObjects
类中添加一个新的字符串变量,例如private String songUrl;
,并使用下面的行而不是上面的行
Uri u = Uri.parse(mySongs.get(position).getSongUrl());
希望它能解决您的问题。
【讨论】:
它在主要活动中没有得到那个 SongUrl,它的显示无法解析 getSongUrl,你能告诉我们应该在“songsList.add(new TrackObjects(thisId, thisTitle, thisArtist))中添加什么;”在播放列表活动中 正如我所提到的,如果从本地存储中获取,您必须在模型类中添加 URI。现在要获取 URI,您可以使用简单的 Google 获取。您可以从***.com/questions/32337949/… 或programcreek.com/java-api-examples/… 获得基本的想法和帮助。 我认为我所做的与您提到的 *** 答案几乎相同,但在播放列表活动中,如果我使用不同的方法(arraylistsong.setPath(cursor.getString(2));
,如投票计数 4 所示。此外,如果您显示日志会很好对于您在 mp.create(Context, Uri)
方法中传递给 mediaplayer 的 URI。
我试过了,但没有帮助。我认为我遇到的问题在媒体播放器中的某个地方以上是关于从列表视图中单击歌曲后,新活动正在打开但歌曲未播放的主要内容,如果未能解决你的问题,请参考以下文章
单击另一个活动时如何发送自定义arraylist的列表项的数据?
如何在 android 的 mediaplayer 中停止和播放新歌曲?