嗨,大家好,我想在点击文字时播放某个mp3文件.例如,我点击了"Nicholas"这个词,应用程序必须播放nicholas.mp3 ...
对不起我的乱码,我是android dev的新手:
package com.example.playword;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
//import android.os.Handler;
import android.view.View;
//import android.view.View.OnClickListener;
//import android.widget.Button;
import android.widget.TextView;
public class PlayWord extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //final Handler mHandler = new Handler();
        final TextView nicholas = (TextView) findViewById(R.id.nicholas);
        final TextView was = (TextView) findViewById(R.id.was);
        nicholas.setText("Nicholas ");
        was.setText("was ");        
        /*
        Button btn = (Button) (findViewById(R.id.nicholasBtn));
        btn.setOnClickListener(new OnClickListener(){
            @Override
            public void …在阅读" Media Playback "和" MediaPlayer "android文档后,我仍然感到困惑,需要有关setDataSource重载方法的经验建议.
我正在使用我的Project MediaPlayer中的一个Service组件,它将在播放音乐时成为前台服务.我在res/raw我的apk文件夹中有我的音乐文件(.mp3).要开始播放,我知道我必须准备MediaPlayer对象.因为Android应用程序中的服务默认使用单个进程和主线程,所以我不希望我的用户
在MediaPlayer自行准备时获得ANR(想想原始文件夹中的媒体文件是否有大的大小).然后我使用prepareAsync而不是prepare(同步).所以我不能用:
mp = MediaPlayer.create(context, R.raw.myfile);
因为这已经在prepare()内部调用但不是prepareAsync().所以基本上我有两个选择(两个四个):
Uri myUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.myfile);
mp.setDataSource(context, myUri);
要么
AssetFileDescriptor afd = context.getResources().openRawResourceFd(R.raw.myfile);
mp.setDataSource(fd.getFileDescriptor());
afd.close();
使用其中之一后,我可以简单地使用:
mp.prepareAsync();
最后,我的问题出现了"包括这些不同的方法,哪一个是最好的选择?一方面有什么好处?我错过了什么吗?"
我有cheerapp.wav或cheerapp.mp3其他一些格式.
InputStream in = context.getResources().openRawResource(R.raw.cheerapp);       
BufferedInputStream bis = new BufferedInputStream(in, 8000);
// Create a DataInputStream to read the audio data from the saved file
DataInputStream dis = new DataInputStream(bis);
byte[] music = null;
music = new byte[??];
int i = 0; // Read the file into the "music" array
while (dis.available() > 0) {
    // dis.read(music[i]); // This assignment does not reverse the order
    music[i]=dis.readByte();
    i++;
}
dis.close();          
对于music从中获取数据的字节数组DataInputStream.我不知道分配的长度是多少.
这是资源而不是文件的原始文件,因此我不知道那个东西的大小.
我试图解决的问题是在一个需要播放音频文件的活动中.大多数文件将由用户创建(并保存到外部存储器中),因此使用以下代码播放(基于Google的示例代码):
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setDataSource(filename);
mPlayer.prepare();
mPlayer.start();
但是,一些音频文件将包含在应用程序中,通常使用以下代码播放:
MediaPlayer mPlayer = MediaPlayer.create(getBaseContext(), R.raw.filename);
mPlayer.prepare();
mPlayer.start();
问题是我希望能够像播放用户创建的文件一样播放raw文件夹中的音频文件,因为我不一定知道需要哪些文件.我试过在raw文件夹中获取音频文件的URI并使用以下代码播放它:
Uri uri = Uri.parse("android/resource://com.my.package/" + R.raw.filename);
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setDataSource(uri.toString());
mPlayer.prepare();
mPlayer.start();
但没有任何反应.没有错误消息,没有播放音频.
我概述了我认为最简单的解决方案,但如果它完成任务,我愿意走另一条路.非常感谢任何帮助.
背景:我正在开发一款健身应用.到目前为止一切都很好,但是当我MediaPlayer在android中使用音频文件时出现了问题.
我检查了资源,发现ListView但在RecyclerView + MediaPlayer上找不到任何内容.
我想知道如何在使用RecyclerView + Toggle Button + String Uri(离线 - 原始文件夹)时使其工作
问题:现在它正在播放每个点击事件的第一个.mp3文件(对于Eng:R.raw.sample_one_eng正在播放,对于印地语:R.raw.sample_one_hindi正在播放).我认为它没有考虑到国际地位.
后来,我想把它放在网上(可能是谷歌云),因为音频文件(.mp3)使我的应用程序非常沉重.关于这一点的任何想法也将受到赞赏(快速缓冲等).
ListExercises.java
public class ListExercises extends AppCompatActivity {
    List<ExerciseAudio> exerciseList = new ArrayList<>();
    RecyclerView.LayoutManager layoutManager;
    RecyclerView recyclerView;
    RecyclerViewAdapterAud adapter;
    PlayClickHandler clickHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_exercises);
        initData();
        recyclerView = (RecyclerView) findViewById(R.id.list_ex);
        adapter = new RecyclerViewAdapterAud(exerciseList, getBaseContext());
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }
    private void initData() {
        exerciseList.add(new ExerciseAudio(R.drawable.sample_one, "Sample Exercise One", "Sans …android android-mediaplayer recycler-adapter android-recyclerview
我做了很多谷歌搜索,但是其他人的解决方案对我来说却行不通。
我的目标是在报警通道上按需播放声音。
(因此,音量是通过警报音量设置来调整的)
从这个线程,我建立以下代码
mediaPlayerScan = MediaPlayer.create(getContext(), R.raw.scan_beep);
if (Build.VERSION.SDK_INT >= 21) {
  mediaPlayerScan.setAudioAttributes(new AudioAttributes.Builder()
          .setUsage(AudioAttributes.USAGE_ALARM)
          .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
          .build());
} else {
  mediaPlayerScan.setAudioStreamType(AudioManager.STREAM_ALARM);
}
它仍然在音乐频道上播放。(在音乐设置中调整IE音量不会发出警报)
我的直觉是我缺少权限之类的东西,但是我没有找到这样的权限。
我正在Google Pixel 1上进行测试
谢谢,
内森
编辑:
感谢@ jeffery-blattman,以下代码对我有用
mediaPlayerScan = new MediaPlayer();
try {
  mediaPlayerScan.setDataSource(getContext(),
          Uri.parse(getString(R.string.res_path) + R.raw.scan_beep));
  if (Build.VERSION.SDK_INT >= 21) {
    mediaPlayerScan.setAudioAttributes(new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_ALARM)
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .build());
  } else {
    mediaPlayerScan.setAudioStreamType(AudioManager.STREAM_ALARM);
  }
  mediaPlayerScan.prepare();
} catch (IOException e) {
  e.printStackTrace();
}