关于MP3播放器 [英] About mp3 player
问题描述
我工作的一个简单的MP3播放器。 A.mp3
是在SD卡。
I'm working on a simple Mp3 player. A.mp3
is at the sdcard.
当我点击按钮播放注意到发生了,为什么呢?
When I click the button play noting happens, why?
我不知道什么是错。
这里的 MainActivity
文件:
public class MainActivity extends Activity implements OnClickListener{
private Button play;
private Button pause;
private Button stop;
private MediaPlayer mediaPlayer = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play =(Button)findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
stop = (Button) findViewById(R.id.stop);
play.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
initMediaPlayer();
}
private void initMediaPlayer()
{
try {
File file =new File(Environment.getExternalStorageDirectory(),"A.mp3");
mediaPlayer.setDataSource(file.getPath());
mediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.play:
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
break;
case R.id.pause:
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
break;
case R.id.stop:
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();
initMediaPlayer();
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
下面是布局
文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Play" />
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pause" />
<Button
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Stop" />
我发现SD卡的路径是/存储/ sdcard1 /。所以我用mediaPlayer.setDataSource(/存储/ sdcard1 / A.mp3);但它的same..setDataSource没跑
I found that the path of sdcard is "/storage/sdcard1/". so I use mediaPlayer.setDataSource("/storage/sdcard1/A.mp3");But it's the same..setDataSource did not run
推荐答案
在评论您的答复去,我已经更新我的答案。结果
1 ..
Going by your replies in the comments, I have updated my answer.
1..
private void initMediaPlayer()
{
try {
File file =new File(Environment.getExternalStorageDirectory(),"A.mp3");
mediaPlayer.setDataSource(file.getPath());
mediaPlayer.prepare(); // Replace this line.
} catch (Exception e) {
e.printStackTrace();
}
}
您可以尝试使用
public void prepareAsync ()
而不是prepare。作为prepare()采用同步线程,可以阻止此程序。
instead of prepare. As prepare() uses synchronous threading which can block the program.
http://developer.android.com/reference/android/media/MediaPlayer.html#$p$ppareAsync()
请使用异步prepare方法,它更加高效。
Keep using the Async Prepare method, it is more efficient.
2 ..结果
此外,如果您设置了 OnErrorListener
,这将检查是否有错误时,要设置数据源。
2..
Also if you set an OnErrorListener
, this will check for errors when you are setting the data source.
public void setOnErrorListener (MediaPlayer.OnErrorListener listener)
<一个href=\"http://developer.android.com/reference/android/media/MediaPlayer.html#setOnErrorListener%28android.media.MediaPlayer.OnErrorListener%29\" rel=\"nofollow\">http://developer.android.com/reference/android/media/MediaPlayer.html#setOnErrorListener%28android.media.MediaPlayer.OnErrorListener%29
3 ..结果
如果你是显示了一个错误:
3..
If you are showing up an error:
Tag MediaPlayer start called in state 0;Tag MediaPlayer error(-38,0)
这是表示MediaPlayer的状态不inialised。你可以在这里看到的状态:
This is showing that the state of the MediaPlayer is not inialised. You can see the states here:
http://developer.android.com/reference/android/media/ MediaPlayer.html
4 ..结果
请确保你有你的参数设置正确的格式数据源:
4..
Ensure that you have your parameter to set data source in the correct format:
的setDataSource(的FileDescriptor),或的setDataSource(字符串),或
的setDataSource(上下文,URI),或的setDataSource(FileDescriptor的,长的,
长)在空闲状态的一个MediaPlayer对象转移
初始化的状态。
setDataSource(FileDescriptor), or setDataSource(String), or setDataSource(Context, Uri), or setDataSource(FileDescriptor, long, long) transfers a MediaPlayer object in the Idle state to the Initialized state.
5 ..结果
然后使用在prepared()
监听器的http://developer.android.com/reference/android/media/MediaPlayer.On$p$pparedListener.html
5..
Then using an OnPrepared()
Listener http://developer.android.com/reference/android/media/MediaPlayer.OnPreparedListener.html
您就可以安全地调用 mediaplayer.start()
6 ..结果
也看到这个问题的媒体播放器称为状态0,错误( - 38,0)
7 ..结果
如果不解决这个问题,让我知道。
7..
If this doesn't fix the problem, let me know.
这篇关于关于MP3播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!