简体   繁体   English

如何让媒体播放器从应用程序播放音频文件

[英]How to get the media player to play audio files from the app

I am having difficulties to play the audio files from within my app here is my code: 我在从我的应用程序中播放音频文件时遇到困难,这是我的代码:

Integer[] mAudio = {
    R.raw.audioa,
    R.raw.audiob,
    R.raw.audioc,
    R.raw.audiod,

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playing);  
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


    Gallery gallery = (Gallery) findViewById(R.id.gallery4);
    gallery.setAdapter(new ImageAdapter(this));
    gallery.setOnItemClickListener(new OnItemClickListener() {

 public void onItemClick(AdapterView<?> parent, View v, final int position, long id){

final ImageView imageView = (ImageView) findViewById(R.id.Image7);
imageView.setImageResource(mFullSizeIds[position]);
mageView.setOnClickListener(new View.OnClickListener() {

@Override
Public void onClick(View arg0) {

    android.media.MediaPlayer.create(Audio.this,mAudio[position]);
    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
    startActivity(intent);                           

         }
});

The idea is when the user selects the image from the gallery and then selects the larger image the audio should play. 这个想法是当用户从图库中选择图像,然后选择音频应播放的较大图像时。 However when I use this code: 但是,当我使用此代码时:

 MediaPlayer mp = MediaPlayer.create(Owls.this,mAudio[position]);  
 mp.start();

it then works great, but can't stop the audio and when exiting the app (home key or back key) the audio keeps playing. 这样效果很好,但无法停止音频,并且退出应用程序(按Home键或Back键)时,音频会继续播放。 What I need to achieve is to when the users selects the image it should then ask which player you would want to use and then play the audio track for that image. 我需要实现的是,当用户选择图像时,它应该询问您要使用哪个播放器,然后播放该图像的音轨。 At the moment it just brings up my entire media library and not the audio file specified in this code: 目前,它只是调出我的整个媒体库,而不是此代码中指定的音频文件:

android.media.MediaPlayer.create(Birds.this,mAudio[position]);

And it should only play the audio file for that image and not bring up the entire audio tracks for this app. 而且它只应播放该图像的音频文件,而不应播放此应用程序的全部音轨。
Any one have got any idea and please be able to help me? 任何人都有任何想法,请能够帮助我吗? Thanks 谢谢

It will solve your one of the issue:('can't stop the audio and when exiting the app (home key or back key) the audio keeps playing') 它将解决您的问题之一:(“无法停止音频,并且在退出应用程序时(Home键或Back键),音频会继续播放”)

This part has to be in EVERY activity's onPause: 这部分必须在每个活动的onPause中:

Context context = getApplicationContext();
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        if (!taskInfo.isEmpty()) {
          ComponentName topActivity = taskInfo.get(0).topActivity; 
          if (!topActivity.getPackageName().equals(context.getPackageName())) {
            player.stop();
            Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
          }
        }

So you can stop your player when you are exit from app. 因此,当您退出应用程序时,可以停止播放器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM