如何创建用于播放音频的媒体控制器?

时间:2017-02-20 09:33:58

标签: android android-mediaplayer

我已经完成了使用媒体控制器播放音频的代码。我的代码工作正常,但运行代码时会出现一些异常。

  public class CallHistoryActivity extends Activity implements MediaPlayer.OnPreparedListener, MediaController.MediaPlayerControl{

    Button audioPlayer;

    private static final String TAG = "AudioPlayer";

    public static final String AUDIO_FILE_NAME = "audioFileName";

    private MediaPlayer mediaPlayer;
    private MediaController mediaController;
    private String audioFile;

    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_history);
        audioPlayer = (Button)findViewById(R.id.audioPlayer);

    }

    public void audioplayer(View view)
    {

        String path =new String(Environment.getExternalStorageDirectory() + "/NewCallLogs/Yaendi Yaendi.mp3 ");
        Uri uri = Uri.parse(path);

        mediaPlayer = new MediaPlayer();

        mediaController = new MediaController(this);


        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(String.valueOf(uri));

        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


public void stopPlayer(View view)
{
    mediaPlayer.stop();
    mediaController.hide();
    mediaPlayer.release();
}

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //the MediaController will hide after 3 seconds - tap the screen to make it appear again
        mediaController.show();
        return false;
    }

    //--MediaPlayerControl methods----------------------------------------------------
    public void start() {
        mediaPlayer.start();
    }

    public void pause() {
        mediaPlayer.pause();
    }

    public int getDuration() {
        return mediaPlayer.getDuration();
    }

    public int getCurrentPosition() {
        return mediaPlayer.getCurrentPosition();
    }

    public void seekTo(int i) {
        mediaPlayer.seekTo(i);
    }

    public boolean isPlaying() {
        return mediaPlayer.isPlaying();
    }

    public int getBufferPercentage() {
        return 0;
    }

    public boolean canPause() {
        return true;
    }

    public boolean canSeekBackward() {
        return true;
    }

    public boolean canSeekForward() {
        return true;
    }

    @Override
    public int getAudioSessionId() {
        return 0;
    }
    //--------------------------------------------------------------------------------

    public void onPrepared(MediaPlayer mediaPlayer) {

        mediaPlayer.start();

        Log.d(TAG, "onPrepared");
        mediaController.setMediaPlayer((MediaController.MediaPlayerControl) this);
        mediaController.setAnchorView(findViewById(R.id.main_audio_view));

        handler.post(new Runnable() {
            public void run() {
                mediaController.setEnabled(true);
                mediaController.show();
            }
        });
    }
}

logcat中显示的异常如下所示

    02-20 14:56:42.402 21410-21410/com.sey.newcalllogs E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 46
02-20 14:56:42.412 21410-21410/com.sey.newcalllogs E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 49
02-20 14:56:42.412 21410-21410/com.sey.newcalllogs E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-20 14:56:42.422 21410-21410/com.sey.newcalllogs E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-20 14:56:42.422 21410-21410/com.sey.newcalllogs E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-20 14:56:42.422 21410-21410/com.sey.newcalllogs E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 52
02-20 14:56:45.442 21410-21410/com.sey.newcalllogs E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
02-20 14:56:53.402 21410-21421/com.sey.newcalllogs E/MediaPlayer: error (1, -2147483648)
02-20 14:56:56.072 21410-21421/com.sey.newcalllogs E/MediaPlayer: error (1, -2147483648)
02-20 14:56:57.602 21410-21421/com.sey.newcalllogs E/MediaPlayer: error (1, -2147483648)
02-20 14:56:58.792 21410-21410/com.sey.newcalllogs E/MediaPlayer: stop called in state 0
02-20 14:56:58.792 21410-21410/com.sey.newcalllogs E/MediaPlayer: error (-38, 0)

然后,媒体控制器的布局设计如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_audio_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.seyali.newcalllogs.CallHistoryActivity">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/audioPlayer"
    android:onClick="audioplayer"/>


<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/stopPlaying"
    android:onClick="stopPlayer"/>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Now playing:"
    android:textSize="25sp"
    android:textStyle="bold"
    />
<TextView
    android:id="@+id/now_playing_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dip"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="10dip"
    android:layout_gravity="center"
    android:text="Now playing.."
    android:textSize="16sp"
    android:textStyle="italic"/>

请帮助我如何克服这个异常。我尝试了所有的方法,但我的问题没有解决。请帮助我。

2 个答案:

答案 0 :(得分:0)

MediaPlayer应遵循设计。 请参阅:https://developer.android.com/reference/android/media/MediaPlayer.html

mediaplayer.stop()应该从播放状态触发(在调用start之后)。

02-20 14:56:58.792 21410-21410 / com.sey.newcalllogs E / MediaPlayer:在州0停止调用

mediaPlayer.reset();
mediaPlayer.setDataSource(String.valueOf(uri));
mediaPlayer.prepare();

   public void onPrepared(MediaPlayer mediaPlayer) {
        mediaPlayer.start();
    }

只有在完成所有这些步骤后,您才能调用mediaPlayer.stop();

以上错误显示您的呼叫停止处于错误状态。请检查

答案 1 :(得分:0)

要解决此问题,请在调用方法之前检查mediaPlayer的状态。例如:

if(mediaPlayer.isPlaying()) {
mediaPlayer.pause();

}

请让我知道它是否解决了问题