0

问题来自这个方法:

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();
    }

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

声音文件的路径是:

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)
4

3 回答 3

0

如果您正在缓冲用户 prepareAsync 而不是在 OnPreparedListener 上准备和启动 mediaPlayer 可能会对您有所帮助

于 2014-10-30T07:53:30.343 回答
0

Code for playing Mp3 from Url

    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;
    }

}

Code for playing Mp3 from SD Card

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();
    }
}

and also add External permision for reading file.

于 2014-06-17T13:42:36.933 回答
-2

尝试像这样初始化它:

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

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

我这样做并且效果很好。

于 2013-06-04T16:06:47.977 回答