1

我正在开发适用于 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)
4

1 回答 1

1

从错误代码中,我们可以观察到报告的错误如下

java.lang.IllegalStateException
at android.media.MediaPlayer.setDataSource(Native Method)

如果您参考 的native实现MediaPlayerIllegalStateException则在MediaPlayerobject 为 null 时抛出。

从您的来源中,如果我们检查retriever,我们可以观察到您已经声明了这个变量,但还没有创建对象。mediaPlayer在您的来源中,请在创建对象之前尝试以下代码。

retreiver = new MediaMetadataRetriever();

通过此更改,retriever将创建对象并且您对setDataSourceie的调用retriever.setDataSource(url);将成功。

于 2013-03-15T14:50:48.293 回答