我正在开发适用于Android的Radio Streming应用程序。 音乐很好地接收并由MediaPlayer类播放,但问题是当我尝试检索元数据时,应用程序阻止并返回错误。 这是代码:
package com.tests.mediaplayer1;
import java.io.IOException;
import android.media.AudioManager;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button btn_prev;
private Button btn_pause;
private Button btn_play;
private Button btn_next;
private TextView txt_song_title;
private SeekBar seekBar_volume;
private MediaPlayer mediaPlayer;
private MediaMetadataRetriever retriever;
private String url = "http://support.k-designed.net/test-z/music/Accordossie.mp3";
private AudioManager audioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_prev = (Button) findViewById(R.id.btn_prev);
btn_pause = (Button) findViewById(R.id.btn_pause);
btn_play = (Button) findViewById(R.id.btn_play);
btn_next = (Button) findViewById(R.id.btn_next);
txt_song_title = (TextView) findViewById(R.id.txt_song_title);
seekBar_volume = (SeekBar) findViewById(R.id.seekBar_volume);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
seekBar_volume.setMax(maxVolume);
seekBar_volume.setProgress(curVolume);
seekBar_volume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
}
});
btn_play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
mediaPlayer.setDataSource(url);
retriever.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
txt_song_title.setText(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE));
} catch(Exception e) {
e.printStackTrace();
}
}
});
btn_pause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.pause();
}
});
}
@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;
}
}
这是我得到的错误:
java.lang.IllegalStateException
at android.media.MediaPlayer.setDataSource(Native Method)
at com.tests.mediaplayer1.MainActivity$2.onClick(MainActivity.java:84)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
从错误代码中,我们可以观察到报告的错误如下
java.lang.IllegalStateException
at android.media.MediaPlayer.setDataSource(Native Method)
如果您引用native
的{{1}}实现,MediaPlayer
对象为空时会抛出IllegalStateException
。
从您的来源,如果我们检查MediaPlayer
,我们可以观察到您已声明此变量,但尚未创建对象。在您的来源中,请在创建retriever
对象之前尝试以下代码。
mediaPlayer
通过此更改,系统会创建retreiver = new MediaMetadataRetriever();
个对象,您对retriever
的呼叫即setDataSource
将成功。