MediaPlayer.setDataSource 导致有效文件的 IOException

Posted

技术标签:

【中文标题】MediaPlayer.setDataSource 导致有效文件的 IOException【英文标题】:MediaPlayer.setDataSource causes IOException for valid file 【发布时间】:2012-03-28 06:35:36 【问题描述】:

这段代码曾经有效。然后,也许我在某处改变了一些东西(或者如果我对 android 的了解是正确的,则更新在媒体播放器中引入了一个错误)。它停止在 一些 设备上运行!尤其是我的 Nexus S (2.3.6)。

该应用下载了文件test.m4a(17775201 字节)。为了验证其完整性,我将其复制到 SD 并在我的 PC 上播放。没问题!还把它与原始文件二进制比较,它匹配了 100%。

try 
    _mediaPlayer = new MediaPlayer();
    _mediaPlayer.setOnCompletionListener(this);
    _mediaPlayer.setOnPreparedListener(this);
    _mediaPlayer.setOnSeekCompleteListener(this);
    _mediaPlayer.setOnBufferingUpdateListener(this);
    _mediaPlayer.setOnInfoListener(this);
    _mediaPlayer.setOnErrorListener(this);

    // I even tried reading the file from SD card. Same error...
    File file = new File("/data/data/com.mycompany.myapp/files/MediaCache/Test.m4a");
    // file.isFile() == true
    // file.length() == expected value!

    FileInputStream is = new FileInputStream(file);
    _mediaPlayer.setDataSource(is.getFD()); // Throws!
    // If I use the filename as parameter, it throws later when preparing the media player...

    _mediaPlayer.prepareAsync();

catch (Exception e) 
    // java.io.IOException: setDataSourceFD failed.: status=0x80000000
    e.printStackTrace();

问:当文件确实有效时,什么可能导致IOException for setDataSourceFD

更新:这是一个 98 kB 的音频文件,我无法在 Nexus S (2.3.6) 上播放: http://www.2shared.com/audio/wUKoRiQk/test.html

我的一个朋友运行一些 ZTE 设备 (2.2),它似乎可以工作。没看懂!

【问题讨论】:

它似乎适用于小文件(到目前为止测试了 700 kB)。 你试过MediaPlayer.setDataSource(String path)吗? 您是否尝试过使用 setDataSource(FileDescriptor fd, long offset, long length)?经过一番谷歌搜索后,我找到了this,这表明 setDataSource(FileDescriptor) 可能会导致 IOException 出现问题。虽然它自己无法计算出文件的长度,但这似乎很奇怪...... 你能从媒体播放器发布堆栈跟踪和错误日志吗?应该有一对错误代码 请看我的更新。我添加了一个无法在 Nexus S (2.3.6) 上播放的音频文件... 【参考方案1】:

我想告诉您,MediaPlayer 正在正常运行。以下是我的所有假设。不要太认真。

这是您的 test.m4a 文件以 MediaPlayer 无法解码的格式编码,因此抛出一个错误,指出它无法创建 MediaPlayer 供您使用。 好像您使用计算机上的一些 FFmpeg 编解码器对其进行了编码。如果您尝试在不使用 FFmpeg 等高级编解码器的情况下创建文件会更好,因为 Android 无法即时解码高级编解码器。

但是,我不知道为什么有些智能手机可以播放您的 M4A 文件,而有些则不能。我对你的建议是使用 Audacity 已经安装的简单 OGG 格式,用于你所有的 Android 项目,因为 Audacity 拥有的 OGG 编解码器足够简单,Android 可以即时解码。我并不是说许多开发人员主要使用 OGG,只是在使用简单的编解码器时 OGG 似乎对 MediaPlayer 和 SoundPool 没有太多问题。需要更多研究...

甚至我的书《开始 Android 4 游戏开发》也依赖于 OGG 文件。

下面是我的证明。这是我在您的链接中使用的文件“test.m4a”。我让它在一个无限循环中运行,使其成为 100% 的证据证明:

03-18 01:05:22.826: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:22.826: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:22.936: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:22.936: I/MediaPlayer(1289): IOException...
03-18 01:05:22.936: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:22.936: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.037: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.046: I/MediaPlayer(1289): IOException...
03-18 01:05:23.046: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.046: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.146: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.146: I/MediaPlayer(1289): IOException...
03-18 01:05:23.146: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.156: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.256: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.256: I/MediaPlayer(1289): IOException...
03-18 01:05:23.256: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.256: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.366: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.366: I/MediaPlayer(1289): IOException...
03-18 01:05:23.366: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.366: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.476: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.476: I/MediaPlayer(1289): IOException...
03-18 01:05:23.476: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.476: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.586: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.586: I/MediaPlayer(1289): IOException...
03-18 01:05:23.586: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.586: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.696: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.696: I/MediaPlayer(1289): IOException...
03-18 01:05:23.696: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.696: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.806: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.806: I/MediaPlayer(1289): IOException...
03-18 01:05:23.806: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.806: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:23.917: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:23.917: I/MediaPlayer(1289): IOException...
03-18 01:05:23.917: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:23.917: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:24.026: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:24.026: I/MediaPlayer(1289): IOException...
03-18 01:05:24.026: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:24.026: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:24.136: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:24.136: I/MediaPlayer(1289): IOException...
03-18 01:05:24.136: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:24.136: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:24.246: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:24.246: I/MediaPlayer(1289): IOException...
03-18 01:05:24.246: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:24.246: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:24.356: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:24.356: I/MediaPlayer(1289): IOException...
03-18 01:05:24.356: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:24.356: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:24.466: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:24.466: I/MediaPlayer(1289): IOException...
03-18 01:05:24.466: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:24.466: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:24.856: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:24.856: I/MediaPlayer(1289): IOException...
03-18 01:05:24.856: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:24.856: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.017: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.017: I/MediaPlayer(1289): IOException...
03-18 01:05:25.017: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.017: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.146: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.157: I/MediaPlayer(1289): IOException...
03-18 01:05:25.157: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.157: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.266: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.266: I/MediaPlayer(1289): IOException...
03-18 01:05:25.266: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.266: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.376: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.376: I/MediaPlayer(1289): IOException...
03-18 01:05:25.376: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.376: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.486: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.486: I/MediaPlayer(1289): IOException...
03-18 01:05:25.486: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.486: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.586: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.586: I/MediaPlayer(1289): IOException...
03-18 01:05:25.586: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.586: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.696: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.696: I/MediaPlayer(1289): IOException...
03-18 01:05:25.696: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.696: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.806: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.806: I/MediaPlayer(1289): IOException...
03-18 01:05:25.806: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.806: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:25.916: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:25.916: I/MediaPlayer(1289): IOException...
03-18 01:05:25.916: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:25.916: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.026: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.026: I/MediaPlayer(1289): IOException...
03-18 01:05:26.026: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.026: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.136: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.136: I/MediaPlayer(1289): IOException...
03-18 01:05:26.136: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.136: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.246: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.246: I/MediaPlayer(1289): IOException...
03-18 01:05:26.246: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.246: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.356: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.356: I/MediaPlayer(1289): IOException...
03-18 01:05:26.356: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.356: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.466: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.466: I/MediaPlayer(1289): IOException...
03-18 01:05:26.466: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.466: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.577: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.577: I/MediaPlayer(1289): IOException...
03-18 01:05:26.577: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.577: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.697: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.697: I/MediaPlayer(1289): IOException...
03-18 01:05:26.697: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.697: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.807: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.807: I/MediaPlayer(1289): IOException...
03-18 01:05:26.807: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.807: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:26.917: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:26.917: I/MediaPlayer(1289): IOException...
03-18 01:05:26.917: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:26.917: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.027: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.027: I/MediaPlayer(1289): IOException...
03-18 01:05:27.027: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.027: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.137: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.137: I/MediaPlayer(1289): IOException...
03-18 01:05:27.137: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.137: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.236: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.236: I/MediaPlayer(1289): IOException...
03-18 01:05:27.236: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.236: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.347: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.347: I/MediaPlayer(1289): IOException...
03-18 01:05:27.347: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.347: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.457: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.457: I/MediaPlayer(1289): IOException...
03-18 01:05:27.457: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.457: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.567: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.567: I/MediaPlayer(1289): IOException...
03-18 01:05:27.567: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.567: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.677: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.677: I/MediaPlayer(1289): IOException...
03-18 01:05:27.677: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.677: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.787: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.787: I/MediaPlayer(1289): IOException...
03-18 01:05:27.787: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.787: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:27.907: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:27.907: I/MediaPlayer(1289): IOException...
03-18 01:05:27.907: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:27.907: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.007: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.017: I/MediaPlayer(1289): IOException...
03-18 01:05:28.017: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.017: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.127: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.127: I/MediaPlayer(1289): IOException...
03-18 01:05:28.127: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.127: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.237: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.237: I/MediaPlayer(1289): IOException...
03-18 01:05:28.237: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.237: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.347: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.347: I/MediaPlayer(1289): IOException...
03-18 01:05:28.347: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.347: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.457: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.457: I/MediaPlayer(1289): IOException...
03-18 01:05:28.457: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.457: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.567: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.567: I/MediaPlayer(1289): IOException...
03-18 01:05:28.567: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.567: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.677: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.677: I/MediaPlayer(1289): IOException...
03-18 01:05:28.677: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.677: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.787: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.787: I/MediaPlayer(1289): IOException...
03-18 01:05:28.787: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.787: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:28.907: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:28.907: I/MediaPlayer(1289): IOException...
03-18 01:05:28.907: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:28.907: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:29.017: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:29.017: I/MediaPlayer(1289): IOException...
03-18 01:05:29.017: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:29.017: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:29.127: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:29.127: I/MediaPlayer(1289): IOException...
03-18 01:05:29.127: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:29.127: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:29.237: E/MediaPlayer(1289): Unable to to create media player
03-18 01:05:29.237: I/MediaPlayer(1289): IOException...
03-18 01:05:29.237: D/MediaPlayer(1289): Making sure it is in IDLE state...
03-18 01:05:29.237: D/MediaPlayer(1289): Setting Data Source...
03-18 01:05:29.327: D/dalvikvm(1289): Calling exit(1)

现在,这是我编写的代码,如下所示:

package nttu.edu.test;

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class MediaPlayerTest extends Activity implements OnTouchListener 
    MediaPlayer player = null;
    TextView tv;
    public final String fileName = "sounds/test.m4a";
    AssetManager asset;
    AssetFileDescriptor afd;

    public void onCreate(Bundle b) 
        super.onCreate(b);
        tv = new TextView(this);
        tv.setOnTouchListener(this);
        tv.setText("Touch to start playing the music.");
        Log.d("MediaPlayer", "Creating MediaPlayer.");
        player = new MediaPlayer();

        try 
            asset = this.getAssets();
            afd = asset.openFd(fileName);
        
        catch (Exception e) 
        
        while (true) 
            try 
                Log.d("MediaPlayer", "Making sure it is in IDLE state...");
                player.reset();
                Log.d("MediaPlayer", "Setting Data Source...");
                player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                Log.d("MediaPlayer", "Now initialized. Preparing it.");
                player.prepare();
                break;
            
            catch (IllegalArgumentException e) 
                Log.i("MediaPlayer", "IllegalArgumentException...");
            
            catch (IllegalStateException e) 
                Log.i("MediaPlayer", "IllegalStateException...");
            
            catch (IOException e) 
                Log.i("MediaPlayer", "IOException...");
            
        
        try 
            afd.close();
        
        catch (IOException e) 
        
        setContentView(tv);
        Log.d("MediaPlayer", "Everything is prepared and ready to play the music file.");
    

    public boolean onTouch(View v, MotionEvent e) 
        if (e.getAction() == MotionEvent.ACTION_UP) 
            Log.d("MediaPlayer", "Playing the audio.");
            if (player.getAudiosessionId() != 0) 
                player.start();
                Log.d("MediaPlayer", "Success!");
            
        
        return true;
    

    protected void onResume() 
        super.onResume();

    

    protected void onPause() 
        super.onPause();
        if (player.getAudioSessionId() != 0) 
            player.pause();
            if (isFinishing()) 
                player.stop();
                player.release();
            
        
    

