简体   繁体   English

媒体播放器 URI

[英]MediaPlayer URI

String path = Environment.getExternalStorageDirectory().toString()+"/Music/";
    File directory = new File(path);
    File[] mSongsList = directory.listFiles();

    Uri mUri= Uri.fromFile(mSongsList[0]);
    Log.v("MainActivity",mUri.toString());
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setDataSource(this, mUri);
    mMediaPlayer.prepare();
    mMediaPlayer.start();

I am trying to play the first song in my list but, i am getting an error on setDataSource.我正在尝试播放列表中的第一首歌曲,但是在 setDataSource 上出现错误。

it say " Unhandled Exception: java.io.IOException "它说未处理的异常:java.io.IOException

Place music files(s) under res/raw or any other folder under SD card.将音乐文件放在 res/raw 或 SD 卡下的任何其他文件夹下。 We are going to refer the audio file based on the Uri.我们将参考基于 Uri 的音频文件。

Create MediaPlayer object:创建媒体播放器对象:

MediaPlayer mPlayer = new MediaPlayer();

Locate the audio file:找到音频文件:

Uri myUri1 = Uri.parse("file:///sdcard/Songs/ARR Hits/hosannatamil.mp3");

Set the audio stream type of the media player:设置媒体播放器的音频流类型:

mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

Set the data source as a content Uri:将数据源设置为内容 Uri:

try {
    mPlayer.setDataSource(getApplicationContext(), myUri1);
} catch (IllegalArgumentException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI 
    correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI 
    correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI 
    correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
    e.printStackTrace();
}

Prepare the player for playback, synchronously:同步准备播放器进行播放:

try {
    mPlayer.prepare();
} catch (IllegalStateException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI 
    correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI 
    correctly!", Toast.LENGTH_LONG).show();
}

Start the player:启动播放器:

mPlayer.start();

The above information has been taken from the below link:以上信息摘自以下链接:

" https://programmerguru.com/android-tutorial/android-mediaplayer-example-play-from-uri/ " https://programmerguru.com/android-tutorial/android-mediaplayer-example-play-from-uri/

Keep your code inside the try-catch block.将您的代码保存在try-catch块中。

try{
// your code
}catch(IOException exception){

}

This is because you are trying to get the music file .这是因为您正在尝试获取music file If its not present then it will throw IOException , so you must handle this.如果它不存在,那么它会抛出IOException ,所以你必须处理这个。

Replace Your Code Like below:替换您的代码,如下所示:

Note: Make Sure That You Have Some Music In MusicDirectory.注意:确保您在 MusicDirectory 中有一些音乐。

    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();
                File directory = new File(path);
try{
 File[] mSongsList = directory.listFiles();

                Uri mUri= Uri.fromFile(mSongsList[0]);
                Log.v("MainActivity",mUri.toString());
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mMediaPlayer.setDataSource(this, mUri);
                mMediaPlayer.prepare();
                mMediaPlayer.start();
}catch(IOException exception){
    e.printStacktrace();
}

First of all make sure you have read permissions in your manifest file.首先确保您在清单文件中具有读取权限。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>

If you are using the android version Marshmallow or above you need to ask the user for Runtime Permissions .如果您使用的是 android 版本 Marshmallow 或更高版本,则需要向用户询问Runtime Permissions

This is a good article to request runtime permissions. 是一篇请求运行时权限的好文章。

For now you can go to the app info page and give the permissions manually.现在,您可以转到应用信息页面并手动授予权限。 It will start working.它将开始工作。

As in the image below :如下图所示: 在此处输入图片说明

You can play the song from your device by the following codes, but I don't know how to play next songs and previous songs , if you have any idea , pls share me.您可以通过以下代码从您的设备播放歌曲,但我不知道如何播放下一首歌曲和上一首歌曲,如果您有任何想法,请与我分享。

fun getURI(){
    val path: String =
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath()
    val directory = File(path)

    try {
         mSongsList= directory.listFiles()
        val mUri = Uri.fromFile((mSongsList as Array<File>?)!![0])
        Log.v("MainActivity", mUri.toString())

        mp!!.reset()
        mp!!.setAudioStreamType(AudioManager.STREAM_MUSIC)
        mp!!.setDataSource(this, mUri)
        mp!!.prepare()
        mp!!.start()
    } catch (e: IOException) {
        e.printStackTrace()
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM