0

我想从许多片段布局中使用这个 MediaPlayer 方法。

如何解析原始资源?

public void playSound(Uri path) throws IOException{
    MediaPlayer player;
    player = new MediaPlayer();
    try {
        player.setDataSource(path.getPath(), path);
    } catch (IOException e) {
        e.printStackTrace(); 
    } catch (IllegalArgumentException e) {
        e.printStackTrace();  
    } catch (SecurityException e) {
        e.printStackTrace();  
    } catch (IllegalStateException e) {
        e.printStackTrace();  
    }
    player.prepare();
    player.start();
    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        public void onCompletion(MediaPlayer mp){
            mp.release();
        }
    });
}

我目前正在从我的片段中使用它来解析原始目录中的 mp3 文件。

Btn_shou.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {

                Uri path = null;

                switch (tone.getCheckedRadioButtonId())
                {
                    case R.id.radioTone1:

                        path = Uri.parse("android.resource://" +getActivity().getApplicationContext().getPackageName() +"/"+R.raw.shou1);
                        try {
                            playSound(path);
                        } catch (IOException e) {
                            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                        }
                        break;

“路径”变量有问题。Android 抱怨找不到 mp3 文件。

我终于通过以下方式解决了这个问题:

  1. 创建我单独的 MediaPlayer 类。

    package com.example.FragmentTabsTutorial;
    import android.content.Context;
    import android.media.MediaPlayer;
    import android.net.Uri;
    import java.io.IOException;
    
    public class plaSnd {
    
    public void playSound(Uri path, Context context) throws IOException {
    
        MediaPlayer player;
        player = new MediaPlayer();
        try {
    
            player.setDataSource(context, path);
        } catch (IOException e) {
            e.printStackTrace();  
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace(); 
        } catch (IllegalStateException e) {
            e.printStackTrace();
    }
        player.prepare();
        player.start();
    
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    
            public void onCompletion(MediaPlayer mp) {
                mp.release();
    
            }
         });
    
    }
    

    }

2.)从我的片段中,我创建了一个 plaSnd() 类的对象:

 final plaSnd pla = new plaSnd();

3.) 创建一个变量来存储上下文:

final Context context = getActivity().getApplicationContext();

4.) 根据单击的按钮设置我的路径变量,然后将“上下文”和“路径”解析为“plaSnd”类中的 playSound() 方法:

 Btn_o.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {

            Uri path = null;

            switch (tone.getCheckedRadioButtonId())
            {
                case R.id.radioTone1:

                    path = Uri.parse("android.resource://" +getActivity().getApplicationContext().getPackageName() +"/"+R.raw.o1);

                    try {
                        pla.playSound(path, context);
                    } catch (IOException e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }

                    break;

这对我有用,但我确信可能有更好的解决方案......

4

1 回答 1

0

尝试替换这个:

player = new MediaPlayer();

有了这个:

// context should be any context, e.g. your activity or getApplicationContext()
player = MediaPlayer.create(context, R.raw.shou1);
于 2013-11-04T18:55:50.570 回答