Android Studio 中的 SoundPool 到 MediaPlayer

Posted

技术标签:

【中文标题】Android Studio 中的 SoundPool 到 MediaPlayer【英文标题】:SoundPool to MediaPlayer in Android Studio 【发布时间】: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();
    

感谢您的帮助!

【问题讨论】:

【参考方案1】:

使用 SoundPool,您确实会调用 load() 方法,它会为您希望稍后播放的每个音频文件分配一个 id。使用 MediaPlayer,您只需在列表中添加绝对路径。每当您想播放歌曲时。只需从列表中选择一首歌曲,将其传递给 MediaPlayer 的静态 create() 方法,然后调用 start() 方法即可开始播放音频文件。这是一个例子

   public void PlaySong(Context context, string songToPlay) 

   MediaPlayer player = MediaPlayer.create(context,Uri.parse(songToPlay));

   if (player != null) 
       player.start() 
   

就是这样。希望对您过渡到使用 MediaPlayer 有所帮助。

【讨论】:

以上是关于Android Studio 中的 SoundPool 到 MediaPlayer的主要内容,如果未能解决你的问题,请参考以下文章

大神, 我的android studio中的虚拟机怎么联网?

android studio怎样运行打包后的apk

无法按照 Google“入门”页面中的说明在 Android Studio 中添加地图; Android Studio 报错

android studio 怎么添加module

android studio application怎么创建

android - android studio中的proguard错误