0

嗨,我一直在尝试处理这段代码,但它给 logcat 一个错误,如下所示:

09-01 13:14:28.799: E/AwesomePlayer(123): reset+
09-01 13:14:28.799: E/AwesomePlayer(123): reset at1
09-01 13:14:28.799: E/AwesomePlayer(123): reset-
09-01 13:14:28.799: E/AwesomePlayer(123): reset at1
09-01 13:14:28.799: E/MediaPlayer(3092): error (1, -2147483648)

堆栈跟踪

09-01 13:31:33.749: E/MediaPlayer(3458): error (1, -2147483648)
09-01 13:31:33.759: E/~~IOException~~(3458): java.io.IOException: Prepare failed.: status=0x1
09-01 13:31:33.789: E/->>(3458): ~~stacktrace~~
09-01 13:31:33.789: E/->>(3458): java.io.IOException: Prepare failed.: status=0x1
09-01 13:31:33.789: E/->>(3458):    at android.media.MediaPlayer.prepare(Native Method)
09-01 13:31:33.789: E/->>(3458):    at com.example.boombastic.Playlist.playSong(Playlist.java:110)
09-01 13:31:33.789: E/->>(3458):    at com.example.boombastic.Playlist.access$0(Playlist.java:104)
09-01 13:31:33.789: E/->>(3458):    at com.example.boombastic.Playlist$1.onItemClick(Playlist.java:75)
09-01 13:31:33.789: E/->>(3458):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
09-01 13:31:33.789: E/->>(3458):    at android.widget.AbsListView.performItemClick(AbsListView.java:1070)
09-01 13:31:33.789: E/->>(3458):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2527)
09-01 13:31:33.789: E/->>(3458):    at android.widget.AbsListView$1.run(AbsListView.java:3188) 
09-01 13:31:33.789: E/->>(3458):    at android.os.Handler.handleCallback(Handler.java:605)
09-01 13:31:33.789: E/->>(3458):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 13:31:33.789: E/->>(3458):    at android.os.Looper.loop(Looper.java:137)
09-01 13:31:33.789: E/->>(3458):    at android.app.ActivityThread.main(ActivityThread.java:4424)
09-01 13:31:33.789: E/->>(3458):    at java.lang.reflect.Method.invokeNative(Native Method)
09-01 13:31:33.789: E/->>(3458):    at java.lang.reflect.Method.invoke(Method.java:511)
09-01 13:31:33.789: E/->>(3458):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
09-01 13:31:33.789: E/->>(3458):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-01 13:31:33.789: E/->>(3458):    at dalvik.system.NativeStart.main(Native Method)

这是我的包含代码的类:

public class Playlist extends Activity
{
ListView lstview;
ArrayList<String> Songslist;
ArrayList<String> Path;
ArrayAdapter<String> Adapteraa;
Cursor cursor;
Uri allsongsuri;
String selection;
Thread tsearchsongs;
MediaPlayer mMediaPlayer=new MediaPlayer();
Toast t1;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState)
{
    //PREPARATIONS FOR ACTIVITY
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_playlist);
    // Show the Up button in the action bar.
    setupActionBar();


    //INITIALIZATIONS
    lstview=(ListView)findViewById(R.string.playlistHolder);
    Songslist=new ArrayList<String>();
    Path=new ArrayList<String>();
    Adapteraa=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Songslist);
    lstview.setAdapter(Adapteraa);
    String[] STAR = { "*" };        
    allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    cursor=managedQuery(allsongsuri, STAR, selection, null, null);
    t1=new Toast(getApplicationContext());


    GETSONGS();     
    //LISTENERS
    lstview.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) 
        {
            // TODO Auto-generated method stub
            int songclicked=arg2;
            String songpath=Path.get(songclicked).toString();
            try 
            {
                playSong(songpath);
            }
            catch (IllegalArgumentException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IllegalStateException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}

private void playSong(String path) throws IllegalArgumentException, IllegalStateException, IOException 
{
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    path = extStorageDirectory + File.separator + path;
    mMediaPlayer.reset();
    mMediaPlayer.setDataSource(path);
    mMediaPlayer.prepare();
    mMediaPlayer.start();
}

我需要为此启动服务吗?我是否需要声明任何播放声音的权限,因为它是用于存储?

4

2 回答 2

1

1)在播放前检查您要播放的文件是否存在以及路径是否正确。

2) 尝试不同的音频格式,例如 mp3。

3)使用文件描述符而不是文件路径,即

FileInputStream fis = new FileInputStream(pathofiletobeplayed); mediaPlayer.setDataSource(fis.getFD());

于 2013-09-01T08:22:08.950 回答
0

您可以使用媒体控制器来设置媒体播放器。如果有帮助,请查看链接 http://intransitione.com/blog/playing-an-audio-file-with-android/

于 2013-09-01T08:20:24.283 回答