我试图在按播放键的时候,程序从WEB服务器上(自己主机上搭建的127.0.0.1/8080/Music/aa.mp3)编译没问题,运行时有错!不能播放,异常如下:
09-22 13:11:38.303: E/Hippo_URL_MP3_Player(333):  Permission denied
09-22 13:11:38.303: E/Hippo_URL_MP3_Player(333):  java.net.SocketException: Permission denied
09-22 13:11:38.303: E/Hippo_URL_MP3_Player(333):  at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method)
09-22 13:11:38.303: E/Hippo_URL_MP3_Player(333):  at dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335)
09-22 13:11:38.303: E/Hippo_URL_MP3_Player(333):  at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:216)
09-22 13:11:38.303: E/Hippo_URL_MP3_Player(333):  at java.net.Socket.checkOpenAndCreate(Socket.java:802)
09-22 13:11:38.303: E/Hippo_URL_MP3_Player(333):  at java.net.Socket.connect(Socket.java:948)
09-22 13:11:38.303: E/Hippo_URL_MP3_Player(333):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
09-22 13:11:38.303: E/Hippo_URL_MP3_Player(333):  at org.apache.harmoorg.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
09-22 13:11:38.303: E/Hippo_URL_MP3_Player(333):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
09-22 13:11:38.303: E/Hippo_URL_MP3_Player(333):  at com.android.EX08.EX08_08Activity.setDataSource(EX08_08Activity.java:232)
09-22 13:11:38.303: E/Hippo_URL_MP3_Player(333):  at com.android.EX08.EX08_08Activity.access$10(EX08_08Activity.java:220)
09-22 13:11:38.303: E/Hippo_URL_MP3_Player(333):  at com.android.EX08.EX08_08Activity$9.run(EX08_08Activity.java:195)
09-22 13:11:38.303: E/Hippo_URL_MP3_Player(333):  at java.lang.Thread.run(Thread.java:1019)
09-22 13:36:53.103: E/MediaPlayer(333):  start called in state 1
09-22 13:36:53.103: E/MediaPlayer(333):  error (-38, 0)
09-22 13:36:53.133: E/MediaPlayer(333):  Error (-38,0)
09-22 13:36:55.323: E/MediaPlayer(333):  Attempt to perform seekTo in wrong state: mPlayer=0x0, mCurrentState=0
09-22 13:36:56.633: E/MediaPlayer(333):  Attempt to perform seekTo in wrong state: mPlayer=0x0, mCurrentState=0
09-22 13:36:58.463: E/MediaPlayer(333):  start called in state 0
09-22 13:36:59.913: E/MediaPlayer(333):  Attempt to perform seekTo in wrong state: mPlayer=0x0, mCurrentState=0
09-22 13:37:00.743: E/MediaPlayer(333):  Attempt to perform seekTo in wrong state: mPlayer=0x0, mCurrentState=0
09-22 13:37:01.995: E/MediaPlayer(333):  start called in state 0
09-22 13:37:03.063: E/MediaPlayer(333):  Attempt to perform seekTo in wrong state: mPlayer=0x0, mCurrentState=0
09-22 13:37:03.063: E/MediaPlayer(333):  pause called in state 0
09-22 13:37:03.063: E/MediaPlayer(333):  stop called in state 0
09-22 13:37:04.673: E/AndroidRuntime(333):  FATAL EXCEPTION: main
09-22 13:37:04.673: E/AndroidRuntime(333):  java.lang.IllegalStateException
09-22 13:37:04.673: E/AndroidRuntime(333):  at android.media.MediaPlayer._stop(Native Method)
09-22 13:37:04.673: E/AndroidRuntime(333):  at android.media.MediaPlayer.stop(MediaPlayer.java:832)
09-22 13:37:04.673: E/AndroidRuntime(333):  at com.android.EX08.EX08_08Activity.playVideo(EX08_08Activity.java:213)
09-22 13:37:04.673: E/AndroidRuntime(333):  at com.android.EX08.EX08_08Activity.access$1(EX08_08Activity.java:136)
09-22 13:37:04.673: E/AndroidRuntime(333):  at com.android.EX08.EX08_08Activity$1.onClick(EX08_08Activity.java:58)
09-22 13:37:04.673: E/AndroidRuntime(333):  at android.view.View.performClick(View.java:2485)
09-22 13:37:04.673: E/AndroidRuntime(333):  at android.view.View$PerformClick.run(View.java:9080)
09-22 13:37:04.673: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-22 13:37:04.673: E/AndroidRuntime(333):  at dalvik.system.NativeStart.main(Native Method)关键代码贴上:public class EX08_08Activity extends Activity
{
  private TextView mTextView01;
  private MediaPlayer mMediaPlayer01;
  private ImageButton mPlay, mReset, mPause, mStop;
  private boolean bIsReleased = false;
  private boolean bIsPaused = false;
  private static final String TAG = "Hippo_URL_MP3_Player";
//  private SurfaceView mSurfaceView01; 
//  private SurfaceHolder mSurfaceHolder01; 
  
  private String currentFilePath = "";
  
  private String currentTempFilePath = "";
  private String strVideoURL = "";
  
  /** Called when the activity is first created. */
  @Override 
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     
    //strVideoURL = "http://www.dubblogs.cc:8751/Android/Test/Media/mp3/test.mp3";
    strVideoURL = "http://localhost/:8080/Music/aa.mp3";
    
    mTextView01 = (TextView)findViewById(R.id.myTextView1);
    
    getWindow().setFormat(PixelFormat.TRANSPARENT);
    
    mPlay = (ImageButton)findViewById(R.id.play);
    mReset = (ImageButton)findViewById(R.id.reset);
    mPause = (ImageButton)findViewById(R.id.pause);
    mStop = (ImageButton)findViewById(R.id.stop);
     
    mPlay.setOnClickListener(new ImageButton.OnClickListener()
    { 
      public void onClick(View view)
      {
        playVideo(strVideoURL);
        mTextView01.setText
        (
          getResources().getText(R.string.str_play).toString()+
          "\n"+ strVideoURL
        );
      }
    });
    
     
  private void playVideo(final String strPath)
  {
    try 
    {
      if (strPath.equals(currentFilePath)&& mMediaPlayer01 != null)
      {
        mMediaPlayer01.start(); 
        return; 
      }
      
      currentFilePath = strPath;
      
      mMediaPlayer01 = new MediaPlayer();
      mMediaPlayer01.setAudioStreamType(2);
      
      mMediaPlayer01.setOnErrorListener(new MediaPlayer.OnErrorListener()
      {
        public boolean onError(MediaPlayer mp, int what, int extra)
        {
          //TODO Auto-generated method stub 
          Log.i(TAG, "Error on Listener, what: " + what + "extra: " + extra); 
          return false;
        }
      });
      
      mMediaPlayer01.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener()
      {
        public void onBufferingUpdate(MediaPlayer mp, int percent)
        {
          //TODO Auto-generated method stub 
          Log.i(TAG, "Update buffer: " + Integer.toString(percent)+ "%");
        }
      });
      
      mMediaPlayer01.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
      {
        public void onCompletion(MediaPlayer mp)
        {
          //TODO Auto-generated method stub 
          delFile(currentTempFilePath);
          Log.i(TAG,"mMediaPlayer01 Listener Completed");
        }
      });
      
      mMediaPlayer01.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
      {
        public void onPrepared(MediaPlayer mp)
        {
          //TODO Auto-generated method stub 
          Log.i(TAG,"Prepared Listener"); 
        }
      });
      
      Runnable r = new Runnable()
      {  
        public void run()
        {  
          try 
          {
            setDataSource(strPath);
            Log.i(TAG, "Duration: " + mMediaPlayer01.getDuration());
            
            mMediaPlayer01.start();
            bIsReleased = false;
          }
          catch (Exception e)
          {
            Log.e(TAG, e.getMessage(), e);
          }
        }
      };  
      new Thread(r).start();
    }
    catch(Exception e)
    {
      if (mMediaPlayer01 != null)
      {
        mMediaPlayer01.stop();
        mMediaPlayer01.release();
      }
      e.printStackTrace();
    }
  }
  
  private void setDataSource(String strPath) throws Exception 
  {
    if (!URLUtil.isNetworkUrl(strPath))
    {
      mMediaPlayer01.setDataSource(strPath);
    }
    else
    {
      if(bIsReleased == false)
      {
        URL myURL = new URL(strPath);  
        URLConnection conn = myURL.openConnection();  
        conn.connect();
        
        InputStream is = conn.getInputStream();  
        if (is == null)
        {
          throw new RuntimeException("stream is null");
        }
        
        File myTempFile = File.createTempFile("hippoplayertmp", "."+getFileExtension(strPath));
        currentTempFilePath = myTempFile.getAbsolutePath();
        
//         currentTempFilePath = "/sdcard/hippoplayertmp39327.mp3" 
        
        
        if(currentTempFilePath!="")
        {
          Log.i(TAG, currentTempFilePath);
        }
        
        
        FileOutputStream fos = new FileOutputStream(myTempFile);
        byte buf[] = new byte[128];  
        do
        { 
          int numread = is.read(buf);
          if (numread <= 0)
          {
            break;
          }
          fos.write(buf, 0, numread);
        }while (true);
        
        mMediaPlayer01.setDataSource(currentTempFilePath);
        try
        {
          is.close();
        }
        catch (Exception ex)
        {
          Log.e(TAG, "error: " + ex.getMessage(), ex);
        }
      }
    }
  }
  
  private String getFileExtension(String strFileName)
  {
    File myFile = new File(strFileName);
    String strFileExtension=myFile.getName();
    strFileExtension=(strFileExtension.substring(strFileExtension.lastIndexOf(".")+1)).toLowerCase();
    if(strFileExtension=="")
    {
      strFileExtension = "dat";
    }
    return strFileExtension;
  }
  
  private void delFile(String strFileName)
  {
    File myFile = new File(strFileName);
    if(myFile.exists())
    {
      myFile.delete();
    }
  }
  }   求大神指教,并告知错在何处

