gpt4 book ai didi

android - 在android中播放音频文件

转载 作者:行者123 更新时间:2023-11-29 14:32:26 25 4
gpt4 key购买 nike

我遇到了一个要求,我需要在 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;
}
}

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

最佳答案

媒体播放器

   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 .它会解决你的问题

关于android - 在android中播放音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17398081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com