【问题标题】:How to load mp3 files from RAW Folder instead of SDCARD如何从 RAW 文件夹而不是 SDCARD 加载 mp3 文件
【发布时间】:2014-10-01 16:37:37
【问题描述】:

您好,我正在查看本教程on the web:但是它有一些错误并且看起来不能正常工作。

我只是想了解如何从 SDCARD 的 RAW 文件夹本地加载我的 MP3 文件,如下面的代码所示。谢谢

public class SongsManager {
    // SDCard Path
    final String MEDIA_PATH = new String("/sdcard/");
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    // Constructor
    public SongsManager(){

    }

    /**
     * Function to read all mp3 files from sdcard
     * and store the details in ArrayList
     * */
    public ArrayList<HashMap<String, String>> getPlayList(){
        File home = new File(MEDIA_PATH);

        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                HashMap<String, String> song = new HashMap<String, String>();
                song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
                song.put("songPath", file.getPath());

                // Adding each song to SongList
                songsList.add(song);
            }
        }
        // return songs list array
        return songsList;
    }

    /**
     * Class to filter files which are having .mp3 extension
     * */
    class FileExtensionFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".mp3") || name.endsWith(".MP3"));
        }
    }
}

【问题讨论】:

    标签: java android arraylist mp3 media-player


    【解决方案1】:

    如你所愿

    >如何从 SDCARD 的 RAW 文件夹本地加载我的 MP3 文件

    答案是:

    MediaPlayer mplayer;    
    mplayer= MediaPlayer.create(this, R.raw.soundfile);// it is the file that you save in ur raw folder 
    mplayer.start();
    

    如果你想要完整的例子

    你可以试试这个代码:

    package com.hrupin.mp3player;
    
    import com.hrupin.mp3player.R;
    
    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnTouchListener;
    import android.widget.Button;
    import android.widget.SeekBar;
    
    public class Mp3player extends Activity {
    
        private Button buttonPlayStop;
        private MediaPlayer mediaPlayer;
        private SeekBar seekBar;
    
        private final Handler handler = new Handler();
    
        // Here i override onCreate method.
        //
        // setContentView() method set the layout that you will see then
        // the application will starts
        //
        // initViews() method i create to init views components.
        @Override
        public void onCreate(Bundle icicle) {
                super.onCreate(icicle);
                setContentView(R.layout.main);
                initViews();  
    
        }
    
        // This method set the setOnClickListener and method for it (buttonClick())
        private void initViews() {
            buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
            buttonPlayStop.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) {buttonClick();}});
    
            mediaPlayer = MediaPlayer.create(this, R.raw.testsong_20_sec); 
    
            seekBar = (SeekBar) findViewById(R.id.SeekBar01);
            seekBar.setMax(mediaPlayer.getDuration());
            seekBar.setOnTouchListener(new OnTouchListener() {@Override public boolean onTouch(View v, MotionEvent event) {
                seekChange(v);
                return false; }
            });
    
        }
    
        public void startPlayProgressUpdater() {
            seekBar.setProgress(mediaPlayer.getCurrentPosition());
    
            if (mediaPlayer.isPlaying()) {
                Runnable notification = new Runnable() {
                    public void run() {
                        startPlayProgressUpdater();
                    }
                };
                handler.postDelayed(notification,1000);
            }else{
                mediaPlayer.pause();
                buttonPlayStop.setText(getString(R.string.play_str));
                seekBar.setProgress(0);
            }
        } 
    
        // This is event handler thumb moving event
        private void seekChange(View v){
            if(mediaPlayer.isPlaying()){
                SeekBar sb = (SeekBar)v;
                mediaPlayer.seekTo(sb.getProgress());
            }
        }
    
        // This is event handler for buttonClick event
        private void buttonClick(){
            if (buttonPlayStop.getText() == getString(R.string.play_str)) {
                buttonPlayStop.setText(getString(R.string.pause_str));
                try{
                    mediaPlayer.start();
                    startPlayProgressUpdater(); 
                }catch (IllegalStateException e) {
                    mediaPlayer.pause();
                }
            }else {
                buttonPlayStop.setText(getString(R.string.play_str));
                mediaPlayer.pause();
            }
        }
    

    希望这会有所帮助...:)

    【讨论】:

      【解决方案2】:

      raw 资源ID 与您的媒体播放类一起使用,例如MediaPlayer。因此,例如,如果您有 res/raw/resources_are_not_files.mp3,您将使用 R.raw.resources_are_not_files 来引用该 MP3 以获取像 create() on MediaPlayer 这样的方法。

      【讨论】:

      • 一个疑问......如果我们在原始文件夹中加载 .mp3 文件,这将反映在 .apk 大小中。?
      • @S3a2r0a8v9a6n9a:是的,打包在 APK 中的 MP3 文件会增加 APK 的大小。由于 MP3 已经是一种压缩格式,因此 APK 的大小将增加大约 MP3 文件本身的大小。因此,这种技术主要适用于短音效等,而不是数小时的音乐。
      【解决方案3】:

      将mp3文件放在raw文件夹中,这样使用

      MediaPlayer mplayer;
      mplayer= MediaPlayer.create(this, R.raw.yoursound);// create & refer the id
      mplayer.start();//will play the file
      

      更多详情请查看此链接http://www.tutorialspoint.com/android/android_mediaplayer.htm

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-19
        • 2016-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多