请注意代码 sn-p 顶部附近的最终字符串变量。如果我用这个替换那行:

public final String fileName = "sounds/test.ogg";

并将您的“test.m4a”文件重新编码为 Ogg Vorbis 文件,并且代码中没有其他更改,我可以正常播放 OGG 文件。以下是 OGG 文件的更改日志:

03-18 01:14:21.547: D/gralloc_goldfish(1335): Emulator without GPU emulation detected.
03-18 01:14:23.467: D/dalvikvm(1335): GC_CONCURRENT freed 50K, 4% free 5952K/6151K, paused 7ms+3ms
03-18 01:14:24.647: D/MediaPlayer(1335): Creating MediaPlayer.
03-18 01:14:24.657: D/MediaPlayer(1335): Making sure it is in IDLE state...
03-18 01:14:24.657: D/MediaPlayer(1335): Setting Data Source...
03-18 01:14:24.707: D/MediaPlayer(1335): Now initialized. Preparing it.
03-18 01:14:24.927: D/MediaPlayer(1335): Everything is prepared and ready to play the music file.
03-18 01:14:26.797: D/MediaPlayer(1335): Playing the audio.
03-18 01:14:26.817: D/MediaPlayer(1335): Success!

然后它就停在这里了。没有循环。

更多证据支持我的回答:

Link 1: Android SDK Reference (Do look at the State Diagram, very important!)

还有一张图片(您的“test.m4a”和您的文件重新编码成 OGG 文件的图像,并排放置。):

【讨论】:

尽管您的答案并不完全正确,但它确实为我指明了正确的方向。我分析了 M4A 标头,发现它根本没有标头! Test.m4a 确实是一个原始 AAC 文件。因此,不支持。不过,为什么有些设备能够播放这种原始音频仍然是个谜。 啊,我明白了。这也解释了为什么重新编码音频文件有效。很高兴帮助您解决问题。 :D 我在从服务器 url 播放视频时遇到这种类型的错误,请帮助我:( @Vaishali 提出一个新问题。 @tom_mai78101 我在尝试从服务器 url ***.com/questions/48071487/… 播放音频时也遇到同样的错误,请检查问题【参考方案2】:

分析 test.m4a 文件显示该文件根本没有 M4A 标头(仅比较记事本中的第一个字节。您可以在其中找到文件格式的标识符,例如 fourcc)。

它看起来与 Android 不支持的 RAW AAC 文件完全一样。中兴 Blade 手机中肯定有一些额外的非标准 AAC 东西。

【讨论】:

以上是关于MediaPlayer.setDataSource 导致有效文件的 IOException的主要内容,如果未能解决你的问题,请参考以下文章

Android MediaPlayer setDataSource failed

Caused by:java.lang.IllegalStateException at android.media.MediaPlayer._setDataSource(Native Method)

MediaPlayer.setDataSource 导致有效文件的 IOException

Nullpointer 异常“无效 android.media.MediaPlayer.setDataSource(android.content.Context,android.net.Uri)”

MediaPlayer setDataSource 失败,状态 = 0x80000000 为 2.3.4 上的文件路径设置的铃声

MediaPlayer 使用prepareAsync的正确姿势