解决方案 »

  1.   

    1.没有访问socket的权限,需要在manifest文件中增加相应的访问权限,具体该加什么权限可以查一下
    java.net.SocketException: Permission denied
    2.MediaPlayer的调用状态不对,idle状态下不能调用start,pause,stop等状态。MediaPlayer的状态调用关系可以仔细阅读下API说明:
    http://developer.android.com/reference/android/media/MediaPlayer.html
    09-22 13:36:53.103: E/MediaPlayer(333): start called in state 1
    09-22 13:37:01.995: E/MediaPlayer(333): start called in state 0
    09-22 13:37:03.063: E/MediaPlayer(333): Attempt to perform seekTo in wrong state: mPlayer=0x0, mCurrentState=0
    09-22 13:37:03.063: E/MediaPlayer(333): pause called in state 0
    09-22 13:37:03.063: E/MediaPlayer(333): stop called in state 0
      

  2.   

    非常感谢大侠回答!
    1    添加了socket权限   :  <uses-permission android:name="android.permission.INTERNET">
      </uses-permission>
    2     目前为止报的错误如下:
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335): localhost/127.0.0.1:80 - Connection refused
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335): java.net.ConnectException: localhost/127.0.0.1:80 - Connection refused
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:207)
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335):  at java.net.Socket.connect(Socket.java:983)
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335):  at com.android.EX08.EX08_08Activity.setDataSource(EX08_08Activity.java:232)
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335):  at com.android.EX08.EX08_08Activity.access$10(EX08_08Activity.java:220)
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335):  at com.android.EX08.EX08_08Activity$9.run(EX08_08Activity.java:195)
    09-23 03:36:46.473: E/Hippo_URL_MP3_Player(335):  at java.lang.Thread.run(Thread.java:1019)我将IIS服务器端口绑定成8080 也就是说:在浏览器中输入:localhost/Music/aa.mp3可以访问到这个文件,不需要添加:8080  但报错是:localhost/127.0.0.1:80 - Connection refused
       我觉着它这样报错,是不是意味着我应该把IIS端口绑定成80?可是纵然是绑成80,依旧是这个问题;
    到底是哪里错了一步?小弟刚学,望多指教