android - 《第一行代码》中播放音频程序在手机运行出错,该怎么办?
天蓬老师
天蓬老师 2017-04-17 16:57:50
0
4
794

练习《第一行代码》中的程序,播放音频小节(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

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

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(4)
黄舟

Have the SD card access permissions been added? <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

大家讲道理

I guess your problem is probably because the corresponding path does not have the "music.mp3" file.

It is recommended that you check the output path in initMediaPlayer()方法中try语句的中的File file = new File(Environment.getExternalStorageDirectory(),"music.mp3");的后面加上上一条Log语句: Log.i("音乐文件路径", file.getPath());.

左手右手慢动作

Suggestion: You should put the audio files under the resource folder in the project. I don’t really believe that you can successfully access the folder on the mobile phone system now

PHPzhong

Error (-38,0) and Error (-19,0) are generally caused by hardware devices not supporting it. You can use asynchronous caching when playing music. See if you load it directly into the cache when writing code. You can try to create a new simulation. The processor and ram are allocated a little more than 700m.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!