2014-07-22 150 views
2

我遇到了MediaPlayer的問題。這是在SherlockFragment,它在我使用XML創建按鈕時起作用。Android MediaPlayer錯誤-1004

工作的代碼是這樣的:

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 

    case R.id.stop: 
     if(mPlayer!=null && mPlayer.isPlaying()){ 
      mPlayer.stop(); 
      buttonPlay.setText("Play"); 
      mPlayer.reset(); 
      buttonPlay.setEnabled(true); 
     } 
     break; 
    case R.id.play: 
     mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
     buttonPlay.setEnabled(false); 
     buttonPlay.setText("Playing..."); 
     try { 
      mPlayer.setDataSource(url); 
     } catch (IllegalArgumentException e) { 
      Toast.makeText(getActivity(), "mPlayer.setDataSource(url); error 1", Toast.LENGTH_LONG).show(); 
     } catch (SecurityException e) { 
      Toast.makeText(getActivity(), "mPlayer.setDataSource(url); error 2", Toast.LENGTH_LONG).show(); 
     } catch (IllegalStateException e) { 
      Toast.makeText(getActivity(), "mPlayer.setDataSource(url); error 3", Toast.LENGTH_LONG).show(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     mPlayer.prepareAsync(); 
     final ProgressDialog progressDialog; 
     progressDialog = ProgressDialog.show(getActivity(), "", "Fetching files..."); 
     mPlayer.setOnPreparedListener(new OnPreparedListener() { 
      @Override 
      public void onPrepared(MediaPlayer mp) { 
       //Now dismis progress dialog, Media palyer will start playing 
       progressDialog.dismiss(); 
       mp.start(); 
      } 
     }); 
     break; 
    } 
} 

它工作得很好,但我也需要爲程序創建的按鈕如下工作:

  play[i].setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        Log.e("button", "play clicked "); 
        mPlayer = new MediaPlayer(); 
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
        try { 
         mPlayer.setDataSource(url); 
        } catch (IllegalArgumentException e) { 
         Toast.makeText(getActivity(), "mPlayer.setDataSource(url); error 1", Toast.LENGTH_LONG).show(); 
        } catch (SecurityException e) { 
         Toast.makeText(getActivity(), "mPlayer.setDataSource(url); error 2", Toast.LENGTH_LONG).show(); 
        } catch (IllegalStateException e) { 
         Toast.makeText(getActivity(), "mPlayer.setDataSource(url); error 3", Toast.LENGTH_LONG).show(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
        play[a].setText("Playing..."); 

        for(int j =0; j < jsons.length; j++){ 
         if(a != j){ 
          play[j].setEnabled(false); 
          stop[j].setEnabled(false); 
          Log.e("button", "making buttons false"); 
         } 
        } 

        mPlayer.prepareAsync(); 
        final ProgressDialog progressDialog; 
        progressDialog = ProgressDialog.show(getActivity(), "", "Fetching files..."); 
        mPlayer.setOnPreparedListener(new OnPreparedListener() { 
         @Override 
         public void onPrepared(MediaPlayer mp) { 
          //Now dismis progress dialog, Media palyer will start playing 
          progressDialog.dismiss(); 
          mp.start(); 
          Log.e("button", "playing "); 
         } 
        }); 
       } 
      }); 

      stop[i].setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        Log.e("button", "stop clicked "); 
       } 
      }); 

我得到這樣的:

E/MediaPlayer(8362): error (1, -1004) 
E/MediaPlayer(8362): Error (1,-1004) 

url = "http://******.******.ie/music/Mark%20Beswick%20-%20My%20Help.mp3";

我在做什麼錯了?

順便說一句我正在4.1.1 Android上測試,這可能是問題嗎?

回答

0

這是解決方案:

<permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.INTERNET" /> 

<permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

由於在這個問題上的第一個答案 - Play SoundCloud Track

0

錯誤代碼1004困擾我在一些設備中了。最後,我編碼了URL並解決了它!

url = "http://******.******.ie/" + Uri.encode("music/Mark Beswick - My Help.mp3");