首页 > 《第一行代码》中播放音频程序在手机运行出错,该怎么办?

《第一行代码》中播放音频程序在手机运行出错,该怎么办?

练习《第一行代码》中的程序,播放音频小节(8.4.1),在手机上运行后音频无法播放。手机在根目录放了音频文件“music.mp3”,在SD卡根目录上也放了该文件,在手机根目录创建“sdcard”文件夹下也放了该文件。
这是MainActivity.java源码

package com.example.android.mediaplayer;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button play;
    Button pause;
    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_button);
        pause = (Button) findViewById(R.id.pause_button);
        stop = (Button) findViewById(R.id.stop_button);
        play.setOnClickListener(this);
        pause.setOnClickListener(this);
        stop.setOnClickListener(this);
        initMediaPlayer();
    }

    private void initMediaPlayer(){
        try{
            File file = new File(Environment.getExternalStorageDirectory(),"music.mp3");
            mediaPlayer.setDataSource(file.getPath());
            mediaPlayer.prepare();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.play_button:
                if (!mediaPlayer.isPlaying()) {
                    mediaPlayer.start();
                }
                break;
            case R.id.pause_button:
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                }
                break;
            case R.id.stop_button:
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.reset();
                    initMediaPlayer();
                }
                break;
            default:
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }
}

这是xml源码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android.mediaplayer.MainActivity">

    <Button
        android:id="@+id/play_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="播放" />

    <Button
        android:id="@+id/pause_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="暂停" />

    <Button
        android:id="@+id/stop_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止" />
</LinearLayout>

运行后,一点击播放按钮会出现下方错误

05-02 15:20:35.197 31272-31272/com.example.android.mediaplayer E/MediaPlayer: start called in state 1
05-02 15:20:35.197 31272-31272/com.example.android.mediaplayer E/MediaPlayer: error (-38, 0)
05-02 15:20:35.199 31272-31272/com.example.android.mediaplayer E/MediaPlayer: Error (-38,0)

再点播放按钮,每次只打印一行错误,如下

05-02 15:20:37.738 31272-31272/com.example.android.mediaplayer E/MediaPlayer: start called in state 0

觉得代码没问题,和书上都一样了,不知道该怎么办,求大神帮忙!!!在此感谢。


建议:你还是把音频文件放在项目内的资源文件夹下吧,我不太相信现在的你能够成功访问手机系统上的文件夹


SD卡访问权限加了没。<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


我猜测你的问题很可能是因为对应的路径没有“music.mp3”文件。

建议你在initMediaPlayer()方法中try语句的中的File file = new File(Environment.getExternalStorageDirectory(),"music.mp3");的后面加上上一条Log语句: Log.i("音乐文件路径", file.getPath());,看输出的路径是什么。


Error (-38,0)和Error (-19,0)一般是硬件设备不支持所致,在播放音乐时可以采用异步缓存,看你在写代码时直接加载到缓存,可以尝试新建一个模拟器,ram分配700m多一点.

【热门文章】
【热门文章】