无法使用MediaPlayer从路径文件播放音频文件(.mp3)

时间:2013-06-04 16:02:54

标签: android android-mediaplayer

问题来自这个方法:

public void playSound(String pathSound) {

        Log.i("Playsound pathFile", "Path:"+pathSound);
        if(mp != null)
        {
            mp.stop();
            mp.release();
            mp = null;
        } 

        mp = new MediaPlayer(); 
        try{
        mp.setDataSource(pathSound);            
        mp.prepare();
        }catch(Exception e){
            Log.i("PLAY SOUND ERROR",e.toString());
        }
        mp.start();
    }

在我的应用程序中,我在运行时下载声音文件,并在SQLite中存储pathFiles,然后我可以使用它。

声音文件的路径是:

PathSound =“/ data / data / com.example.mypackage / Folder / Raws / title.mp3”,我不知道我是否有权在此处存储此文件。

给我的错误:

06-04 15:26:18.420: E/MediaPlayer(1043): error (1, -2147483648)
06-04 15:26:18.420: W/System.err(1043): java.io.IOException: Prepare failed.: status=0x1
06-04 15:26:18.430: W/System.err(1043):     at android.media.MediaPlayer.prepare(Native Method)
06-04 15:26:18.430: W/System.err(1043):     at com.example.databasesqlitetest.ItemActivity.playSound(ItemActivity.java:187)
06-04 15:26:18.439: W/System.err(1043):     at com.example.databasesqlitetest.ItemActivity$2.onClick(ItemActivity.java:91)
06-04 15:26:18.439: W/System.err(1043):     at android.view.View.performClick(View.java:3540)
06-04 15:26:18.449: W/System.err(1043):     at android.view.View$PerformClick.run(View.java:14167)
06-04 15:26:18.449: W/System.err(1043):     at android.os.Handler.handleCallback(Handler.java:605)
06-04 15:26:18.459: W/System.err(1043):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 15:26:18.459: W/System.err(1043):     at android.os.Looper.loop(Looper.java:137)
06-04 15:26:18.459: W/System.err(1043):     at android.app.ActivityThread.main(ActivityThread.java:4560)
06-04 15:26:18.459: W/System.err(1043):     at java.lang.reflect.Method.invokeNative(Native Method)
06-04 15:26:18.459: W/System.err(1043):     at java.lang.reflect.Method.invoke(Method.java:511)
06-04 15:26:18.469: W/System.err(1043):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-04 15:26:18.469: W/System.err(1043):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-04 15:26:18.469: W/System.err(1043):     at dalvik.system.NativeStart.main(Native Method)
06-04 15:26:18.469: E/MediaPlayer(1043): start called in state 0
06-04 15:26:18.469: E/MediaPlayer(1043): error (-38, 0)
06-04 15:26:18.479: E/MediaPlayer(1043): Error (-38,0)

3 个答案:

答案 0 :(得分:0)

从Url播放Mp3的代码

    private void playSong(int position)  {

    HashMap<String, String > map= PlayListActivity.list.get(position);

    Uri myUri = Uri.parse(map.get("songsUrl"));// "http://searchgurbani.com/audio/sggs/1.mp3");

    if (!isPlaying)
    {           
        isPlaying = true;

        mp = new MediaPlayer();

        try {

            mp.reset(); // new one

            mp.setDataSource(AudioPlayer.this, myUri);

            //mp.prepareAsync();

            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

            mp.prepare(); // don't use prepareAsync for mp3 playback

            mp.start();

             String songTitle = map.get("songsName");
             songTitleLabel.setText(songTitle);

            songProgressBar.setProgress(0);
            songProgressBar.setMax(100);
            songTitleLabel.setSelected(true);

            // Updating progress bar
            updateProgressBar();

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    else
    {
        isPlaying = false; 

        //mHandler.removeCallbacks(mUpdateTimeTask);

        mp.release();// stop Playing 

        mp = null;
    }

}

从SD卡播放Mp3的代码

public void playSong(int songIndex) {

    try {

        mp.reset();

        mp.setDataSource(SongsList.songsList.get(songIndex).get("songPath"));
        mp.prepare();
        mp.start();

        String songTitle = SongsList.songsList.get(songIndex).get("songTitle");
        songTitleLabel.setText(songTitle);

        songTitleLabel.setSelected(true);// i think this is extra..
        // Changing Button Image to pause image
        btnPlay.setBackgroundResource(R.drawable.pause_btn);

        // set Progress bar values
        songProgressBar.setProgress(0);
        songProgressBar.setMax(100);

        // Updating progress bar
        updateProgressBar();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

并添加外部permision以读取文件。

答案 1 :(得分:0)

如果您正在缓冲用户prepareAsync而不是在OnPreparedListener上准备和启动mediaPlayer,这可以帮助您

答案 2 :(得分:-2)

尝试像这样初始化:

MediaPlayer mp = MediaPlayer.create(context, R.raw.title);
mp.start();

但你需要把title.mp3放在res / raw。

我这样做,效果很好。