如何直接播放 res/raw 文件夹中的音频文件?

发布于 2024-12-05 20:41:03 字数 193 浏览 0 评论 0原文

我在 res/raw 文件夹中有多个音频文件。我显示包含音频文件名的 ListView 。我想在用户选择ListView时播放相应的音频文件。我已经使用了setDataSource(path),但在播放时显示错误。如何直接播放该文件夹中的音频文件?或者还有其他办法吗?

I have multiple audio files in res/raw folder. I showing ListView that contains audio files name. I want to play the corresponding audio file when user select into the ListView. I have used setDataSource(path), but it showing error while playing. How play the audio files directly from that folder? Or Is there any other way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

弃爱 2024-12-12 20:41:03

在 onItemClickListener 中添加此代码。

listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {                 
                TextView txtView=(TextView)view.findViewById(R.id.txt_view);
                String fname=txtView.getText().toString().toLowerCase();

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

                              MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
                    mediaPlayer.start();
            }
        });

add this code in onItemClickListener.

listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {                 
                TextView txtView=(TextView)view.findViewById(R.id.txt_view);
                String fname=txtView.getText().toString().toLowerCase();

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

                              MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
                    mediaPlayer.start();
            }
        });
时光匆匆的小流年 2024-12-12 20:41:03

尝试从原始播放::

MediaPlayer mPlayer2;
mPlayer2= MediaPlayer.create(this, R.raw.bg_music_wav);
        mPlayer2.start();

清单文件中的权限::

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

更新::

public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {     
  MediaPlayer mPlayer2;
if(position==1)
{
    mPlayer2= MediaPlayer.create(this, R.raw.song1);
            mPlayer2.start();
}else it() .....
}

try this for playing from raw ::

MediaPlayer mPlayer2;
mPlayer2= MediaPlayer.create(this, R.raw.bg_music_wav);
        mPlayer2.start();

permission in manifest file ::

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

Update::

public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {     
  MediaPlayer mPlayer2;
if(position==1)
{
    mPlayer2= MediaPlayer.create(this, R.raw.song1);
            mPlayer2.start();
}else it() .....
}
魔法少女 2024-12-12 20:41:03
mVideoView = (VideoView) findViewById(R.id.Video_FrontPage);
uri = Uri.parse("android.resource://com.urPackageName/" + R.raw.welcom_video);
mVideoView.setVideoURI(uri);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.start();
mVideoView = (VideoView) findViewById(R.id.Video_FrontPage);
uri = Uri.parse("android.resource://com.urPackageName/" + R.raw.welcom_video);
mVideoView.setVideoURI(uri);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.start();
怀里藏娇 2024-12-12 20:41:03
 lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

          // selected item 
          String product = ((TextView) view).getText().toString();

          int [] resID= {R.raw.sound1,R.raw.sound2,R.raw.sound3};
        MediaPlayer mediaPlayer=MediaPlayer.create(this,resID[position]);

          mediaPlayer.start();
          // sending data to new activity


      }
    });
}
 lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

          // selected item 
          String product = ((TextView) view).getText().toString();

          int [] resID= {R.raw.sound1,R.raw.sound2,R.raw.sound3};
        MediaPlayer mediaPlayer=MediaPlayer.create(this,resID[position]);

          mediaPlayer.start();
          // sending data to new activity


      }
    });
}
幸福还没到 2024-12-12 20:41:03
var mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1)
mediaPlayer.start() // no need to call prepare(); create() does that for you
var mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1)
mediaPlayer.start() // no need to call prepare(); create() does that for you
鹿港小镇 2024-12-12 20:41:03

将音频文件放在 res/raw 文件夹中,并从 MediaPlayer 的 create 方法中调用它。

MediaPlayer.create(this, R.raw.audio_file_name).start();

例如:

MediaPlayer.create(this, R.raw.sample).start();

Place your audio file in the res/raw folder and call it from the create method of MediaPlayer.

MediaPlayer.create(this, R.raw.audio_file_name).start();

Eg:

MediaPlayer.create(this, R.raw.sample).start();
岁吢 2024-12-12 20:41:03

要播放原始文件夹中的音频,请调用此方法。就我而言,我的文件名是 notificaion.mp3

AudioPlayer().playAudio(mContext, "notificaion")

这是 AudioPlayer 类:

class AudioPlayer {
private var mMediaPlayer: MediaPlayer = MediaPlayer()

private fun stopAudio() {
   try {
       mMediaPlayer.release()
   }catch (ex: Exception){
       ex.printStackTrace()
   }

}

fun playAudio(mContext: Context, fileName: String) {
    try {
        stopAudio()
        mMediaPlayer = MediaPlayer.create(mContext, mContext.resources.getIdentifier(fileName, "raw", mContext.packageName))
        mMediaPlayer.setOnCompletionListener {  stopAudio() }
        mMediaPlayer.start()
    }catch (ex: Exception){
        ex.printStackTrace()
    }

    }
}

To play audio from the raw folder call this method. In my case my file name was notificaion.mp3:

AudioPlayer().playAudio(mContext, "notificaion")

Here is the AudioPlayer class:

class AudioPlayer {
private var mMediaPlayer: MediaPlayer = MediaPlayer()

private fun stopAudio() {
   try {
       mMediaPlayer.release()
   }catch (ex: Exception){
       ex.printStackTrace()
   }

}

fun playAudio(mContext: Context, fileName: String) {
    try {
        stopAudio()
        mMediaPlayer = MediaPlayer.create(mContext, mContext.resources.getIdentifier(fileName, "raw", mContext.packageName))
        mMediaPlayer.setOnCompletionListener {  stopAudio() }
        mMediaPlayer.start()
    }catch (ex: Exception){
        ex.printStackTrace()
    }

    }
}
半窗疏影 2024-12-12 20:41:03
listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {                 
                TextView txtView=(TextView)view.findViewById(R.id.txt_view);
                String fname=txtView.getText().toString().toLowerCase();

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

                              MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
                    mediaPlayer.start();
            }
        });

在 onItemClickListener 中添加此代码,还有一个信息是您无法播放 mp3 以外的任何文件,并且 mp3 文件必须是原始扩展名文件。

listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {                 
                TextView txtView=(TextView)view.findViewById(R.id.txt_view);
                String fname=txtView.getText().toString().toLowerCase();

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

                              MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
                    mediaPlayer.start();
            }
        });

add this code in onItemClickListener and one more information is you can't play any file other than mp3 and the mp3 file must be the original extension as file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文