在android中播放音频文件

时间:2013-07-01 05:44:47

标签: android android-mediaplayer android-mediarecorder

我遇到了一个我需要在android中播放媒体文件的要求。我会发布页面的屏幕截图

Playing the media file

实际要求是用户想要在他的录音列表中播放音频文件。录音列表通过列表适配器显示。我想在同一页面上显示音频文件的播放进度(如屏幕所示)发)。

我粘贴了我尝试过的代码。

public class RecordingActivity extends ListActivity implements MediaPlayerControl {
    MediaPlayer mMediaPlayer;
    MediaController mMediaController;
    Handler mHandler;
    String OUTPUT_FILE;
    RelativeLayout rl; 
    Context context = null;
    Point p;
    static final String[] recordings = new String[] {"Example1","Example2",
            "Example3","Example4","Example5" };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new FirstAdapter(this, recordings));
        }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        showPopup(RecordingActivity.this, p); 
        // get selected items
        mMediaPlayer = new MediaPlayer();
        mMediaController = new MediaController(this);
        mHandler = new Handler();
        mMediaController.setMediaPlayer(RecordingActivity.this);
        OUTPUT_FILE = Environment.getExternalStorageDirectory()+"/recorder.mp3";
        try{
        mMediaPlayer.setDataSource(OUTPUT_FILE);
        mMediaPlayer.prepare();

        }
        catch(Exception e){


        }
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                 mHandler.post(new Runnable() {
                     public void run() {
                       mMediaController.show(10000);
                       mMediaPlayer.start();
                       mMediaController.setEnabled(true);
                     }
             });            
            }
        });
    }
    private void showPopup(final Activity context, Point p) {
       int popupWidth = 200;
       int popupHeight = 150;
     // Inflate the popup_layout.xml
       RelativeLayout viewGroup = (RelativeLayout) context.findViewById(R.id.reviewlist);
       LayoutInflater layoutInflater = (LayoutInflater) context
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View layout = layoutInflater.inflate(R.layout.activity_play, viewGroup);
     // Creating the PopupWindow
       final PopupWindow popup = new PopupWindow(context);
       popup.setContentView(layout);
       popup.setWidth(popupWidth);
       popup.setHeight(popupHeight);
       popup.setFocusable(true);
       // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
       int OFFSET_X = 30;
       int OFFSET_Y = 30;
       // Clear the default translucent background
       popup.setBackgroundDrawable(new BitmapDrawable());
       // Displaying the popup at the specified location, + offsets.
       popup.showAtLocation(layout, Gravity.NO_GRAVITY,p.x+OFFSET_X,p.y+OFFSET_Y);
       // Getting a reference to Close button, and close the popup when clicked.
       ImageButton close = (ImageButton) layout.findViewById(R.id.close);
       Button b1 =(Button) layout.findViewById(R.id.button1);
       Button b2 =(Button) layout.findViewById(R.id.button2);
       close.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
          popup.dismiss();
        }
       });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMediaPlayer.stop();
        mMediaPlayer.release();
    }




 @Override
    public boolean canPause() {
        return true;
    }

    @Override
    public boolean canSeekBackward() {
        return false;
    }

    @Override
    public boolean canSeekForward() {
        return false;
    }

    @Override    
    public int getBufferPercentage() {
        int percentage = (mMediaPlayer.getCurrentPosition() * 100) / mMediaPlayer.getDuration();
        return percentage;
    }

    @Override
    public int getCurrentPosition() {
        return mMediaPlayer.getCurrentPosition();
    }

    @Override
    public int getDuration() {
        return mMediaPlayer.getDuration();
    }

    @Override
    public boolean isPlaying() {
        return mMediaPlayer.isPlaying();
    }

    @Override
    public void pause() {
      if(mMediaPlayer.isPlaying())
       mMediaPlayer.pause();
    }

    @Override
    public void seekTo(int pos) {
        mMediaPlayer.seekTo(pos);
    }

    @Override
    public void start() {
        mMediaPlayer.start();
        mMediaController.show();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mMediaController.show();
        return false;
    }
}

我面临的问题是我能够播放音频文件,但它没有显示任何弹出布局。

3 个答案:

答案 0 :(得分:4)

媒体播放器

   MediaPlayer mp = new MediaPlayer();

 // Set data source -
  setDataSource("/sdcard/path_to_song");

 // Play audio
 mp.start();

// Pause audio
mp.pause();

   // Reset mediaplayer
  mp.reset();

  // Get song length duration - in milliseconds
 mp.getDuration();

  // Get current duration - in milliseconds
 mp.getCurrentDuration();

   // Move song to particular second - used for Forward or Backward
  mp.seekTo(positon); // position in milliseconds

  // Check if song is playing or not
 mp.isPlaying(); // returns true or false

希望这会给你一些解决方案。请参阅此link。它将解决您的问题

答案 1 :(得分:3)

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mMediaPlayer = new MediaPlayer();
        mMediaController = new MediaController(this);
        mMediaController.setMediaPlayer(PlayAudioActivity.this);
        mMediaController.setAnchorView(findViewById(R.id.audioView));

        String audioFile = "" ; 
        try {
            mMediaPlayer.setDataSource(audioFile);
            mMediaPlayer.prepare();
        } catch (IOException e) {
           System.out.println("Error in playing audio");
        }

        mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                    mHandler.post(new Runnable() {
                            public void run() {
                                    mMediaController.show(10000);
                                    mMediaPlayer.start();
                            }
                    });
            }
        });
    }

如果你已经这样做了。

@Override
    protected void onDestroy() {
        super.onDestroy();
        mMediaPlayer.stop();
        mMediaPlayer.release();
    }




 @Override
    public boolean canPause() {
        return true;
    }

    @Override
    public boolean canSeekBackward() {
        return false;
    }

    @Override
    public boolean canSeekForward() {
        return false;
    }

    @Override    
    public int getBufferPercentage() {
        int percentage = (mMediaPlayer.getCurrentPosition() * 100) / mMediaPlayer.getDuration();

        return percentage;
    }

    @Override
    public int getCurrentPosition() {
        return mMediaPlayer.getCurrentPosition();
    }

    @Override
    public int getDuration() {
        return mMediaPlayer.getDuration();
    }

    @Override
    public boolean isPlaying() {
        return mMediaPlayer.isPlaying();
    }

    @Override
    public void pause() {
        if(mMediaPlayer.isPlaying())
            mMediaPlayer.pause();
    }

    @Override
    public void seekTo(int pos) {
        mMediaPlayer.seekTo(pos);
    }

    @Override
    public void start() {
        mMediaPlayer.start();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mMediaController.show();

        return false;
    }
}

答案 2 :(得分:1)

public class PlayAudioActivity extends Activity implements MediaPlayerControl {
        private MediaController mMediaController;
        private MediaPlayer mMediaPlayer;
        private Handler mHandler = new Handler();

使用它。