2017-02-06 4 views
0

setDataSourceを使用してUriソースをMediaPlayerのオブジェクトに配置する際にIOExceptionエラーが発生しました。Media PlayerでUriソースを配置する際にIOExceptionエラーが発生しました。

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(intent, 10); 

onActivityResult

@Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

      if(resultCode == RESULT_OK && requestCode == 10){ 
       Uri uriSound=data.getData(); 
       Toast.makeText(getApplicationContext(),"It Came Here",Toast.LENGTH_SHORT).show(); 
       play_sound(uriSound); 
      } 
     } 

play_sound

private void play_sound(Uri uri) { 


      Toast.makeText(getApplicationContext(),"It Came Here 2" + uri,Toast.LENGTH_LONG).show(); 

      MediaPlayer mp = new MediaPlayer(); 

      try { 

       mp.setDataSource(getApplicationContext(), uri); 
       mp.start(); 
      } catch (IllegalArgumentException e) { 

       Toast.makeText(getApplicationContext(),"Error 1",Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } catch (SecurityException e) { 
       Toast.makeText(getApplicationContext(),"Error 2",Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } catch (IllegalStateException e) { 
       Toast.makeText(getApplicationContext(),"Error 3",Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } catch (IOException e) { 
       Toast.makeText(getApplicationContext(),"Error 4",Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } 

     } 

私はエラーがどこにあるかを確認するにはトーストを使用しを使用して携帯電話からの歌のURIを取得しようとしています。

「IOException e」で取得していることを知っていません。私が取得したすべてがグーグルであることが分かっています。選択したファイルの「ファイル形式」の問題です。

答えて

0

解決済み!

private void Play_Sound(Uri uri) 
     { 
      MediaPlayer mPlayer = new MediaPlayer(); 
      mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
      try { 
       mPlayer.setDataSource(getApplicationContext(), uri); 
      } catch (IllegalArgumentException e) { 
       Toast.makeText(getApplicationContext(), "You might 1 not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } catch (SecurityException e) { 
       Toast.makeText(getApplicationContext(), "You might 2 not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } catch (IllegalStateException e) { 
       Toast.makeText(getApplicationContext(), "You might 3 not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } catch (IOException e) { 
       Toast.makeText(getApplicationContext(), "You might 4 not set the URI correctly!", Toast.LENGTH_LONG).show(); 

       e.printStackTrace(); 
      } 
      try { 
       mPlayer.prepare(); 
      } catch (IllegalStateException e) { 
       Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } catch (IOException e) { 
       Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); 
      } 
      mPlayer.start(); 
     } 
:ちょうど一行にはサウンド

mP.setAudioStreamType(AudioManager.STREAM_MUSIC); 

Play_Sound(ウリURI)を再生追加することにより、

関連する問題