android 简单的媒体播放器播放mp3从文件路径?

xriantvc  于 2022-12-28  发布在  Android
关注(0)|答案(7)|浏览(249)

我有一个非常简单的播放背景的媒体播放器。它从APK调用文件,但我希望它从任何目录播放,如音乐或SD卡。
下面是我的代码:

private MediaPlayer mpintro;

.
.

mpintro = MediaPlayer.create(this, R.raw.intro);
        mpintro.setLooping(true);
        mpintro.start();
ukxgm1gy

ukxgm1gy1#

它是这样工作的:

mpintro = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Music/intro.mp3"));
mpintro.setLooping(true);
        mpintro.start();

它不能作为字符串文件路径正常工作...

ha5z0ras

ha5z0ras2#

String filePath = Environment.getExternalStorageDirectory()+"/yourfolderNAme/yopurfile.mp3";
mediaPlayer = new  MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();   
mediaPlayer.start()

和原始文件夹中的此播放。

int resID = myContext.getResources().getIdentifier(playSoundName,"raw",myContext.getPackageName());

            MediaPlayer mediaPlayer = MediaPlayer.create(myContext,resID);
            mediaPlayer.prepare();
            mediaPlayer.start();

**我的上下文=应用程序. this.**使用.

e7arh2l6

e7arh2l63#

下面是设置MediaPlayer以播放SD卡的代码:

String PATH_TO_FILE = "/sdcard/music.mp3";    
mediaPlayer = new  MediaPlayer();
mediaPlayer.setDataSource(PATH_TO_FILE);
mediaPlayer.prepare();   
mediaPlayer.start()

你可以看到完整的例子here。让我知道如果你有任何问题。

tzdcorbm

tzdcorbm4#

使用下面的代码,它为我工作.

MediaPlayer mp = new MediaPlayer();
mp.setDataSource("/mnt/sdcard/yourdirectory/youraudiofile.mp3");
mp.prepare();
mp.start();
gt0wga4j

gt0wga4j5#

我使用这个类的音频播放。如果你的音频位置是原始文件夹。

调用播放方法:

new AudioPlayer().play(mContext, getResources().getIdentifier(alphabetItemList.get(mPosition)
                        .getDetail().get(0).getAudio(),"raw", getPackageName()));

音频播放器.java类:

public class AudioPlayer {

    private MediaPlayer mMediaPlayer;

    public void stop() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }
    // mothod for raw folder (R.raw.fileName)
    public void play(Context context, int rid){
        stop();

        mMediaPlayer = MediaPlayer.create(context, rid);
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                stop();
            }
        });

        mMediaPlayer.start();
    }

    // mothod for other folder 
    public void play(Context context, String name) {
        stop();

        //mMediaPlayer = MediaPlayer.create(c, rid);
        mMediaPlayer = MediaPlayer.create(context, Uri.parse("android.resource://"+ context.getPackageName()+"/your_file/"+name+".mp3"));
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                stop();
            }
        });

        mMediaPlayer.start();
    }

}
m2xkgtsf

m2xkgtsf6#

    • 2020年11月**

这对我很有效:

final File file = new File(getFilesDir(), "test.wav");//OR path to existing file
mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.fromFile(file));
mediaPlayer.start();
dddzy1tm

dddzy1tm7#

在Kotlin
1)文件位于资源文件夹中:

var nowLesson =resources.getIdentifier("test.mp3", "raw", packageName)
             
mediaPlayer = MediaPlayer.create(applicationContext, nowLesson)
    
mediaPlayer.start()

2)文件位于路径(文件路径)中

val file = File(
        this.getDir("Music", MODE_PRIVATE),
        "/t.mp3"
    )
mediaPlayer = MediaPlayer.create(this,Uri.fromFile(file ))
                mediaPlayer.start()

相关问题