简体   繁体   English

如何从RAW文件夹而不是SDCARD加载mp3文件

[英]How to load mp3 files from RAW Folder instead of SDCARD

Hello I was looking at this tutorial on the web : But it has some bugs and dont seen to be working right. 您好,我在网上观看本教程:但是它存在一些错误,并且无法正常运行。

I just wanted to find out, how to load my MP3 files locally from the RAW Folder insted of the SDCARD as in the code below. 我只是想了解如何从SDCARD插入的RAW文件夹本地加载我的MP3文件,如下面的代码所示。 thanks 谢谢

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"));
        }
    }
}

As you Are Asking for 当您要求

> How to load my MP3 files locally from the RAW Folder insted of the SDCARD >如何从SDCARD插入的RAW文件夹本地加载我的MP3文件

Ans is: Ans是:

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

If you Want complete example 如果您想要完整的示例

You Can Try This code: 您可以尝试以下代码:

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();
        }
    }

Hope this helps...:) 希望这可以帮助...:)

Use raw resource IDs with your media playback class, like MediaPlayer . 在媒体播放类(如MediaPlayer使用raw资源ID。 So, for example, if you have res/raw/resources_are_not_files.mp3 , you would use R.raw.resources_are_not_files to reference that MP3 for methods like create() on MediaPlayer . 因此,例如,如果您有res/raw/resources_are_not_files.mp3 ,则可以使用R.raw.resources_are_not_files MediaPlayer为诸如create()类的方法引用该MP3。

Place the mp3 files in raw folder and use this way 将mp3文件放在原始文件夹中,然后使用这种方式

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

For more detail check this link http://www.tutorialspoint.com/android/android_mediaplayer.htm 有关更多详细信息,请检查此链接http://www.tutorialspoint.com/android/android_mediaplayer.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果第一个完成然后第二个将开始,如何播放原始文件夹中的所有 mp3 文件 - how to play all mp3 files from raw folder if first is complete then second will be start 如何将原始文件夹中的所有mp3文件按名称和其他元数据放入列表中? - How to put all mp3 files from raw folder into a list by name and other metadata? 从原始文件夹中读取的mp3文件 - mp3 file read from raw folder 如何将Raw文件夹中的mp3 / mp4文件发送到Android上的服务器? - How to send the mp3/mp4 file from Raw folder to server on Android? Android App /可以从Raw或Assets文件夹而不是/ SDCard /播放音频/视频文件 - Android App/ to paly audio/videos files from Raw or Assets folder instead of /SDCard/ 获取文件夹名称而不是.mp3文件{Android} - Getting Folder names instead of .mp3 files {Android} Android MediaPlayer-如何从原始文件夹加载加载多个文件 - Android MediaPlayer - how load load multiple files from raw folder 如何将原始 mp3 从 recyclerview 项目传递到另一个活动 - How to pass raw mp3 from recyclerview item to another activity 如何播放 Jar 文件中资源文件夹中的 MP3 文件? - How to play MP3 files that are in the resources folder in a Jar file? Android Studio:将原始文件夹中的 mp3 设置为 2020 年的设备铃声 - Android Studio: Set mp3 from raw folder as device ringtone in 2020
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM