gpt4 book ai didi

android - 单击项目时 ListView 特定歌曲播放?

转载 作者:行者123 更新时间:2023-11-30 05:06:06 25 4
gpt4 key购买 nike

我正在创建 Mp3 播放器。我成功地从我的设备中获取歌曲名称和艺术家并将其显示在 ListView 中,现在我的问题是当任何用户点击特定项目时歌曲将播放如果之前播放任何歌曲它会自动停止并播放新歌曲用户是单击,我还想从我的 ListView 中获取歌曲名称和艺术家名称。

请帮我这里我所有的代码

package com.monstertechno.musicplayerappui;
import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.renderscript.Sampler;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
import com.sothree.slidinguppanel.SlidingUpPanelLayout.PanelState;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
private ArrayList<Song> songList;
private ListView songView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
runtimpermission();
songView = (ListView) findViewById(R.id.song_list);
songList = new ArrayList<Song>();
SongAdapter songAdt = new SongAdapter(this, songList);
songView.setAdapter(songAdt);
getSongList();
Collections.sort(songList, new Comparator<Song>() {
public int compare(Song a, Song b) {
return a.getTitle().compareTo(b.getTitle());
}
});

songView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(mediaPlayer!=null){
mediaPlayer.release();
}



}
});

}




public void runtimpermission(){

Dexter.withActivity(this)
.withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)

.withListener(new PermissionListener() {
@Override public void onPermissionGranted(PermissionGrantedResponse response) {
Toast.makeText(MainActivity.this, "Permisson Access", Toast.LENGTH_SHORT).show();

}
@Override public void onPermissionDenied(PermissionDeniedResponse response) {

Toast.makeText(MainActivity.this, "Permisson Denied", Toast.LENGTH_SHORT).show();
}
@Override


public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {


token.continuePermissionRequest();}
}).check();

}

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);
songList.add(new Song(thisId, thisTitle, thisArtist));
}
while (musicCursor.moveToNext());
}

}

}







package com.monstertechno.musicplayerappui;

public class Song {
private long id;
private String title;
private String artist;

public Song(long songID, String songTitle, String songArtist) {
id=songID;
title=songTitle;
artist=songArtist;
}

public long getId() {
return id;
}

public String getTitle() {
return title;
}

public String getArtist() {
return artist;
}
}



package com.monstertechno.musicplayerappui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class SongAdapter extends BaseAdapter {
private ArrayList<Song> songs;
private LayoutInflater songInf;

public SongAdapter(Context c, ArrayList<Song> theSongs){
songs=theSongs;
songInf=LayoutInflater.from(c);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return songs.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View arg1, ViewGroup parent) {
LinearLayout songLay = (LinearLayout)songInf.inflate
(R.layout.musiccustom,parent , false);
// TODO Auto-generated method stub
TextView songView = (TextView)songLay.findViewById(R.id.song_title);
TextView artistView = (TextView)songLay.findViewById(R.id.song_artist);
//get song using position
Song currSong = songs.get(position);
//get title and artist strings
songView.setText(currSong.getTitle());
artistView.setText(currSong.getArtist());
//set position as tag
songLay.setTag(position);
return songLay;


}
}

最佳答案

官方文档说:但是,当您调用 stop() 时,请注意您不能再次调用 start() 直到您再次准备 MediaPlayer。 https://developer.android.com/guide/topics/media/mediaplayer

所以您需要做的是在 ListView 的 onClick() 监听器中再次准备 MediaPlayer 实例(对象)。

MediaPlayer mediaPlayer = new 

MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url); //use it when your audio is in a network location
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();

为手机存储中存在的音频文件创建 URI:

(由于您已经有了指向音频文件所在文件夹的基本 Uri,因此您需要在其后附加文件名以获取每个文件的 Uri)

在列表项的onClickListener中写入以下几行(检查拼写),然后准备媒体播放器类

Uri fileUri = Uri.withAppendedPath(musicURI,"the name of audio file");
mediaPlayer.setDataSource(fileUri);
mediaPlayer.prepare(); //
mediaPlayer.start();

更新:搜索您的代码后,我发现您没有从光标获取文件路径。

目前,您只能获取 MediaStore.Audio.Media.TITLE、MediaStore.Audio.Media._IDMediaStore.Audio.Media.ARTIST

  • 您还需要MediaStore.Audio.Media.DATA 来获取音频文件的路径。因此,只需在您的 Song 类中添加另一个变量,该变量将保存来自光标的此文件路径。当你得到光标时添加这些行 -

    int dataColumn = musicCursor.getColumnIndex (android.provider.MediaStore.Audio.Media.DATA);//现在将其添加到歌曲列表

    String thisDataPath = musicCursor.getString(dataColumn);

现在,您可以通过以下方式轻松构建每个音频文件的 Uri-

Uri uri= Uri.parse("file:///"+song.getPath());

无需使用 Uri.withAppendPath()。只需使用上面的行,然后设置音乐播放器的数据源

关于android - 单击项目时 ListView 特定歌曲播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54575401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com