【发布时间】:2016-10-31 13:48:10
【问题描述】:
不幸的是,我没有意识到 SoundPool 只会播放大约 5 秒的任何文件,我正在尝试播放比这更大但已经使用 SoundPool 完成大部分代码的文件。所以我想知道如何轻松地将我的代码从使用 SoundPool 更改为 MediaPlayer?我主要担心的是,对于 SoundPool,我使用了一个名为 sample_music 的资产子文件中的所有文件,而 MediaPlayer 似乎无法做到这一点。任何帮助将非常感激!谢谢。
这是我使用 SoundPool 的两个主要文件:
public class Featured {
private static final String TAG = "Featured";
private static final String MUSIC_FOLDER = "sample_music";
private static final int MAX_SONGS = 1; //One song can be played at a time
private AssetManager mAssets;
private List<Song> mSongs = new ArrayList<>();
private SoundPool mSoundPool;
public Featured(Context context) {
mAssets = context.getAssets();
mSoundPool = new SoundPool(MAX_SONGS, AudioManager.STREAM_MUSIC, 0);
loadFeatured();
}
public void play(Song song) {
Integer songId = song.getSongId();
if (songId == null) { return; } //No song
mSoundPool.play(songId, 1.0f, 1.0f, 1, 0, 1.0f);
}
private void loadFeatured() {
String[] songTitles;
try {
songTitles = mAssets.list(MUSIC_FOLDER);
Log.i(TAG, "Found " + songTitles.length + " songs");
} catch (IOException ioe) {
Log.e(TAG, "Could not list assets", ioe);
return;
}
//Build a list of songs
for (String filename : songTitles){
try {
String assetPath = MUSIC_FOLDER + "/" + filename;
Song song = new Song(assetPath);
load(song);
mSongs.add(song);
} catch (IOException ioe) {
Log.e(TAG, "Could not load song " + filename, ioe);
}
}
}
//Load songs
public void load(Song song)throws IOException {
AssetFileDescriptor afd = mAssets.openFd(song.getAssetPath());
int songId = mSoundPool.load(afd, 1); //load for later playback
song.setSongId(songId);
}
public List<Song> getSongs() {
return mSongs;
}
}
还有另一个文件:
public class FeaturedFragment extends Fragment {
private Featured mFeatured;
public static FeaturedFragment newInstance() {
return new FeaturedFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFeatured = new Featured(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Load recycler view of featured songs
View view = inflater.inflate(R.layout.fragment_featured, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.fragment_featured_recycler_view);
//set 3 songs per row
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
recyclerView.setAdapter(new SoundAdapter(mFeatured.getSongs()));
return view;
}
private class SoundHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private Button mButton;
private Song mSong;
public SoundHolder(LayoutInflater inflater, ViewGroup container) {
super(inflater.inflate(R.layout.list_item_sound, container, false));
mButton = (Button)itemView.findViewById(R.id.list_item_sound_button);
mButton.setOnClickListener(this);
}
public void bindSong(Song song) {
mSong = song;
mButton.setText(mSong.getTitle());
}
@Override
public void onClick(View v) {
mFeatured.play(mSong);
}
}
private class SoundAdapter extends RecyclerView.Adapter<SoundHolder> {
private List<Song> mSongs;
public SoundAdapter(List<Song> songs) {
mSongs = songs;
}
private MediaPlayer mMediaPlayer;
@Override
public SoundHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
return new SoundHolder(inflater, parent);
}
@Override
public void onBindViewHolder(SoundHolder soundHolder, int position) {
Song song = mSongs.get(position);
soundHolder.bindSong(song);
}
@Override
public int getItemCount() {
return mSongs.size();
}
}
感谢您的帮助!
【问题讨论】:
标签: android-studio android-mediaplayer soundpool