如何在资产之前从RAW文件夹播放声音?

时间:2018-03-28 08:49:57

标签: android android-fragments

我有一个场景,用户通过单击设置页面中的按钮打开详细信息,当详细信息打开时,它将播放第2页上的音频之前10秒的详细音频,如下所示在代码中实现,如果用户关闭通过再次单击设置页面中的按钮,它将直接播放下面实现的音频。

这是设置页面,其中使用了sharedprefrences

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    button1 = (ImageButton)findViewById(R.id.button1);
    button2 = (ImageButton) findViewById(R.id.button2);
    button3 = (ImageButton) findViewById(R.id.button3);

    button1.setOnClickListener(new View.OnClickListener() {



        public void onClick(View arg0)
        {
            SavePreferencesInteger("audio",0,getApplicationContext());
            Toast.makeText(Settings.this, "Qari 1 Voice selected",
                    Toast.LENGTH_LONG).show();
        }
    });



    button2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0)
        {
            SavePreferencesInteger("audio",1,getApplicationContext());
            Toast.makeText(Settings.this, "Qari 2 Voice is not available ",
                    Toast.LENGTH_LONG).show();
        }
    });


}


public static void SavePreferencesInteger(String key, int value,Context context){

    SharedPreferences pref = context.getSharedPreferences("NooraniQaida",0);
    SharedPreferences.Editor editor = pref.edit();
    editor.putInt(key, value);
    editor.commit();
}
public static int GetPreferencesInteger(String key,Context context){

    SharedPreferences pref = context.getSharedPreferences("NooraniQaida",0);
    int savedPreferences = pref.getInt(key, 0);
    return savedPreferences;
}

public static void saveStringSet(Context context,String value, HashSet<String> mSet) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sp.edit();
    editor.putStringSet(value, mSet);
    editor.apply();
}

这是第2页上的按钮

case R.id.pg2_4: {

            clearcolor();
            if (mp.isPlaying()) {
                mp.stop();
                clearcolor();
            }
            try {
                pg2_4.setColorFilter(0xff00ff00, PorterDuff.Mode.LIGHTEN);
                mp.reset();
                AssetFileDescriptor afd = null;
                if (Settings.GetPreferencesInteger("audio", getActivity().getApplicationContext()) == 0) {
                    afd = getActivity().getAssets().openFd("q1p2_1.mp3");
                } else if (Settings.GetPreferencesInteger("audio", getActivity().getApplicationContext()) == 1) {
                    afd = getActivity().getAssets().openFd("q1p3_1.mp3");
                } else if (Settings.GetPreferencesInteger("audio", getActivity().getApplicationContext()) == 2) {
                    afd = getActivity().getAssets().openFd("q3p2_1.mp3");
                } else if (Settings.GetPreferencesInteger("audio", getActivity().getApplicationContext()) == 3) {
                    afd = getActivity().getAssets().openFd("q4p2_1.mp3");
                }
                mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                mp.prepare();
                mp.start();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                    clearcolor();
                }
            });
            break ;
        }

任何人都可以帮助我实现这一目标,而page2就是一个片段。

1 个答案:

答案 0 :(得分:0)

 player = MediaPlayer.create(MyService.this, R.raw.your_file_name);
 player.start();

希望这会对你有所帮助。