我正在尝试创建一个示例 MP3 播放器。问题是,当我单击将从 url 播放 mp3 的按钮时,它会在模拟器中显示这个 logcat 并且歌曲根本不播放,当我在 Samsung Galaxy y duos lite (2.3.6) 中运行它时,当我按下播放按钮时,什么也没有发生(手机已连接到互联网),所以我尝试使用资源作为资产文件夹(2.34 MB)中的本机文件运行应用程序。同样的问题仍然存在。
import java.io.IOException;
import java.net.URL;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
MediaPlayer mediaPlayer = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}public void music(View v) throws IOException
{ final String url = "http://xxx.xxx.xxx/1.mp3";
AssetFileDescriptor afd = getAssets().openFd("1.mp3");
//final MediaPlayer mediaPlayer = new MediaPlayer();
try{
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
//mediaPlayer.setDataSource(afd.getFileDescriptor());
mediaPlayer.prepareAsync();
mediaPlayer.start();
}
catch(Exception e) {
e.printStackTrace(); }}
@Override
protected void onPause() {
super.onPause();
mediaPlayer.stop(); //>>> stop myMediaPlayer
mediaPlayer.release(); //>>> free myMediaPlayer
}
private void releaseMediaPlayer() {
if (mediaPlayer != null) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
mediaPlayer = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer.stop(); //>>> stop myMediaPlayer
mediaPlayer.release(); //>>> free myMediaPlayer
releaseMediaPlayer();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是当我单击触发音乐方法的播放按钮时出现的 Logcat 条目。
08-04 13:31:11.179: W/System.err(28943): java.lang.IllegalStateException
08-04 13:31:11.195: W/System.err(28943): at android.media.MediaPlayer.setAudioStreamType(Native Method)
08-04 13:31:11.195: W/System.err(28943): at com.example.music.MainActivity.music(MainActivity.java:30)
08-04 13:31:11.195: W/System.err(28943): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 13:31:11.195: W/System.err(28943): at java.lang.reflect.Method.invoke(Method.java:507)
08-04 13:31:11.195: W/System.err(28943): at android.view.View$1.onClick(View.java:2139)
08-04 13:31:11.195: W/System.err(28943): at android.view.View.performClick(View.java:2485)
08-04 13:31:11.195: W/System.err(28943): at android.view.View$PerformClick.run(View.java:9080)
08-04 13:31:11.195: W/System.err(28943): at android.os.Handler.handleCallback(Handler.java:587)
08-04 13:31:11.195: W/System.err(28943): at android.os.Handler.dispatchMessage(Handler.java:92)
08-04 13:31:11.195: W/System.err(28943): at android.os.Looper.loop(Looper.java:130)
08-04 13:31:11.195: W/System.err(28943): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-04 13:31:11.203: W/System.err(28943): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 13:31:11.203: W/System.err(28943): at java.lang.reflect.Method.invoke(Method.java:507)
08-04 13:31:11.203: W/System.err(28943): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-04 13:31:11.203: W/System.err(28943): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-04 13:31:11.203: W/System.err(28943): at dalvik.system.NativeStart.main(Native Method)
PS我也使用了访问Internet的权限。
感谢你的协助。