2013-11-04 30 views
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.)根据哪个bu来设置我的路径变量tton点击然后解析“语境”和“路径”到“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; 

这工作对我来说,虽然我相信有可能是一个更好的解决方案...

+0

'R.raw.shou1'是一个整数'R.java'是不是?如果您将它用作路径的一部分,它将不起作用。你应该尝试别的。 – Piovezan

回答

0

尝试更换此:

player = new MediaPlayer(); 

与此:

// context should be any context, e.g. your activity or getApplicationContext() 
player = MediaPlayer.create(context, R.raw.shou1); 
+0

谢谢你Piovezan。但那不是我正在寻找的。我没有说明这个问题...我有一个数字或按钮,当按下时需要播放相关的MP3。所以根据选择哪个按钮动态分析mp3文件。我应该说我的MediPLayer类是作为普通的Java类创建的 - 没有inflater或onCreate方法。 – user2732582

相关问题